Skip to content

Commit

Permalink
create and use esbuild-plugin-metadata-injector
Browse files Browse the repository at this point in the history
  • Loading branch information
cm-ayf committed Jun 10, 2024
1 parent 33583c2 commit adcb595
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 35 deletions.
6 changes: 4 additions & 2 deletions build.js
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -9,6 +11,6 @@ await build({
platform: "node",
format: "esm",
external: Object.keys(packageJson.dependencies),
plugins: [esbuildPluginVersionInjector()],
plugins: [esbuildPluginMetadataInjector()],
minify: true,
});
2 changes: 2 additions & 0 deletions esbuild-plugin-metadata-injector/placeholder.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const PACKAGE_VERSION: string;
export const GIT_DESCRIBE: string;
7 changes: 7 additions & 0 deletions esbuild-plugin-metadata-injector/placeholder.js
Original file line number Diff line number Diff line change
@@ -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;
29 changes: 29 additions & 0 deletions esbuild-plugin-metadata-injector/plugin.js
Original file line number Diff line number Diff line change
@@ -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),
}));
},
};
}
59 changes: 33 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 5 additions & 2 deletions src/commands/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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}
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -64,7 +64,7 @@ client.once(Events.ClientReady, async (client) => {
]);
client.user.setActivity({
type: ActivityType.Custom,
name: `v${OM_VERSION}`,
name: `v${PACKAGE_VERSION}`,
});
});

Expand Down
1 change: 0 additions & 1 deletion src/version.ts

This file was deleted.

7 changes: 6 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/**/*"]
}

0 comments on commit adcb595

Please sign in to comment.