feat(angular): change the way we decoreate angular cli to remove warnigs

This commit is contained in:
Victor Savkin 2020-11-18 16:02:08 -05:00 committed by Victor Savkin
parent 7d3627ab27
commit a55e7cae52
4 changed files with 46 additions and 31 deletions

View File

@ -0,0 +1,19 @@
import { readFileSync, writeFileSync } from 'fs';
export function decorateCli() {
const path = 'node_modules/@angular/cli/lib/cli/index.js';
const angularCLIInit = readFileSync(path, 'utf-8').toString();
const start = angularCLIInit.indexOf(`(options) {`) + 11;
const end = angularCLIInit.lastIndexOf(`}`) - 2;
const newContent = `${angularCLIInit.substr(0, start)}
if (!process.env['NX_CLI_SET']) {
require('@nrwl/cli/bin/nx');
return new Promise(function(res, rej) {});
} else {
${angularCLIInit.substring(start, end)}
}
${angularCLIInit.substring(end)}
`;
writeFileSync(path, newContent);
}

View File

@ -134,6 +134,11 @@
"version": "10.4.0-beta.5",
"description": "Add an explicit dependency on @nrwl/tao",
"factory": "./src/migrations/update-10-4-0/add-explicit-dep-on-tao"
},
"update-decorate-angular-cli": {
"version": "11.0.0-beta.3",
"description": "Update the decoration script when using Angular CLI",
"factory": "./src/migrations/update-11-0-0/update-decorate-angular-cli"
}
},
"packageJsonUpdates": {

View File

@ -0,0 +1,21 @@
import { Rule, Tree } from '@angular-devkit/schematics';
import { join as pathJoin } from 'path';
import { readFileSync } from 'fs';
export default function update(): Rule {
return (host: Tree) => {
const decorateCli = readFileSync(
pathJoin(
__dirname as any,
'..',
'..',
'schematics',
'utils',
'decorate-angular-cli.js__tmpl__'
)
).toString();
if (host.exists('/decorate-angular-cli.js')) {
host.overwrite('/decorate-angular-cli.js', decorateCli);
}
};
}

View File

@ -33,36 +33,6 @@ try {
process.exit(0);
}
/**
* Paths to files being patched
*/
const angularCLIInitPath = 'node_modules/@angular/cli/lib/cli/index.js';
/**
* Patch index.js to warn you if you invoke the undecorated Angular CLI.
*/
function patchAngularCLI(initPath) {
const angularCLIInitContent = fs.readFileSync(initPath, 'utf-8').toString();
if (!angularCLIInitContent.includes('NX_CLI_SET')) {
fs.writeFileSync(initPath, `
// this env variable is set when invoked from within Nx
if (!process.env['NX_CLI_SET']) {
const { useNxToRunNxBuilderOrSchematic } = require('@nrwl/cli/lib/use-nx-to-run-nx-builder-or-schematic');
const { output } = require('@nrwl/workspace');
if (useNxToRunNxBuilderOrSchematic()) {
output.warn({ title: 'The command will use Nx CLI instead of Angular CLI because it uses an Nx builder. Read more at https://nx.dev/latest/angular/cli/overview' });
require('@nrwl/tao/index.js');
} else {
output.warn({ title: 'The Angular CLI was invoked instead of the Nx CLI. Use "npx ng [command]" or "nx [command]" instead. Read more at https://nx.dev/latest/angular/cli/overview' });
}
}
${angularCLIInitContent}
`);
}
}
/**
* Symlink of ng to nx, so you can keep using `ng build/test/lint` and still
* invoke the Nx CLI and get the benefits of computation caching.
@ -92,7 +62,7 @@ function symlinkNgCLItoNxCLI() {
try {
symlinkNgCLItoNxCLI();
patchAngularCLI(angularCLIInitPath);
require('@nrwl/cli/lib/decorate-cli').decorateCli();
output.log({ title: 'Angular CLI has been decorated to enable computation caching.' });
} catch(e) {
output.error({ title: 'Decoration of the Angular CLI did not complete successfully' });