Skip to content

Instantly share code, notes, and snippets.

@dannyockilson
Created November 29, 2018 12:44
Show Gist options
  • Save dannyockilson/b25ae96d7563c7eb3689d813e56d58ce to your computer and use it in GitHub Desktop.
Save dannyockilson/b25ae96d7563c7eb3689d813e56d58ce to your computer and use it in GitHub Desktop.
Quick node script to build multiple angular libraries (zero deps, tested on node 8 LTS)
const { spawn } = require('child_process');
const fileName = './angular.json';
let file;
try {
file = require(fileName);
} catch(e) {
console.error(e);
process.exit(1);
}
if(!file.projects) {
console.error('No projects found in angular.json file');
process.exit(1);
}
const libraries = Object.keys(file.projects).filter((name) => file.projects[name].projectType == "library");
if(!libraries || libraries.length == 0) {
console.warn('No libraries found in current angular project, nothing to build');
// throw exit code 0 cause technically everything that "should" be built was...
process.exit(0);
}
async function buildLibrary(name) {
const build = spawn('npx', ['ng', 'build', name]);
build.stdout.on('data', data => console.log(data.toString()));
build.stdout.on('end', _ => console.log(`Finished building ${name} library`));
build.on('exit', code => {
if(code != 0) {
throw `Failed to build library ${name}`;
}
return;
})
}
async function buildAllLibraries(libraries) {
for(let i = 0; i < libraries.length; i++) {
await buildLibrary(libraries[i]);
}
}
try {
buildAllLibraries(libraries);
} catch(e) {
console.error(e);
process.exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment