forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
79 lines (70 loc) · 2.52 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// @ts-check
import { readFileSync } from "fs";
import { dirname, join, resolve } from "path";
import { fileURLToPath } from "url";
import { run, runOrExit } from "../../packages/internal-build-utils/dist/src/index.js";
function read(filename) {
const txt = readFileSync(filename, "utf-8")
.replace(/\r/gm, "")
.replace(/\n/gm, "«")
.replace(/\/\*.*?\*\//gm, "")
.replace(/«/gm, "\n")
.replace(/\s+\/\/.*/g, "");
return JSON.parse(txt);
}
export const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "../..");
export const prettier = resolve(repoRoot, "packages/compiler/node_modules/.bin/prettier");
export const tsc = resolve(repoRoot, "packages/compiler/node_modules/.bin/tsc");
const rush = read(`${repoRoot}/rush.json`);
export function forEachProject(onEach, filter) {
// load all the projects
for (const each of rush.projects) {
const packageName = each.packageName;
if (filter !== undefined && !filter.includes(packageName)) continue;
const projectFolder = resolve(`${repoRoot}/${each.projectFolder}`);
const project = JSON.parse(readFileSync(`${projectFolder}/package.json`, "utf-8"));
onEach(packageName, projectFolder, project);
}
}
export function npmForEachDependency(cmd, projectDir, options) {
const project = JSON.parse(readFileSync(`${projectDir}/package.json`, "utf-8"));
const deps = [
Object.keys(project.dependencies || {}),
Object.keys(project.devDependencies || {}),
Object.keys(project.peerDependencies || {}),
].flat();
forEachProject((name, location, project) => {
if (project.scripts[cmd] || cmd === "pack") {
const args = cmd === "pack" ? [cmd] : ["run", cmd];
runOrExit("npm", args, { cwd: location, ...options });
}
}, deps);
}
export function npmForEach(cmd, options) {
forEachProject((name, location, project) => {
if (cmd === "test-official" && !project.scripts[cmd] && project.scripts["test"]) {
const pj = join(location, "package.json");
throw new Error(`${pj} has a 'test' script, but no 'test-official' script for CI.`);
}
if (project.scripts[cmd] || cmd === "pack") {
const args = cmd === "pack" ? [cmd] : ["run", cmd];
runOrExit("npm", args, { cwd: location, ...options });
}
});
}
export async function runPrettier(...args) {
await run(
prettier,
[
...args,
"--config",
".prettierrc.json",
"--ignore-path",
".prettierignore",
"**/*.{ts,js,tsx,jsx,cjs,mjs,css,json,yml,yaml,tsp,cadl,md}",
],
{
cwd: repoRoot,
}
);
}