| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- // Build script for @fmode/wechat-cli.
- //
- // Bundles src/index.ts + src/commands/*.ts into a single self-contained
- // dist/wecli.js with a node shebang. The tgz/zip this plugin gets copied
- // into only needs that one file to be executable via `node dist/wecli.js`
- // or (once `npm link`ed) via plain `wecli`.
- //
- // Intentionally zero runtime deps — this CLI uses Node 20+ globals
- // (fetch, util.parseArgs, fs/promises, os, path) only, so the bundle is
- // tiny and has no cross-platform native binding quirks.
- import { build } from "esbuild";
- import { readFileSync, rmSync, mkdirSync, existsSync, chmodSync } from "node:fs";
- import { fileURLToPath } from "node:url";
- import { dirname, resolve } from "node:path";
- const __dirname = dirname(fileURLToPath(import.meta.url));
- const projectRoot = resolve(__dirname, "..");
- const pkg = JSON.parse(
- readFileSync(resolve(projectRoot, "package.json"), "utf8")
- );
- const outfile = resolve(projectRoot, "dist", "wecli.js");
- const distDir = resolve(projectRoot, "dist");
- if (existsSync(distDir)) {
- rmSync(distDir, { recursive: true, force: true });
- }
- mkdirSync(distDir, { recursive: true });
- const result = await build({
- entryPoints: [resolve(projectRoot, "src", "index.ts")],
- outfile,
- bundle: true,
- format: "esm",
- platform: "node",
- target: "node20",
- sourcemap: "inline",
- legalComments: "none",
- minify: false,
- keepNames: true,
- external: ["node:*"],
- mainFields: ["module", "main"],
- conditions: ["import", "node", "default"],
- logLevel: "info",
- metafile: true,
- treeShaking: true,
- banner: {
- // Shebang first so `chmod +x dist/wecli.js && ./dist/wecli.js` works on
- // Unix. On Windows the shebang is harmless; `npm link` writes its own
- // `.cmd` shim regardless.
- js: `#!/usr/bin/env node\n// @fmode/wechat-cli v${pkg.version} — generated by scripts/build.mjs. Do not edit by hand.`,
- },
- });
- // Make the built file executable on POSIX. No-op on Windows (chmod succeeds
- // silently; the bit is irrelevant because Windows ignores it).
- try {
- chmodSync(outfile, 0o755);
- } catch {
- /* best-effort */
- }
- const inputCount = Object.keys(result.metafile.inputs).length;
- const bundled = readFileSync(outfile, "utf8");
- const outputSize = bundled.length;
- console.log(
- `\n\u2705 bundled ${inputCount} modules -> dist/wecli.js (${(outputSize / 1024).toFixed(1)} KiB)`
- );
- console.log(
- ` Run with: node dist/wecli.js --help (or \`npm link\` + \`wecli --help\`)`
- );
|