Franckeddy /
Pre-Site
| 1 | /** |
||
| 2 | * This file decorates the Angular CLI with the Nx CLI to enable features such as computation caching |
||
| 3 | * and faster execution of tasks. |
||
| 4 | * |
||
| 5 | * It does this by: |
||
| 6 | * |
||
| 7 | * - Patching the Angular CLI to warn you in case you accidentally use the undecorated ng command. |
||
| 8 | * - Symlinking the ng to nx command, so all commands run through the Nx CLI |
||
| 9 | * - Updating the package.json postinstall script to give you control over this script |
||
| 10 | * |
||
| 11 | * The Nx CLI decorates the Angular CLI, so the Nx CLI is fully compatible with it. |
||
| 12 | * Every command you run should work the same when using the Nx CLI, except faster. |
||
| 13 | * |
||
| 14 | * Because of symlinking you can still type `ng build/test/lint` in the terminal. The ng command, in this case, |
||
| 15 | * will point to nx, which will perform optimizations before invoking ng. So the Angular CLI is always invoked. |
||
| 16 | * The Nx CLI simply does some optimizations before invoking the Angular CLI. |
||
| 17 | * |
||
| 18 | * To opt out of this patch: |
||
| 19 | * - Replace occurrences of nx with ng in your package.json |
||
| 20 | * - Remove the script from your postinstall script in your package.json |
||
| 21 | * - Delete and reinstall your node_modules |
||
| 22 | */ |
||
| 23 | |||
| 24 | const fs = require('fs'); |
||
| 25 | const os = require('os'); |
||
| 26 | const cp = require('child_process'); |
||
| 27 | const isWindows = os.platform() === 'win32'; |
||
| 28 | let output; |
||
| 29 | try { |
||
| 30 | output = require('@nrwl/workspace').output; |
||
| 31 | } catch (e) { |
||
| 32 | console.warn('Angular CLI could not be decorated to enable computation caching. Please ensure @nrwl/workspace is installed.'); |
||
| 33 | process.exit(0); |
||
|
0 ignored issues
–
show
Compatibility
Debugging Code
Best Practice
introduced
by
Loading history...
|
|||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Symlink of ng to nx, so you can keep using `ng build/test/lint` and still |
||
| 38 | * invoke the Nx CLI and get the benefits of computation caching. |
||
| 39 | */ |
||
| 40 | function symlinkNgCLItoNxCLI() { |
||
| 41 | try { |
||
| 42 | const ngPath = './node_modules/.bin/ng'; |
||
| 43 | const nxPath = './node_modules/.bin/nx'; |
||
| 44 | if (isWindows) { |
||
| 45 | /** |
||
| 46 | * This is the most reliable way to create symlink-like behavior on Windows. |
||
| 47 | * Such that it works in all shells and works with npx. |
||
| 48 | */ |
||
| 49 | ['', '.cmd', '.ps1'].forEach(ext => { |
||
| 50 | if (fs.existsSync(nxPath + ext)) fs.writeFileSync(ngPath + ext, fs.readFileSync(nxPath + ext)); |
||
|
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. Loading history...
|
|||
| 51 | }); |
||
| 52 | } else { |
||
| 53 | // If unix-based, symlink |
||
| 54 | cp.execSync(`ln -sf ./nx ${ngPath}`); |
||
| 55 | } |
||
| 56 | } |
||
| 57 | catch(e) { |
||
| 58 | output.error({ title: 'Unable to create a symlink from the Angular CLI to the Nx CLI:' + e.message }); |
||
| 59 | throw e; |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | try { |
||
| 64 | symlinkNgCLItoNxCLI(); |
||
| 65 | require('nx/src/adapter/decorate-cli').decorateCli(); |
||
| 66 | output.log({ title: 'Angular CLI has been decorated to enable computation caching.' }); |
||
| 67 | } catch(e) { |
||
| 68 | output.error({ title: 'Decoration of the Angular CLI did not complete successfully' }); |
||
| 69 | } |
||
| 70 |