From a55e7cae52d1fe2a30cc118096b0617c01b8d9a9 Mon Sep 17 00:00:00 2001 From: Victor Savkin Date: Wed, 18 Nov 2020 16:02:08 -0500 Subject: [PATCH] feat(angular): change the way we decoreate angular cli to remove warnigs --- packages/cli/lib/decorate-cli.ts | 19 +++++++++++ packages/workspace/migrations.json | 5 +++ .../update-decorate-angular-cli.ts | 21 ++++++++++++ .../utils/decorate-angular-cli.js__tmpl__ | 32 +------------------ 4 files changed, 46 insertions(+), 31 deletions(-) create mode 100644 packages/cli/lib/decorate-cli.ts create mode 100644 packages/workspace/src/migrations/update-11-0-0/update-decorate-angular-cli.ts diff --git a/packages/cli/lib/decorate-cli.ts b/packages/cli/lib/decorate-cli.ts new file mode 100644 index 0000000000..42cd339804 --- /dev/null +++ b/packages/cli/lib/decorate-cli.ts @@ -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); +} diff --git a/packages/workspace/migrations.json b/packages/workspace/migrations.json index 4f9ee2e38b..19b71270ff 100644 --- a/packages/workspace/migrations.json +++ b/packages/workspace/migrations.json @@ -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": { diff --git a/packages/workspace/src/migrations/update-11-0-0/update-decorate-angular-cli.ts b/packages/workspace/src/migrations/update-11-0-0/update-decorate-angular-cli.ts new file mode 100644 index 0000000000..76936731b1 --- /dev/null +++ b/packages/workspace/src/migrations/update-11-0-0/update-decorate-angular-cli.ts @@ -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); + } + }; +} diff --git a/packages/workspace/src/schematics/utils/decorate-angular-cli.js__tmpl__ b/packages/workspace/src/schematics/utils/decorate-angular-cli.js__tmpl__ index 228d5167d0..bc81a83788 100644 --- a/packages/workspace/src/schematics/utils/decorate-angular-cli.js__tmpl__ +++ b/packages/workspace/src/schematics/utils/decorate-angular-cli.js__tmpl__ @@ -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' });