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