Move a couple scripts to eng/scripts/ and update Rush commands

This commit is contained in:
David Wilson 2021-01-05 15:20:51 -08:00
Родитель e4e6f29d3b
Коммит 5f66b64568
3 изменённых файлов: 108 добавлений и 0 удалений

73
eng/scripts/for-each.js Normal file
Просмотреть файл

@ -0,0 +1,73 @@
const { spawn } = require('child_process');
const { readFileSync } = require('fs');
const { resolve } = require('path');
function read(filename) {
const txt = readFileSync(filename, "utf8")
.replace(/\r/gm, "")
.replace(/\n/gm, "«")
.replace(/\/\*.*?\*\//gm, "")
.replace(/«/gm, "\n")
.replace(/\s+\/\/.*/g, "");
return JSON.parse(txt);
}
const repo = `${__dirname}/../..`;
const rush = read(`${repo}/rush.json`);
const pjs = {};
function forEachProject(onEach) {
// load all the projects
for (const each of rush.projects) {
const packageName = each.packageName;
const projectFolder = resolve(`${repo}/${each.projectFolder}`);
const project = JSON.parse(readFileSync(`${projectFolder}/package.json`));
onEach(packageName, projectFolder, project);
}
}
function npmForEach(cmd) {
let count = 0;
let exitCode = 0;
const result = {};
const procs = [];
const t1 = process.uptime() * 100;
forEachProject((name, location, project) => {
// checks for the script first
if (project.scripts[cmd]) {
count++;
const proc = spawn("npm", ["--silent", "run", cmd], { cwd: location, shell: true, stdio: "inherit" });
procs.push(proc);
result[name] = {
name, location, project, proc,
};
}
});
procs.forEach(proc => proc.on("close", (code, signal) => {
count--;
exitCode += code;
if (count === 0) {
const t2 = process.uptime() * 100;
console.log('---------------------------------------------------------');
if (exitCode !== 0) {
console.log(` Done : command '${cmd}' - ${Math.floor(t2 - t1) / 100} s -- Errors ${exitCode} `)
} else {
console.log(` Done : command '${cmd}' - ${Math.floor(t2 - t1) / 100} s -- No Errors `)
}
console.log('---------------------------------------------------------');
process.exit(exitCode);
}
}));
return result;
}
module.exports.forEachProject = forEachProject;
module.exports.npm = npmForEach;
module.exports.projectCount = rush.projects.length;

2
eng/scripts/npm-run.js Normal file
Просмотреть файл

@ -0,0 +1,2 @@
// Runs the npm run command on each project that has it.
require('./for-each').npm(process.argv[2]);

33
eng/scripts/watch.js Normal file
Просмотреть файл

@ -0,0 +1,33 @@
var cp = require('child_process');
require('./for-each').forEachProject((packageName, projectFolder, project) => {
if (project.scripts && project.scripts.watch) {
// NOTE: We deliberately use `tsc --watch --project ${projectFolder}` here
// with cwd at the repo root instead of `npm run watch` with cwd at project
// folder. This ensures that error messages put source file paths relative
// to the repo root, which then allows VS Code to navigate to error
// locations correctly.
const tsc = `${projectFolder}/node_modules/.bin/tsc`;
const args = ['--watch', '--project', projectFolder];
console.log(`${tsc} ${args.join(' ')}`);
const proc = cp.spawn(tsc, args, { cwd: `${__dirname}/../`, shell: true, stdio: "inherit" });
proc.on("error", (c, s) => {
console.log(packageName);
console.error(c);
console.error(s);
});
proc.on('exit', (c, s) => {
console.log(packageName);
console.error(c);
console.error(s);
});
proc.on('message', (c, s) => {
console.log(packageName);
console.error(c);
console.error(s);
})
}
});