microsoft/typespec
Publicmirrored from https://github.com/microsoft/typespecAvailable
packages/compiler/scripts/generate-manifest.js
41lines · modecode
| 1 | // @ts-check |
| 2 | import { execSync } from "child_process"; |
| 3 | import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs"; |
| 4 | import { join } from "path"; |
| 5 | import { fileURLToPath } from "url"; |
| 6 | |
| 7 | const root = fileURLToPath(new URL("..", import.meta.url).href); |
| 8 | const distDir = join(root, "dist"); |
| 9 | const manifestTarget = join(distDir, "manifest.js"); |
| 10 | |
| 11 | function loadPackageJson() { |
| 12 | const packageJsonPath = join(root, "package.json"); |
| 13 | return JSON.parse(readFileSync(packageJsonPath, "utf-8")); |
| 14 | } |
| 15 | |
| 16 | function getCommit() { |
| 17 | return execSync("git rev-parse HEAD").toString().trim(); |
| 18 | } |
| 19 | |
| 20 | function getPrNumber() { |
| 21 | // Set by Azure DevOps. |
| 22 | return process.env["SYSTEM_PULLREQUEST_PULLREQUESTNUMBER"]; |
| 23 | } |
| 24 | |
| 25 | function main() { |
| 26 | const pkg = loadPackageJson(); |
| 27 | |
| 28 | const manifest = { |
| 29 | version: pkg.version, |
| 30 | commit: getCommit(), |
| 31 | pr: getPrNumber(), |
| 32 | }; |
| 33 | |
| 34 | if (!existsSync(distDir)) { |
| 35 | mkdirSync(distDir, { recursive: true }); |
| 36 | } |
| 37 | const manifestJs = `export default ${JSON.stringify(manifest, null, 2)};`; |
| 38 | writeFileSync(manifestTarget, manifestJs); |
| 39 | } |
| 40 | |
| 41 | main(); |
| 42 | |