Skip to content

Instantly share code, notes, and snippets.

@apolaskey
Created November 15, 2020 14:50
Show Gist options
  • Save apolaskey/d31c80b4c524836a23067c7b09369b95 to your computer and use it in GitHub Desktop.
Save apolaskey/d31c80b4c524836a23067c7b09369b95 to your computer and use it in GitHub Desktop.
Build All Angular Projects
// Imports
const fs = require('fs'), spawn = require('child_process').spawn;
// Filter for projects to not build with this
const filter = ['e2e'];
// Amount of projects to build async.
const batch = 3;
/**
* Builds a provide Angular Project
* @param project
* @returns {Promise<unknown>}
*/
function buildProject(project) {
return new Promise((resolve, reject) => {
console.log("Spawning process:");
let cmd;
let logs = [];
if (process.platform === "win32") {
cmd = 'npm.cmd';
} else {
cmd = 'npm';
}
let child = spawn(cmd, ['run', 'ng', 'build', '--project', project]);
console.log(child.spawnargs);
child.stdout.on('data', (data) => {
logs.push(data.toString());
});
child.stderr.on('data', (data) => {
logs.push(data.toString());
});
child.on('close', (code) => {
if (code === 0) {
resolve(code);
} else {
reject(code);
}
});
child.on('exit', (code) => {
logs.forEach((item) => {
process.stdout.write(item);
});
logs.length = 0;
});
});
}
/**
* Util function to check a string for a given index
* @param array
* @param searchItem
* @returns {[]}
*/
function indexOfAll(array, searchItem) {
var i = array.indexOf(searchItem),
indexes = [];
while (i !== -1) {
indexes.push(i);
i = array.indexOf(searchItem, ++i);
}
return indexes;
}
/**
* Filter out projects that contain strings with filter keys
* @param projects
* @returns Array of project names
*/
function filterProjects(projects) {
return Object.keys(projects).filter(project => indexOfAll(filter, project).length === 0);
}
/**
* Run builds async. in small batches
* @param projects
* @returns {{}}
*/
function batchProjects(projects) {
let currentBatch = 0,
i,
batches = {};
for (i = 0; i < projects.length; i += 1) {
if ((i) % batch === 0) {
currentBatch += 1;
}
if (typeof (batches['batch' + currentBatch]) === 'undefined') {
batches['batch' + currentBatch] = [];
}
batches['batch' + currentBatch].push(projects[i]);
}
return batches;
}
/**
* Main function to process angular projects to build
*/
fs.readFile('angular.json', 'utf8', async (err, data) => {
let batches = {}, batchesArray = [], i;
if (err) { throw err; }
let ngCli = JSON.parse(data);
batches = batchProjects(filterProjects(ngCli.projects));
batchesArray = Object.keys(batches);
for (i = 0; i < batchesArray.length; i += 1) {
let promises = [];
batches[batchesArray[i]].forEach((project) => {
promises.push(buildProject(project));
});
console.log('Building projects: ' + batches[batchesArray[i]].join(', '));
await Promise.all(promises).then(statusCode => {
console.log('Batch[' + i + ']' + ' Projects ' + batches[batchesArray[i]].join(', ') + ' built successfully!');
if (i + 1 === batchesArray.length) {
process.exit(0);
}
}, (reject, total) => {
console.log(reject);
process.exit(1);
});
}
});
@apolaskey
Copy link
Author

@apolaskey
Copy link
Author

node build-all-projects.js to kick things off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment