-
Notifications
You must be signed in to change notification settings - Fork 0
/
nx.js
executable file
·354 lines (329 loc) · 11.8 KB
/
nx.js
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
const fs = require("fs");
const path = require("path");
const yargsParser = require("yargs-parser");
const net = require("net");
const { existsSync } = require('fs-extra');
const { getFileHashes, getIgnoredGlobs } = require("./core/git-hash");
const { createProjectFileMap, updateProjectFileMap } = require("./core/file-map-utils");
const { ProjectGraphBuilder } = require("./core/project-graph-builder");
const { buildWorkspaceProjectNodes } = require("./core/workspace-projects");
const { resolveNewFormatWithInlineProjects } = require("./core/workspace");
const { buildNpmPackageNodes } = require("./core/npm-packages");
const { buildExplicitDependencies, jsPluginConfig } = require("./core/build-project-graph");
const { buildImplicitProjectDependencies } = require("./core/implicit-project-dependencies");
const { createCache, writeCache } = require("./core/nx-deps-cache");
const { projectGraphAdapter } = require('./core/project-graph');
const { getProjects } = require('./core/run-one');
const { runCommand } = require('./core/run-command');
function findWorkspaceRoot(dir) {
if (fs.existsSync(path.join(dir, 'angular.json'))) {
return { type: 'angular', dir };
}
if (fs.existsSync(path.join(dir, 'nx.json'))) {
return { type: 'nx', dir };
}
if (path.dirname(dir) === dir) {
return null;
}
return findWorkspaceRoot(path.dirname(dir));
}
function readJsonFile(path) {
const content = fs.readFileSync(path, 'utf-8');
return JSON.parse(content);
}
function normalizeNxJson(nxJson, projects) {
return nxJson.implicitDependencies
? Object.assign(Object.assign({}, nxJson), {
implicitDependencies: Object.entries(nxJson.implicitDependencies).reduce((acc, [key, val]) => {
acc[key] = recur(projects, val);
return acc;
}, {})
}) : nxJson;
}
function recur(projects, v) {
if (v === '*') {
return projects;
}
else if (Array.isArray(v)) {
return v;
}
else {
return Object.keys(v).reduce((acc, key) => {
acc[key] = recur(projects, v[key]);
return acc;
}, {});
}
}
function readCombinedDeps(appRootPath) {
const json = readJsonFile(path.join(appRootPath, 'package.json'));
return { ...json.dependencies, ...json.devDependencies };
}
function readRootTsConfig(appRootPath) {
for (const tsConfigName of ['tsconfig.base.json', 'tsconfig.json']) {
const tsConfigPath = path.join(appRootPath, tsConfigName);
if (existsSync(tsConfigPath)) {
return readJsonFile(tsConfigPath);
}
}
}
function createContext(
workspaceJson,
nxJson,
fileMap,
filesToProcess
) {
const projects = Object.keys(workspaceJson.projects).reduce((map, projectName) => {
map[projectName] = {
...workspaceJson.projects[projectName],
};
return map;
}, {});
return {
workspace: {
...workspaceJson,
...nxJson,
projects,
},
fileMap,
filesToProcess,
};
}
const runOne = [
'target',
'configuration',
'prod',
'runner',
'parallel',
'max-parallel',
'exclude',
'only-failed',
'help',
'with-deps',
'skip-nx-cache',
'scan',
'hide-cached-output',
];
const runMany = [...runOne, 'projects', 'all'];
const runAffected = [
...runOne,
'untracked',
'uncommitted',
'all',
'base',
'head',
'files',
'plain',
'select',
];
const ignoreArgs = ['$0', '_'];
function splitArgsIntoNxArgsAndOverrides(args, mode, options = { printWarnings: true }) {
console.log(args, mode, options);
const nxSpecific = mode === 'run-one' ? runOne : mode === 'run-many' ? runMany : runAffected;
const nxArgs = {};
const overrides = yargsParser(args._, {
configuration: {
'strip-dashed': true,
'dot-notation': false,
},
});
args._ = overrides._;
delete overrides._;
Object.entries(args).forEach(([key, value]) => {
if (nxSpecific.includes(key) || key.startsWith('nx-')) {
if (value !== undefined)
nxArgs[key] = value;
}
else if (!ignoreArgs.includes(key)) {
overrides[key] = value;
}
});
if (!nxArgs.skipNxCache) {
nxArgs.skipNxCache = process.env.NX_SKIP_NX_CACHE === 'true';
}
return { nxArgs, overrides };
}
// 读取本地 nx.json 和 workspace.json 配置文件
const workspace = findWorkspaceRoot(process.cwd());
const nxJsonPath = path.join(workspace.dir, 'nx.json');
const workspaceJsonPath = path.join(workspace.dir, 'workspace.json');
const nxJson = readJsonFile(nxJsonPath);
const nxDepsJsonPath = path.join(workspace.dir, 'nx-deps.json');
const nxDepsJson = fs.existsSync(nxDepsJsonPath) ? readJsonFile(nxDepsJsonPath) : null;
// 转换 workspaceJson 的数据
let workspaceJson = readJsonFile(workspaceJsonPath);
// 本质 workspaceJson + project.json
// 读取 workspaceJson 里所有子项目的 project.json 配置,并合并到 workspaceJson 的 projects 属性里面
workspaceJson = resolveNewFormatWithInlineProjects(workspaceJson, workspace.dir);
// 本质 workspaceJson + nx.json
// readWorkspaceConfiguration,将 workspaceJson 和 nx.json 合并
// 这里省略检测 nx.json 配置的合法性
workspaceJson = Object.assign(Object.assign({}, workspaceJson), nxJson);
// 这里会有个 parseRunOneOptions 来过滤参数
const args = process.argv.slice(2);
const parsedArgs = yargsParser(args, {
boolean: ['prod', 'help'],
string: ['configuration', 'project'],
alias: {
c: 'configuration',
},
configuration: {
'strip-dashed': true,
},
});
let project;
let target;
let configuration;
if (parsedArgs._[0] === 'run') {
[project, target, configuration] = parsedArgs._[1].split(':');
parsedArgs._ = parsedArgs._.slice(2);
}
const runOpts = { project, target, configuration, parsedArgs };
delete parsedArgs['c'];
delete parsedArgs['configuration'];
delete parsedArgs['prod'];
delete parsedArgs['project'];
// nx 支持的命令
const supportedNxCommands = [
'affected',
'affected:apps',
'affected:libs',
'affected:build',
'affected:test',
'affected:e2e',
'affected:dep-graph',
'affected:graph',
'affected:lint',
'print-affected',
'daemon',
'dep-graph',
'graph',
'format',
'format:check',
'format:write',
'workspace-schematic',
'workspace-generator',
'workspace-lint',
'migrate',
'report',
'run-many',
'connect-to-nx-cloud',
'clear-cache',
'reset',
'list',
'help',
'--help',
'--version',
];
// 判断是否用内置命令
const running = runOpts !== false;
if (supportedNxCommands.includes(process.argv[2])) {
}
// 判断是否运行一条命令,例如 nx run xxx
else if (running) {
(async () => {
// 将 parsedArgs (执行命令参数)拆分为 Nx 参数并覆盖,最终所有参数组合成 nxArgs
const { nxArgs, overrides } = splitArgsIntoNxArgsAndOverrides(Object.assign(Object.assign({}, runOpts.parsedArgs), { configuration: runOpts.configuration, target: runOpts.target }), 'run-one');
// 使用 git 命令查看变更的文件
const gitResult = await getFileHashes(workspace.dir);
const ignore = getIgnoredGlobs(workspace.dir);
const fileHashes = new Map();
gitResult.allFiles.forEach((hash, filename) => {
if (!ignore.ignores(filename)) {
fileHashes.set(filename, hash);
}
});
const allFileData = (() => {
const res = [];
fileHashes.forEach((hash, file) => {
res.push({
file,
hash,
});
});
res.sort((x, y) => x.file.localeCompare(y.file));
return res;
})()
// createProjectFileMap
const projectGraphVersion = '5.0';
const { projectFileMap, allWorkspaceFiles } = createProjectFileMap(workspaceJson, allFileData);
// 缓存是否启用
const cacheEnabled = process.env.NX_CACHE_PROJECT_GRAPH !== 'false';
// 判断是否存在 nx_deps.json,如果层级执行过这个文件会被存储起来当缓存,并读取 nx_deps.json 缓存文件
let cache = nxDepsJson;
const normalizedNxJson = normalizeNxJson(nxJson, Object.keys(workspaceJson.projects));
const packageJsonDeps = readCombinedDeps(workspace.dir);
const rootTsConfig = readRootTsConfig(workspace.dir);
let filesToProcess;
let cachedFileData;
if (cache) {
} else {
filesToProcess = projectFileMap;
cachedFileData = {};
}
const context = createContext(workspaceJson, normalizedNxJson, projectFileMap, filesToProcess);
// buildProjectGraphUsingContext 使用作用域构建 projectGraph
const builder = new ProjectGraphBuilder();
// 创建完 builder 之后,其实最关键是 getUpdatedProjectGraph
// 这里会把 project.json 和 package.json 的命令做合并
// package.json 会默认使用 @nrwl/workspace:run-script 作为 executor 执行器
buildWorkspaceProjectNodes(context, builder, workspace.dir);
buildNpmPackageNodes(builder, workspace.dir);
for (const proj of Object.keys(cachedFileData)) {
for (const f of builder.graph.nodes[proj].data.files) {
const cached = cachedFileData[proj][f.file];
if (cached && cached.deps) {
f.deps = [...cached.deps];
}
}
}
// 分析 ts 代码的 import 依赖
await buildExplicitDependencies(jsPluginConfig(nxJson), context, builder);
buildImplicitProjectDependencies(context, builder);
builder.setVersion(projectGraphVersion);
// 创建 projectGraph
let projectGraph = builder.getUpdatedProjectGraph();
// 缓存 projectGraph
const projectGraphCache = createCache(nxJson, packageJsonDeps, projectGraph, rootTsConfig);
// 写入 nxdeps.json 缓存文件到 .cache 文件夹
if (cacheEnabled) {
writeCache(projectGraphCache);
}
// projectGraph 项目图的向后兼容性适配器
// 这里如果是使用 5 的 nx 版本,但是是 4 版本的写法,会使用 projectGraphCompat5to4 函数转换一次
projectGraph = projectGraphAdapter('5.0', projectGraphVersion, projectGraph);
// 根据项目图配合命令 npm run xxx:xxx 参数寻找出 project.json 中对应的命令
const { projects, projectsMap } = getProjects(projectGraph, runOpts.project);
// 读取 nxJson 和 workspaceJson 作为环境
const env = { nxJson, workspaceJson, workspaceResults: null };
await runCommand(
projects,
projectGraph,
env,
nxArgs,
overrides,
'run-one',
runOpts.project
);
// const socket = net.connect('./d.sock');
// socket.on('error', (err) => {
// console.log('socekt error', err);
// });
// socket.on('connect', () => {
// socket.write('REQUEST_PROJECT_GRAPH_PAYLOAD');
// let serializedProjectGraphResult = '';
// socket.on('data', (data) => {
// serializedProjectGraphResult += data.toString();
// });
// socket.on('end', () => {
// try {
// const projectGraphResult = JSON.parse(serializedProjectGraphResult);
// console.log('projectGraphResult', projectGraphResult);
// }
// catch (e) {
// console.log('connect error', e);
// }
// });
// });
console.log('end');
})()
}