-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
run-one.ts
162 lines (145 loc) · 4.46 KB
/
run-one.ts
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
import { runCommand } from '../tasks-runner/run-command';
import { splitArgsIntoNxArgsAndOverrides } from '../utils/command-line-utils';
import { connectToNxCloudUsingScan } from './connect-to-nx-cloud';
import { performance } from 'perf_hooks';
import { createProjectGraphAsync } from '../project-graph/project-graph';
import { ProjectGraph } from '../config/project-graph';
import { NxJsonConfiguration } from '../config/nx-json';
import { workspaceRoot } from '../utils/app-root';
import { splitTarget } from '../utils/split-target';
import { output } from '../utils/output';
import { readEnvironment } from './read-environment';
import { WorkspaceJsonConfiguration } from '../config/workspace-json-project-json';
export async function runOne(
cwd: string,
args: { [k: string]: any }
): Promise<void> {
performance.mark('command-execution-begins');
performance.measure('code-loading', 'init-local', 'command-execution-begins');
const env = readEnvironment();
const opts = parseRunOneOptions(cwd, args, env.workspaceJson);
const { nxArgs, overrides } = splitArgsIntoNxArgsAndOverrides(
{
...opts.parsedArgs,
configuration: opts.configuration,
target: opts.target,
},
'run-one'
);
if (nxArgs.help) {
await (
await import('./run')
).run(cwd, workspaceRoot, opts, {}, false, true);
process.exit(0);
}
const projectGraph = await createProjectGraphAsync();
await connectToNxCloudUsingScan(nxArgs.scan);
const { projects } = getProjects(projectGraph, opts.project);
await runCommand(
projects,
projectGraph,
env,
nxArgs,
overrides,
opts.project
);
}
function getProjects(projectGraph: ProjectGraph, project: string): any {
if (!projectGraph.nodes[project]) {
output.error({
title: `Cannot find project '${project}'`,
});
process.exit(1);
}
let projects = [projectGraph.nodes[project]];
let projectsMap = {
[project]: projectGraph.nodes[project],
};
return { projects, projectsMap };
}
const targetAliases = {
b: 'build',
e: 'e2e',
'i18n-extract': 'extract-i18n',
xi18n: 'extract-i18n',
l: 'lint',
s: 'serve',
t: 'test',
};
function parseRunOneOptions(
cwd: string,
parsedArgs: { [k: string]: any },
workspaceConfiguration: WorkspaceJsonConfiguration & NxJsonConfiguration
): { project; target; configuration; parsedArgs } {
const defaultProjectName = calculateDefaultProjectName(
cwd,
workspaceRoot,
workspaceConfiguration
);
let project;
let target;
let configuration;
if (parsedArgs['project:target:configuration'].indexOf(':') > -1) {
// run case
[project, target, configuration] = splitTarget(
parsedArgs['project:target:configuration']
);
// this is to account for "nx npmsript:dev"
if (project && !target && defaultProjectName) {
project = defaultProjectName;
target = project;
}
} else {
target = parsedArgs['project:target:configuration'];
}
if (parsedArgs.project) {
project = parsedArgs.project;
}
if (!project && defaultProjectName) {
project = defaultProjectName;
}
if (!project || !target) {
throw new Error(`Both project and target to have to be specified`);
}
if (targetAliases[target]) {
target = targetAliases[target];
}
if (parsedArgs.configuration) {
configuration = parsedArgs.configuration;
} else if (parsedArgs.prod) {
configuration = 'production';
}
const res = { project, target, configuration, parsedArgs };
delete parsedArgs['c'];
delete parsedArgs['project:target:configuration'];
delete parsedArgs['configuration'];
delete parsedArgs['prod'];
delete parsedArgs['project'];
return res;
}
function calculateDefaultProjectName(
cwd: string,
root: string,
workspaceConfiguration: WorkspaceJsonConfiguration & NxJsonConfiguration
) {
let relativeCwd = cwd.replace(/\\/g, '/').split(root.replace(/\\/g, '/'))[1];
if (relativeCwd) {
relativeCwd = relativeCwd.startsWith('/')
? relativeCwd.substring(1)
: relativeCwd;
const matchingProject = Object.keys(workspaceConfiguration.projects).find(
(p) => {
const projectRoot = workspaceConfiguration.projects[p].root;
return (
relativeCwd == projectRoot ||
relativeCwd.startsWith(`${projectRoot}/`)
);
}
);
if (matchingProject) return matchingProject;
}
return (
(workspaceConfiguration.cli as { defaultProjectName: string })
?.defaultProjectName || workspaceConfiguration.defaultProject
);
}