microsoft/typespec

Public

mirrored from https://github.com/microsoft/typespecAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
a710be4a437129eacb9c7ecc523bee1c2889e1fc

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

eng/scripts/helpers.js

68lines · modecode

1import { spawnSync } from "child_process";
2import { readFileSync } from "fs";
3import { dirname, resolve } from "path";
4import { fileURLToPath } from "url";
5
6function read(filename) {
7 const txt = readFileSync(filename, "utf8")
8 .replace(/\r/gm, "")
9 .replace(/\n/gm, "«")
10 .replace(/\/\*.*?\*\//gm, "")
11 .replace(/«/gm, "\n")
12 .replace(/\s+\/\/.*/g, "");
13 return JSON.parse(txt);
14}
15
16export const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "../..");
17
18const rush = read(`${repoRoot}/rush.json`);
19
20export function forEachProject(onEach) {
21 // load all the projects
22 for (const each of rush.projects) {
23 const packageName = each.packageName;
24 const projectFolder = resolve(`${repoRoot}/${each.projectFolder}`);
25 const project = JSON.parse(readFileSync(`${projectFolder}/package.json`));
26 onEach(packageName, projectFolder, project);
27 }
28}
29
30export function npmForEach(cmd) {
31 forEachProject((name, location, project) => {
32 // checks for the script first
33 if (project.scripts[cmd] || cmd === "pack") {
34 const args = cmd === "pack" ? [cmd] : ["run", cmd];
35 run("npm", args, { cwd: location });
36 }
37 });
38}
39
40// We could use { shell: true } to let Windows find .cmd, but that causes other issues.
41// It breaks ENOENT checking for command-not-found and also handles command/args with spaces
42// poorly.
43const isCmdOnWindows = ["rush", "npm", "code", "code-insiders"];
44
45export function run(command, args, options) {
46 console.log();
47 console.log(`> ${command} ${args.join(" ")}`);
48
49 options = {
50 stdio: "inherit",
51 ...options,
52 };
53
54 if (process.platform === "win32" && isCmdOnWindows.includes(command)) {
55 command += ".cmd";
56 }
57
58 const proc = spawnSync(command, args, options);
59 if (proc.error) {
60 if (options.ignoreCommandNotFound && proc.error.code === "ENOENT") {
61 console.log("Skipped: Command not found.");
62 } else {
63 throw proc.error;
64 }
65 } else if (proc.status !== 0) {
66 throw new Error(`Command failed with exit code ${proc.status}`);
67 }
68}
69