Repository navigation
Expand file tree
/
Copy pathinit-local.ts
More file actions
207 lines (191 loc) · 6.23 KB
/
Copy pathinit-local.ts
File metadata and controls
207 lines (191 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { performance } from 'perf_hooks';
import { commandsObject } from '../src/command-line/nx-commands';
import { WorkspaceTypeAndRoot } from '../src/utils/find-workspace-root';
import { stripIndents } from '../src/utils/strip-indents';
import * as Mod from 'module';
/**
* Nx is being run inside a workspace.
*
* @param workspace Relevant local workspace properties
*/
export function initLocal(workspace: WorkspaceTypeAndRoot) {
process.env.NX_CLI_SET = 'true';
try {
performance.mark('init-local');
monkeyPatchRequire();
if (workspace.type !== 'nx' && shouldDelegateToAngularCLI()) {
console.warn(
stripIndents`Using Nx to run Angular CLI commands is deprecated and will be removed in a future version.
To run Angular CLI commands, use \`ng\`.`
);
handleAngularCLIFallbacks(workspace);
return;
}
const command = process.argv[2];
if (command === 'run' || command === 'g' || command === 'generate') {
commandsObject.parse(process.argv.slice(2));
} else if (isKnownCommand(command)) {
const newArgs = rewriteTargetsAndProjects(process.argv);
const help = newArgs.indexOf('--help');
const split = newArgs.indexOf('--');
if (help > -1 && (split === -1 || split > help)) {
commandsObject.showHelp();
} else {
commandsObject.parse(newArgs);
}
} else {
commandsObject.parse(process.argv.slice(2));
}
} catch (e) {
console.error(e.message);
process.exit(1);
}
}
export function rewriteTargetsAndProjects(args: string[]) {
const newArgs = [args[2]];
let i = 3;
while (i < args.length) {
if (args[i] === '--') {
return [...newArgs, ...args.slice(i)];
} else if (
args[i] === '-p' ||
args[i] === '--projects' ||
args[i] === '--exclude' ||
args[i] === '--files' ||
args[i] === '-t' ||
args[i] === '--target' ||
args[i] === '--targets'
) {
newArgs.push(args[i]);
i++;
const items = [];
while (i < args.length && !args[i].startsWith('-')) {
items.push(args[i]);
i++;
}
newArgs.push(items.join(','));
} else {
newArgs.push(args[i]);
++i;
}
}
return newArgs;
}
function wrapIntoQuotesIfNeeded(arg: string) {
return arg.indexOf(':') > -1 ? `"${arg}"` : arg;
}
function isKnownCommand(command: string) {
const commands = [
...Object.keys(
(commandsObject as any)
.getInternalMethods()
.getCommandInstance()
.getCommandHandlers()
),
'g',
'dep-graph',
'affected:dep-graph',
'format',
'workspace-schematic',
'connect-to-nx-cloud',
'clear-cache',
'help',
];
return !command || command.startsWith('-') || commands.indexOf(command) > -1;
}
function shouldDelegateToAngularCLI() {
const command = process.argv[2];
const commands = [
'analytics',
'cache',
'completion',
'config',
'doc',
'update',
];
return commands.indexOf(command) > -1;
}
function handleAngularCLIFallbacks(workspace: WorkspaceTypeAndRoot) {
if (process.argv[2] === 'update' && process.env.FORCE_NG_UPDATE != 'true') {
console.log(
`Nx provides a much improved version of "ng update". It runs the same migrations, but allows you to:`
);
console.log(`- rerun the same migration multiple times`);
console.log(`- reorder migrations, skip migrations`);
console.log(`- fix migrations that "almost work"`);
console.log(`- commit a partially migrated state`);
console.log(
`- change versions of packages to match organizational requirements`
);
console.log(
`And, in general, it is lot more reliable for non-trivial workspaces. Read more at: https://nx.dev/getting-started/nx-and-angular#ng-update-and-nx-migrate`
);
console.log(
`Run "nx migrate latest" to update to the latest version of Nx.`
);
console.log(
`Running "ng update" can still be useful in some dev workflows, so we aren't planning to remove it.`
);
console.log(`If you need to use it, run "FORCE_NG_UPDATE=true ng update".`);
} else if (process.argv[2] === 'completion') {
if (!process.argv[3]) {
console.log(`"ng completion" is not natively supported by Nx.
Instead, you could try an Nx Editor Plugin for a visual tool to run Nx commands. If you're using VSCode, you can use the Nx Console p
6AD9
lugin, or if you're using WebStorm, you could use one of the available community plugins.
For more information, see https://nx.dev/features/integrate-with-editors`);
}
} else if (process.argv[2] === 'cache') {
console.log(`"ng cache" is not natively supported by Nx.
To clear the cache, you can delete the ".angular/cache" directory (or the directory configured by "cli.cache.path" in the "nx.json" file).
To update the cache configuration, you can directly update the relevant options in your "nx.json" file (https://angular.io/guide/workspace-config#cache-options).`);
} else {
try {
// nx-ignore-next-line
const cli = require.resolve('@angular/cli/lib/init.js', {
paths: [workspace.dir],
});
require(cli);
} catch (e) {
console.error(
`Could not find '@angular/cli/lib/init.js' module in this workspace.`,
e
);
process.exit(1);
}
}
}
// TODO(v17): Remove this once the @nrwl/* packages are not
function monkeyPatchRequire() {
const originalRequire = Mod.prototype.require;
(Mod.prototype.require as any) = function (...args) {
const modulePath = args[0];
if (!modulePath.startsWith('@nrwl/')) {
return originalRequire.apply(this, args);
} else {
try {
// Try the original require
return originalRequire.apply(this, args);
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
try {
// Retry the require with the @nx package
return originalRequire.apply(
this,
args.map((value, i) => {
if (i !== 0) {
return value;
} else {
return value.replace('@nrwl/', '@nx/');
}
})
);
} catch {
// Throw the original error
throw e;
}
}
}
// do some side-effect of your own
};
}