diff --git a/build.js b/build.js index 572f20ae..e114e019 100644 --- a/build.js +++ b/build.js @@ -1,5 +1,7 @@ +// @ts-check + import { build } from "esbuild"; -import { esbuildPluginVersionInjector } from "esbuild-plugin-version-injector"; +import esbuildPluginMetadataInjector from "./esbuild-plugin-metadata-injector/plugin.js"; import packageJson from "./package.json" with { type: "json" }; await build({ @@ -9,6 +11,6 @@ await build({ platform: "node", format: "esm", external: Object.keys(packageJson.dependencies), - plugins: [esbuildPluginVersionInjector()], + plugins: [esbuildPluginMetadataInjector()], minify: true, }); diff --git a/esbuild-plugin-metadata-injector/placeholder.d.ts b/esbuild-plugin-metadata-injector/placeholder.d.ts new file mode 100644 index 00000000..315f6bae --- /dev/null +++ b/esbuild-plugin-metadata-injector/placeholder.d.ts @@ -0,0 +1,2 @@ +export const PACKAGE_VERSION: string; +export const GIT_DESCRIBE: string; diff --git a/esbuild-plugin-metadata-injector/placeholder.js b/esbuild-plugin-metadata-injector/placeholder.js new file mode 100644 index 00000000..2b82adcf --- /dev/null +++ b/esbuild-plugin-metadata-injector/placeholder.js @@ -0,0 +1,7 @@ +// @ts-check + +import gitDescribe from "git-describe"; +import packageJSON from "../package.json" with { type: "json" }; + +export const PACKAGE_VERSION = packageJSON.version; +export const GIT_DESCRIBE = gitDescribe.gitDescribeSync().raw; diff --git a/esbuild-plugin-metadata-injector/plugin.js b/esbuild-plugin-metadata-injector/plugin.js new file mode 100644 index 00000000..d437992a --- /dev/null +++ b/esbuild-plugin-metadata-injector/plugin.js @@ -0,0 +1,29 @@ +// @ts-check + +import { fileURLToPath } from "node:url"; +import * as placeholder from "./placeholder.js"; + +/** + * @param {string} path + * @returns {RegExp} + */ +function exact(path) { + return new RegExp(`^${path.replace(/[/.]/g, "\\$&")}$`); +} + +const target = fileURLToPath(import.meta.resolve("./placeholder.js")); + +/** + * @returns {import("esbuild").Plugin} + */ +export default function esbuildPluginMetadataInjector() { + return { + name: "esbuild-plugin-metadata-injector", + setup(build) { + build.onLoad({ filter: exact(target) }, () => ({ + loader: "json", + contents: JSON.stringify(placeholder), + })); + }, + }; +} diff --git a/package-lock.json b/package-lock.json index 3339e159..8c103794 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,10 +23,10 @@ "@types/node": "20.14.2", "dotenv": "16.4.5", "esbuild": "0.21.4", - "esbuild-plugin-version-injector": "1.2.1", "eslint": "8.57.0", "eslint-config-prettier": "9.1.0", "eslint-plugin-import": "2.29.1", + "git-describe": "4.1.1", "globals": "15.4.0", "husky": "9.0.11", "lint-staged": "15.2.5", @@ -1021,17 +1021,6 @@ "npm": ">=7.0.0" } }, - "node_modules/@sapphire/result": { - "version": "2.6.6", - "resolved": "https://registry.npmjs.org/@sapphire/result/-/result-2.6.6.tgz", - "integrity": "sha512-QCjj7X/QlY0QUCeAaZQmnrsMH/b2BMQYee3F1Y5iF17JagUQqO3KZlG7vfXWQU3SRAJX5OgZZynBjixUH+nNGg==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=v14.0.0", - "npm": ">=7.0.0" - } - }, "node_modules/@sapphire/shapeshift": { "version": "3.9.7", "resolved": "https://registry.npmjs.org/@sapphire/shapeshift/-/shapeshift-3.9.7.tgz", @@ -1164,6 +1153,12 @@ "csstype": "^3.0.2" } }, + "node_modules/@types/semver": { + "version": "7.5.8", + "resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.5.8.tgz", + "integrity": "sha512-I8EUhyrgfLrcTkzV3TSsGyl1tSuPrEDzr0yd5m90UgNxQkyDXULk3b6MlQqTCpZpNtWe1K0hzclnZkTcLBe2UQ==", + "dev": true + }, "node_modules/@types/ws": { "version": "8.5.10", "resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.10.tgz", @@ -2430,20 +2425,6 @@ "@esbuild/win32-x64": "0.21.4" } }, - "node_modules/esbuild-plugin-version-injector": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/esbuild-plugin-version-injector/-/esbuild-plugin-version-injector-1.2.1.tgz", - "integrity": "sha512-OKrLqniWutFlv36lKm2lIhJS1L5hncmbC4NXWjEt2a4bcMuyZ5TcZ/idUpH1Vju5FVlGX122cYcRhkMxHlq5GA==", - "dev": true, - "license": "MIT", - "dependencies": { - "@sapphire/result": "^2.6.4" - }, - "engines": { - "node": ">=v14.0.0", - "npm": ">=7.0.0" - } - }, "node_modules/escape-string-regexp": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz", @@ -3052,6 +3033,32 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/git-describe": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/git-describe/-/git-describe-4.1.1.tgz", + "integrity": "sha512-JC8ganO5kO80G8+XE98TDDjnMXQN3Estk3qdJuG2EGRF/l6zuMTMcN+8OSfQZ5FWpqIRLB015anWX4aSRgnxAQ==", + "dev": true, + "dependencies": { + "@types/semver": "^7.3.8", + "lodash": "^4.17.21" + }, + "engines": { + "node": ">=4.0.0" + }, + "optionalDependencies": { + "semver": "^5.6.0" + } + }, + "node_modules/git-describe/node_modules/semver": { + "version": "5.7.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.2.tgz", + "integrity": "sha512-cBznnQ9KjJqU67B52RMC65CMarK2600WFnbkcaiwWq3xy/5haFJlshgnpjovMVJ+Hff49d8GEn0b87C5pDQ10g==", + "dev": true, + "optional": true, + "bin": { + "semver": "bin/semver" + } + }, "node_modules/glob": { "version": "7.2.3", "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz", diff --git a/package.json b/package.json index b3430fbf..20980344 100644 --- a/package.json +++ b/package.json @@ -26,10 +26,10 @@ "@types/node": "20.14.2", "dotenv": "16.4.5", "esbuild": "0.21.4", - "esbuild-plugin-version-injector": "1.2.1", "eslint": "8.57.0", "eslint-config-prettier": "9.1.0", "eslint-plugin-import": "2.29.1", + "git-describe": "4.1.1", "globals": "15.4.0", "husky": "9.0.11", "lint-staged": "15.2.5", diff --git a/src/commands/version.ts b/src/commands/version.ts index cc9d51e1..a164ae0f 100644 --- a/src/commands/version.ts +++ b/src/commands/version.ts @@ -7,7 +7,10 @@ import type { ChatInputCommandInteraction, RESTPostAPIChatInputApplicationCommandsJSONBody, } from "discord.js"; -import { OM_VERSION } from "../version"; +import { + PACKAGE_VERSION, + GIT_DESCRIBE, +} from "esbuild-plugin-metadata-injector/placeholder"; export const definition = { name: "version", @@ -19,7 +22,7 @@ export async function handler( ) { await interaction.reply({ content: `\`\`\` -discordjs-japan/om: ${OM_VERSION} +discordjs-japan/om: ${PACKAGE_VERSION} (${GIT_DESCRIBE}) discordjs-japan/om-syrinx: ${OM_SYRINX_VERSION} jpreprocess/jpreprocess: ${JPREPROCESS_VERSION} jpreprocess/jbonsai: ${JBONSAI_VERSION} diff --git a/src/main.ts b/src/main.ts index 204d9235..fa2d6f59 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,11 +1,11 @@ import { ActivityType, Client, Events, GatewayIntentBits } from "discord.js"; +import { PACKAGE_VERSION } from "esbuild-plugin-metadata-injector/placeholder"; import * as join from "./commands/join"; import * as leave from "./commands/leave"; import * as skip from "./commands/skip"; import * as version from "./commands/version"; import { ReplyableError } from "./error"; import Pipeline from "./pipeline"; -import { OM_VERSION } from "./version"; process.title = "discordjs-japan/om"; @@ -64,7 +64,7 @@ client.once(Events.ClientReady, async (client) => { ]); client.user.setActivity({ type: ActivityType.Custom, - name: `v${OM_VERSION}`, + name: `v${PACKAGE_VERSION}`, }); }); diff --git a/src/version.ts b/src/version.ts deleted file mode 100644 index 1ed8680d..00000000 --- a/src/version.ts +++ /dev/null @@ -1 +0,0 @@ -export const OM_VERSION: string = "[VI]{{inject}}[/VI]"; diff --git a/tsconfig.json b/tsconfig.json index 2e2eb5db..3a451391 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,7 +13,12 @@ "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, - "verbatimModuleSyntax": true + "verbatimModuleSyntax": true, + "paths": { + "esbuild-plugin-metadata-injector/placeholder": [ + "./esbuild-plugin-metadata-injector/placeholder" + ] + } }, "include": ["src/**/*"] }