microsoft/typespec

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
55e232d553efb50cdb0f2db3fea905f63cc9248a

Branches

Tags

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

Clone

HTTPS

Download ZIP

eng/scripts/helpers.js

79lines · modecode

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