|
1 |
| -import { execSync } from "node:child_process"; |
2 |
| -import { existsSync as fsExistsSync } from "node:fs"; |
3 |
| -import { mkdir as fsMkdir, readdir as fsReaddir, readFile as fsReadFile, rm as fsRm, writeFile as fsWriteFile } from "node:fs/promises"; |
| 1 | +import { mkdir as fsMkdir, readdir as fsReaddir, rm as fsRm, writeFile as fsWriteFile } from "node:fs/promises"; |
4 | 2 | import { dirname as pathDirname, join as pathJoin } from "node:path";
|
5 | 3 | import { fileURLToPath } from "node:url";
|
6 | 4 | import ncc from "@vercel/ncc";
|
7 |
| -const root = pathDirname(fileURLToPath(import.meta.url)); |
8 |
| -const packageFileName = "package.json"; |
9 |
| -const scriptEntryPointFileName = "main.js"; |
10 |
| -const inputDirectoryPath = pathJoin(root, "temp"); |
11 |
| -const inputFilePath = pathJoin(inputDirectoryPath, scriptEntryPointFileName); |
12 |
| -const outputDirectoryPath = pathJoin(root, "hugoalh.GitHubActionsToolkit", "nodejs-wrapper"); |
13 |
| -const outputFilePath = pathJoin(outputDirectoryPath, scriptEntryPointFileName); |
14 |
| -async function getDirectoryItem(directoryPath, relativeBasePath) { |
15 |
| - if (typeof relativeBasePath === "undefined") { |
16 |
| - relativeBasePath = directoryPath; |
17 |
| - } |
18 |
| - try { |
19 |
| - let result = []; |
20 |
| - for (let item of await fsReaddir(directoryPath, { withFileTypes: true })) { |
21 |
| - if (item.isDirectory()) { |
22 |
| - result.push(...await getDirectoryItem(pathJoin(directoryPath, item.name), relativeBasePath)); |
23 |
| - } else { |
24 |
| - result.push(pathJoin(directoryPath, item.name).slice(relativeBasePath.length + 1).replace(/\\/gu, "/")); |
25 |
| - } |
26 |
| - } |
27 |
| - return result; |
28 |
| - } catch (error) { |
29 |
| - return []; |
30 |
| - } |
31 |
| -} |
| 5 | +const workspace = pathDirname(fileURLToPath(import.meta.url)); |
| 6 | +const directoryInput = pathJoin(workspace, "temp"); |
| 7 | +const directoryOutput = pathJoin(workspace, "hugoalh.GitHubActionsToolkit", "nodejs-wrapper"); |
| 8 | +const scripts = new Set([ |
| 9 | + "main.js" |
| 10 | +]); |
32 | 11 |
|
33 |
| -/* Clean up or initialize output directory (need to await in order to prevent race conditions). */ |
34 |
| -if (fsExistsSync(outputDirectoryPath)) { |
35 |
| - for (let fileName of await getDirectoryItem(outputDirectoryPath)) { |
36 |
| - await fsRm(pathJoin(outputDirectoryPath, fileName), { recursive: true }); |
37 |
| - } |
38 |
| -} else { |
39 |
| - await fsMkdir(outputDirectoryPath, { recursive: true }); |
| 12 | +// Initialize output directory. |
| 13 | +await fsMkdir(directoryOutput, { recursive: true }); |
| 14 | +for (const fileName of await fsReaddir(directoryOutput)) { |
| 15 | + await fsRm(pathJoin(directoryOutput, fileName), { maxRetries: 4, recursive: true }); |
40 | 16 | }
|
41 | 17 |
|
42 |
| -/* Create bundle. */ |
43 |
| -console.log(execSync(`"${pathJoin(root, "node_modules", ".bin", process.platform === "win32" ? "tsc.cmd" : "tsc")}" -p "${pathJoin(root, "tsconfig.json")}"`).toString("utf8")); |
44 |
| -let { code } = await ncc(inputFilePath, { |
45 |
| - assetBuilds: false, |
46 |
| - cache: false, |
47 |
| - debugLog: false, |
48 |
| - license: "", |
49 |
| - minify: true, |
50 |
| - quiet: false, |
51 |
| - sourceMap: false, |
52 |
| - sourceMapRegister: false, |
53 |
| - target: "es2022", |
54 |
| - v8cache: false, |
55 |
| - watch: false |
56 |
| -}); |
57 |
| -await fsWriteFile(outputFilePath, code, { encoding: "utf8" }); |
58 |
| -let packageMeta = JSON.parse(await fsReadFile(pathJoin(root, packageFileName), { encoding: "utf8" })); |
59 |
| -delete packageMeta.scripts; |
60 |
| -delete packageMeta.dependencies; |
61 |
| -delete packageMeta.devDependencies; |
62 |
| -await fsWriteFile(pathJoin(outputDirectoryPath, packageFileName), `${JSON.stringify(packageMeta, undefined, "\t")}\n`, { encoding: "utf8" }); |
| 18 | +// Create bundle. |
| 19 | +for (const script of scripts.values()) { |
| 20 | + const { code } = await ncc(pathJoin(directoryInput, script), { |
| 21 | + assetBuilds: false, |
| 22 | + cache: false, |
| 23 | + debugLog: false, |
| 24 | + license: "", |
| 25 | + minify: true, |
| 26 | + quiet: false, |
| 27 | + sourceMap: false, |
| 28 | + sourceMapRegister: false, |
| 29 | + target: "es2022", |
| 30 | + v8cache: false, |
| 31 | + watch: false |
| 32 | + }); |
| 33 | + await fsWriteFile(pathJoin(directoryOutput, script), code, { encoding: "utf8" }); |
| 34 | +} |
0 commit comments