Skip to content

Commit

Permalink
update nx to 10.4.5 (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored Dec 10, 2020
1 parent e43d760 commit bb6e6d6
Show file tree
Hide file tree
Showing 3 changed files with 350 additions and 253 deletions.
112 changes: 112 additions & 0 deletions decorate-angular-cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* This file decorates the Angular CLI with the Nx CLI to enable features such as computation caching
* and faster execution of tasks.
*
* It does this by:
*
* - Patching the Angular CLI to warn you in case you accidentally use the undecorated ng command.
* - Symlinking the ng to nx command, so all commands run through the Nx CLI
* - Updating the package.json postinstall script to give you control over this script
*
* The Nx CLI decorates the Angular CLI, so the Nx CLI is fully compatible with it.
* Every command you run should work the same when using the Nx CLI, except faster.
*
* Because of symlinking you can still type `ng build/test/lint` in the terminal. The ng command, in this case,
* will point to nx, which will perform optimizations before invoking ng. So the Angular CLI is always invoked.
* The Nx CLI simply does some optimizations before invoking the Angular CLI.
*
* To opt out of this patch:
* - Replace occurrences of nx with ng in your package.json
* - Remove the script from your postinstall script in your package.json
* - Delete and reinstall your node_modules
*/

const fs = require('fs');
const os = require('os');
const cp = require('child_process');
const isWindows = os.platform() === 'win32';
let output;
try {
output = require('@nrwl/workspace').output;
} catch (e) {
console.warn(
'Angular CLI could not be decorated to enable computation caching. Please ensure @nrwl/workspace is installed.'
);
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 angularCLIInit = fs.readFileSync(initPath, 'utf-8').toString();

if (!angularCLIInit.includes('NX_CLI_SET')) {
fs.writeFileSync(
initPath,
`
if (!process.env['NX_CLI_SET']) {
const { output } = require('@nrwl/workspace');
output.warn({ title: 'The Angular CLI was invoked instead of the Nx CLI. Use "npx ng [command]" or "nx [command]" instead.' });
}
if (process.argv[2] === 'update') {
const { output } = require('@nrwl/workspace');
output.error({
title: '"ng update" is deprecated in favor of "nx migrate". Read more: https://nx.dev/latest/angular/workspace/update'
});
throw new Error();
}
${angularCLIInit}
`
);
}
}

/**
* 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.
*/
function symlinkNgCLItoNxCLI() {
try {
const ngPath = './node_modules/.bin/ng';
const nxPath = './node_modules/.bin/nx';
if (isWindows) {
/**
* This is the most reliable way to create symlink-like behavior on Windows.
* Such that it works in all shells and works with npx.
*/
['', '.cmd', '.ps1'].forEach((ext) => {
if (fs.existsSync(nxPath + ext))
fs.writeFileSync(ngPath + ext, fs.readFileSync(nxPath + ext));
});
} else {
// If unix-based, symlink
cp.execSync(`ln -sf ./nx ${ngPath}`);
}
} catch (e) {
output.error({
title:
'Unable to create a symlink from the Angular CLI to the Nx CLI:' +
e.message,
});
throw e;
}
}

try {
symlinkNgCLItoNxCLI();
patchAngularCLI(angularCLIInitPath);
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',
});
}
35 changes: 18 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
"format": "nx format:write",
"format:write": "nx format:write",
"format:check": "nx format:check",
"update": "ng update @nrwl/workspace",
"update": "nx migrate latest",
"update:check": "ng update",
"workspace-schematic": "nx workspace-schematic",
"dep-graph": "nx dep-graph",
"help": "nx help",
"nx": "nx",
"postinstall": "ngcc --properties es2015 browser module main --first-only --create-ivy-entry-points"
"postinstall": "ngcc --properties es2015 browser module main --first-only --create-ivy-entry-points && node decorate-angular-cli.js"
},
"private": true,
"dependencies": {
Expand All @@ -45,12 +45,12 @@
"@ngrx/entity": "10.0.0",
"@ngrx/router-store": "10.0.0",
"@ngrx/store": "10.0.0",
"@nrwl/angular": "10.3.1-beta.1",
"@nrwl/angular": "10.4.7",
"core-js": "^2.5.4",
"document-register-element": "1.13.1",
"normalize.css": "^8.0.1",
"react": "16.13.1",
"react-dom": "16.13.1",
"react": "17.0.1",
"react-dom": "17.0.1",
"react-router-dom": "5.2.0",
"rxjs": "~6.5.5",
"tslib": "^2.0.0",
Expand All @@ -63,19 +63,20 @@
"@angular/language-service": "10.1.4",
"@babel/preset-react": "7.0.0",
"@ngrx/store-devtools": "10.0.0",
"@nrwl/cli": "10.3.1-beta.1",
"@nrwl/cypress": "10.3.1-beta.1",
"@nrwl/jest": "10.3.1-beta.1",
"@nrwl/nx-cloud": "10.1.6",
"@nrwl/react": "10.3.1-beta.1",
"@nrwl/web": "10.3.1-beta.1",
"@nrwl/workspace": "10.3.1-beta.1",
"@testing-library/react": "10.4.1",
"@nrwl/cli": "10.4.7",
"@nrwl/cypress": "10.4.7",
"@nrwl/jest": "10.4.7",
"@nrwl/nx-cloud": "10.1.9",
"@nrwl/react": "10.4.7",
"@nrwl/tao": "10.4.7",
"@nrwl/web": "10.4.7",
"@nrwl/workspace": "10.4.7",
"@testing-library/react": "11.1.2",
"@types/jest": "26.0.8",
"@types/node": "^12.11.1",
"@types/react": "16.9.38",
"@types/react-dom": "16.9.8",
"@types/react-router-dom": "5.1.5",
"@types/react": "16.9.56",
"@types/react-dom": "16.9.9",
"@types/react-router-dom": "5.1.6",
"codelyzer": "^6.0.0",
"cypress": "^3.8.2",
"dotenv": "6.2.0",
Expand All @@ -86,7 +87,7 @@
"jest-environment-jsdom-fourteen": "^0.1.0",
"jest-preset-angular": "8.3.1",
"netlify": "^2.4.8",
"prettier": "2.0.5",
"prettier": "2.1.2",
"ts-jest": "26.4.0",
"ts-node": "~7.0.0",
"tslint": "~6.1.0",
Expand Down
Loading

0 comments on commit bb6e6d6

Please sign in to comment.