1 #!/usr/bin/env node
2
3 const fs = require("fs");
4 const path = require("path");
5
6 const [
7 packageName,
8 version,
9 operatingSystem,
10 architecture,
11 // `@smbcloud` is the pre-1.5.5 scope, still published alongside `@getsigit`
12 // so installs made before the rename keep updating.
13 scope = "@getsigit",
14 ] = process.argv.slice(2);
15
16 if (!packageName || !version || !operatingSystem || !architecture) {
17 throw new Error(
18 "Usage: render-platform-package.cjs <package-name> <version> <os> <arch> [scope]",
19 );
20 }
21
22 const npmRoot = path.resolve(__dirname, "..");
23 const packageDirectory = path.resolve(npmRoot, packageName);
24
25 fs.mkdirSync(packageDirectory, { recursive: true });
26
27 // Variable map for template interpolation
28 const vars = {
29 node_scope: scope,
30 node_pkg: packageName,
31 node_version: version,
32 node_os: operatingSystem,
33 node_arch: architecture,
34 };
35
36 /**
37 * Replace every `${key}` in the template with the corresponding value from vars.
38 */
39 function interpolate(template, variables) {
40 return template.replace(/\$\{(\w+)\}/g, (match, key) => {
41 if (key in variables) return variables[key];
42 return match;
43 });
44 }
45
46 // Read and interpolate package.json.tmpl
47 const packageTemplate = fs.readFileSync(
48 path.join(npmRoot, "package.json.tmpl"),
49 "utf-8",
50 );
51 fs.writeFileSync(
52 path.join(packageDirectory, "package.json"),
53 interpolate(packageTemplate, vars),
54 );
55
56 // Read and interpolate README.md.tmpl
57 const readmeTemplate = fs.readFileSync(
58 path.join(npmRoot, "README.md.tmpl"),
59 "utf-8",
60 );
61 fs.writeFileSync(
62 path.join(packageDirectory, "README.md"),
63 interpolate(readmeTemplate, vars),
64 );