|
| 1 | +import {exec} from "child_process"; |
| 2 | +import {access} from "fs/promises" |
| 3 | +import {parseArgs, ParseArgsConfig} from 'node:util'; |
| 4 | +import path from "path"; |
| 5 | +import {simpleGit} from 'simple-git'; |
| 6 | + |
| 7 | +async function runCmd(cmd:string, cwd:string) { |
| 8 | + console.log(`run command:${cmd}`) |
| 9 | + const { err, stdout, stderr } = await new Promise((res) => |
| 10 | + exec( |
| 11 | + cmd, |
| 12 | + { encoding: "utf8", maxBuffer: 1024 * 1024 * 64, cwd: cwd}, |
| 13 | + (err: unknown, stdout: unknown, stderr: unknown) => |
| 14 | + res({ err: err, stdout: stdout, stderr: stderr }) |
| 15 | + ) |
| 16 | + ) as any; |
| 17 | + let resultString = stderr + stdout; |
| 18 | + console.log("Stdout output:") |
| 19 | + console.log(stdout) |
| 20 | + if (stderr) { |
| 21 | + console.log("Stderr output:") |
| 22 | + console.log(stderr) |
| 23 | + } |
| 24 | + if (stderr || err) { |
| 25 | + throw new Error(err); |
| 26 | + } |
| 27 | + return resultString as string; |
| 28 | +} |
| 29 | + |
| 30 | +async function checkFileExists(file:string) { |
| 31 | + return access(file) |
| 32 | + .then(() => true) |
| 33 | + .catch(() => false) |
| 34 | +} |
| 35 | + |
| 36 | +export async function main() { |
| 37 | + const args = process.argv.slice(2); |
| 38 | + const options = { |
| 39 | + folder: { |
| 40 | + type: 'string', |
| 41 | + short: 'f', |
| 42 | + }, |
| 43 | + }; |
| 44 | + const parsedArgs = parseArgs({ args, options, allowPositionals: true} as ParseArgsConfig); |
| 45 | + const folder = parsedArgs.positionals[0]; |
| 46 | + console.log("Running TypeSpecValidation on folder:", folder); |
| 47 | + |
| 48 | + // Verify all specs are using root level pacakge.json |
| 49 | + let expected_npm_prefix = process.cwd() |
| 50 | + const actual_npm_prefix = (await runCmd(`npm prefix`, folder)).trim() |
| 51 | + if (expected_npm_prefix !== actual_npm_prefix) { |
| 52 | + console.log("ERROR: TypeSpec folders MUST NOT contain a package.json, and instead MUST rely on the package.json at repo root.") |
| 53 | + throw new Error ("Expected npm prefix: " + expected_npm_prefix +"\nActual npm prefix: " + actual_npm_prefix) |
| 54 | + } |
| 55 | + |
| 56 | + // Spec compilation check |
| 57 | + if (await checkFileExists(path.join(folder, "main.tsp"))) { |
| 58 | + await runCmd( |
| 59 | + `npx --no tsp compile . --warn-as-error`, |
| 60 | + folder |
| 61 | + ); |
| 62 | + } |
| 63 | + if (await checkFileExists(path.join(folder, "client.tsp"))) { |
| 64 | + await runCmd( |
| 65 | + `npx --no tsp compile client.tsp --no-emit --warn-as-error`, |
| 66 | + folder |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + // Format parent folder to include shared files |
| 71 | + await runCmd( |
| 72 | + `npx tsp format ../**/*.tsp`, |
| 73 | + folder |
| 74 | + ); |
| 75 | + |
| 76 | + // Verify generated swagger file is in sync with one on disk |
| 77 | + const git = simpleGit(); |
| 78 | + let gitStatusIsClean = await (await git.status(['--porcelain'])).isClean() |
| 79 | + if (!gitStatusIsClean) { |
| 80 | + let gitStatus = await git.status() |
| 81 | + let gitDiff = await git.diff() |
| 82 | + console.log("git status") |
| 83 | + console.log(gitStatus) |
| 84 | + console.log("git diff") |
| 85 | + console.log(gitDiff) |
| 86 | + throw new Error("Generated swagger file does not match swagger file on disk") |
| 87 | + } |
| 88 | +} |
0 commit comments