Description
Feature requested
Add the --all
(or --projects
) option for the ng build
schematic. This option would build libraries, then applications, then the default project; because of how they are dependent of each other. It should ignore the e2e projects.
To make it easier to find, and order which project should be built first, it would be better to change the generate schematics of application
and library
to change the angular.json file adding libraries
and applications
as attributes holding an array of strings.
{
...
"libraries": ["foo-lib", "bar-lib"],
"applications": ["example-foo"],
"defaultProject": "project"
}
Case of use
Building several libraries and applications in a monorepo that all packages/projects have the same semantic version.
When working with several libraries and applications on a monorepo, it is a pain to create a npm script for each project, and update the build:all
npm script. Running a single command to build all projects is less painfull and has less chance of mistakes (forgetting, skipping).
Example / attempt
As much as this example works for me, I still can't order the libraries and applications. If I have a library that needs another, and it builds first, it would break.
const fs = require('fs');
const { exec } = require('child_process');
const projectTypes = {
library: 'libraries',
application: 'applications'
};
function readAngularJson() {
try {
const data = fs.readFileSync('./angular.json', 'utf8');
return JSON.parse(data);
} catch (err) {
throw err;
}
}
function generateBuildProjects() {
const angularJson = readAngularJson();
const projects = angularJson.projects;
const projectsKeys = Object.keys(projects).filter((key) => {
return key.indexOf('e2e') < 0 && key !== angularJson.defaultProject;
});
const buildProjects = {
libraries: [],
applications: [],
defaultProject: angularJson.defaultProject
};
projectsKeys.forEach((pk) => {
const project = projects[pk];
const projectType = projectTypes[project.projectType];
buildProjects[projectType].push(pk);
});
return buildProjects;
}
function buildAll() {
const bp = generateBuildProjects();
const projects = [
...bp.libraries,
...bp.applications,
bp.defaultProject
];
build(projects);
}
function build(projects) {
const project = projects.shift();
console.log('Started building', project);
const child = exec(`ng build ${project} --prod`, (err, stdout, stderr) => {
if (stdout) console.log(stdout);
if (stderr) console.log(stderr);
});
child.on('close', () => {
if (projects.length) build(projects);
else console.log('finished');
});
}
buildAll();