Skip to content

Commit e274fb8

Browse files
committed
Implement clean and copy as custom plugins
1 parent 8a3bfe6 commit e274fb8

13 files changed

+43
-207
lines changed

build.mjs

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,55 @@
1-
import { rm } from "node:fs/promises";
1+
import { copyFile, rm } from "node:fs/promises";
22
import { dirname, join } from "node:path";
33
import { fileURLToPath } from "node:url";
44

55
import * as esbuild from "esbuild";
66
import { globSync } from "glob";
7-
import { copy } from "esbuild-plugin-copy";
87

98
const __filename = fileURLToPath(import.meta.url);
109
const __dirname = dirname(__filename);
1110

12-
const OUT_DIR = "lib";
11+
const SRC_DIR = join(__dirname, "src");
12+
const OUT_DIR = join(__dirname, "lib");
1313

14-
await rm(join(__dirname, OUT_DIR), { recursive: true, force: true });
14+
/**
15+
* Clean the output directory before building.
16+
*
17+
* @type {esbuild.Plugin}
18+
*/
19+
const cleanPlugin = {
20+
name: "clean",
21+
setup(build) {
22+
build.onStart(async () => {
23+
await rm(OUT_DIR, { recursive: true, force: true });
24+
});
25+
},
26+
};
1527

16-
// This will just log when a build ends
17-
/** @type {esbuild.Plugin} */
28+
/**
29+
* Copy defaults.json to the output directory since other projects depend on it.
30+
*
31+
* @type {esbuild.Plugin}
32+
*/
33+
const copyDefaultsPlugin = {
34+
name: "copy-defaults",
35+
setup(build) {
36+
build.onEnd(async () => {
37+
await rm(join(OUT_DIR, "defaults.json"), {
38+
force: true,
39+
});
40+
await copyFile(
41+
join(SRC_DIR, "defaults.json"),
42+
join(OUT_DIR, "defaults.json"),
43+
);
44+
});
45+
},
46+
}
47+
48+
/**
49+
* Log when the build ends.
50+
*
51+
* @type {esbuild.Plugin}
52+
*/
1853
const onEndPlugin = {
1954
name: "on-end",
2055
setup(build) {
@@ -25,20 +60,13 @@ const onEndPlugin = {
2560
},
2661
};
2762

28-
const copyDefaults = copy({
29-
assets: {
30-
from: ["src/defaults.json"],
31-
to: ["defaults.json"],
32-
},
33-
});
34-
3563
const context = await esbuild.context({
36-
entryPoints: globSync(["src/*-action.ts", "src/*-action-post.ts"]),
64+
entryPoints: globSync([`${SRC_DIR}/*-action.ts`, `${SRC_DIR}/*-action-post.ts`]),
3765
bundle: true,
3866
format: "cjs",
3967
outdir: OUT_DIR,
4068
platform: "node",
41-
plugins: [copyDefaults, onEndPlugin],
69+
plugins: [cleanPlugin, copyDefaultsPlugin, onEndPlugin],
4270
});
4371

4472
await context.rebuild();

lib/analyze-action-post.js

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/analyze-action.js

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/autobuild-action.js

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action-post.js

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action.js

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/resolve-environment-action.js

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/start-proxy-action-post.js

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/start-proxy-action.js

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-sarif-action-post.js

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)