1 #!/usr/bin/env node
2
3 const fs = require("fs");
4 const path = require("path");
5
6 const [outputPath, version, templateName = "package-main.json.tmpl"] =
7 process.argv.slice(2);
8
9 if (!outputPath || !version) {
10 throw new Error(
11 "Usage: render-main-package.cjs <output-path> <version> [template-name]",
12 );
13 }
14
15 const npmRoot = path.resolve(__dirname, "..");
16
17 const vars = {
18 release_version: version,
19 };
20
21 /**
22 * Replace every `${key}` in the template with the corresponding value from vars.
23 */
24 function interpolate(template, variables) {
25 return template.replace(/\$\{(\w+)\}/g, (match, key) => {
26 if (key in variables) return variables[key];
27 return match;
28 });
29 }
30
31 // Read and interpolate the requested template: package-main.json.tmpl for
32 // @getsigit/sigit, package-compat.json.tmpl for the @smbcloud/sigit copy.
33 const template = fs.readFileSync(path.join(npmRoot, templateName), "utf-8");
34
35 fs.writeFileSync(path.resolve(outputPath), interpolate(template, vars));