@nx/angular
Version:
79 lines (78 loc) • 3.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.angularInitGenerator = angularInitGenerator;
const devkit_1 = require("@nx/devkit");
const add_plugin_1 = require("@nx/devkit/src/utils/add-plugin");
const plugin_1 = require("../../plugins/plugin");
const validations_1 = require("../utils/validations");
const version_utils_1 = require("../utils/version-utils");
async function angularInitGenerator(tree, options) {
(0, validations_1.assertNotUsingTsSolutionSetup)(tree, 'init');
ignoreAngularCacheDirectory(tree);
const installTask = installAngularDevkitCoreIfMissing(tree, options);
// For Angular inference plugin, we only want it during import since our
// generators do not use `angular.json`, and `nx init` should split
// `angular.json` into multiple `project.json` files -- as this is preferred
// by most folks we've talked to.
options.addPlugin ??= process.env.NX_RUNNING_NX_IMPORT === 'true';
if (options.addPlugin) {
await (0, add_plugin_1.addPlugin)(tree, await (0, devkit_1.createProjectGraphAsync)(), '@nx/angular/plugin', plugin_1.createNodesV2, {
targetNamePrefix: ['', 'angular:', 'angular-'],
}, options.updatePackageScripts);
}
if (!options.skipFormat) {
await (0, devkit_1.formatFiles)(tree);
}
return installTask;
}
function installAngularDevkitCoreIfMissing(tree, options) {
const packageVersion = (0, devkit_1.getDependencyVersionFromPackageJson)(tree, '@angular-devkit/core');
if (!packageVersion) {
const pkgVersions = (0, version_utils_1.versions)(tree);
const devkitVersion = (0, version_utils_1.getInstalledAngularDevkitVersion)(tree) ??
pkgVersions.angularDevkitVersion;
try {
(0, devkit_1.ensurePackage)('@angular-devkit/core', devkitVersion);
}
catch {
// @schematics/angular cannot be required so this fails but this will still allow wrapping the schematic later on
}
if (!options.skipPackageJson) {
return (0, devkit_1.addDependenciesToPackageJson)(tree, {}, { ['@angular-devkit/core']: devkitVersion }, undefined, options.keepExistingVersions);
}
}
return () => { };
}
function ignoreAngularCacheDirectory(tree) {
const { cli } = (0, devkit_1.readNxJson)(tree);
// angular-specific cli config is supported though is not included in the
// NxJsonConfiguration type
const angularCacheDir = cli?.cache?.path ?? '.angular';
addGitIgnoreEntry(tree, angularCacheDir);
addPrettierIgnoreEntry(tree, angularCacheDir);
}
function addGitIgnoreEntry(tree, entry) {
if (tree.exists('.gitignore')) {
let content = tree.read('.gitignore', 'utf-8');
if (/^\.angular$/gm.test(content)) {
return;
}
content = `${content}\n${entry}\n`;
tree.write('.gitignore', content);
}
else {
devkit_1.logger.warn(`Couldn't find .gitignore file to update`);
}
}
function addPrettierIgnoreEntry(tree, entry) {
if (!tree.exists('.prettierignore')) {
return;
}
let content = tree.read('.prettierignore', 'utf-8');
if (/^\.angular(\/cache)?$/gm.test(content)) {
return;
}
content = `${content}\n${entry}\n`;
tree.write('.prettierignore', content);
}
exports.default = angularInitGenerator;