I created a script that, when placed in the same folder as angular.json
, will pull in the file, loop over the projects, and build them in batches asynchronously.
Here's a quick gist, you can toggle the output path and the number of asynchronous builds. I've excluded e2e for the moment, but you can remove the reference to the filteredProjects
function, and it will run for e2e as projects as well. It would also be easy to add this to package.json
as an npm run script. So far, it has been working well.
https://gist.github.com/bmarti44/f6b8d3d7b331cd79305ca8f45eb8997b
const fs = require('fs'),
spawn = require('child_process').spawn,
// Custom output path.
outputPath = '/nba-angular',
// Number of projects to build asynchronously.
batch = 3;
let ngCli;
function buildProject(project) {
return new Promise((resolve, reject) => {
let child = spawn('ng', ['build', '--project', project, '--prod', '--extract-licenses', '--build-optimizer', `--output-path=${outputPath}/dist/` + project]);
child.stdout.on('data', (data) => {
console.log(data.toString());
});
child.stderr.on('data', (data) => {
process.stdout.write('.');
});
child.on('close', (code) => {
if (code === 0) {
resolve(code);
} else {
reject(code);
}
});
})
}
function filterProjects(projects) {
return Object.keys(projects).filter(project => project.indexOf('e2e') === -1);
}
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;
}
fs.readFile('angular.json', 'utf8', async (err, data) => {
let batches = {},
batchesArray = [],
i;
if (err) {
throw err;
}
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('Projects ' + batches[batchesArray[i]].join(',') + ' built successfully!');
if (i + 1 === batchesArray.length) {
process.exit(0);
}
}, (reject) => {
console.log(reject);
process.exit(1);
});
}
});