0
# CLI Configuration
1
2
Parsers and manipulators for workspace configuration and build targets. These utilities help manage Nx workspace configuration files and target definitions.
3
4
## Capabilities
5
6
### Workspace Path Discovery
7
8
Locate workspace configuration files (deprecated but still functional).
9
10
```typescript { .api }
11
/**
12
* Get the path to workspace configuration file
13
* @deprecated Nx no longer supports workspace.json
14
* @param host - The file system tree
15
* @returns Path to workspace config file or undefined
16
*/
17
function getWorkspacePath(host: Tree): string;
18
```
19
20
**Usage Example:**
21
22
```typescript
23
import { Tree } from "@angular-devkit/schematics";
24
import { getWorkspacePath } from "@nx/workspace";
25
26
function findWorkspaceConfig(tree: Tree) {
27
const workspacePath = getWorkspacePath(tree);
28
29
if (workspacePath) {
30
console.log("Found workspace config at:", workspacePath);
31
// Could be '/angular.json' or '/workspace.json'
32
} else {
33
console.log("No workspace configuration found");
34
}
35
}
36
```
37
38
### Target String Parsing
39
40
Parse target strings into structured objects for manipulation.
41
42
```typescript { .api }
43
/**
44
* Parse a target string into its components
45
* @param targetString - Target string in format "project:target:config"
46
* @returns Parsed target object
47
*/
48
function parseTarget(targetString: string): ParsedTarget;
49
50
interface ParsedTarget {
51
/** Name of the project */
52
project: string;
53
/** Name of the target */
54
target: string;
55
/** Optional configuration name */
56
config?: string;
57
}
58
```
59
60
**Usage Examples:**
61
62
```typescript
63
import { parseTarget } from "@nx/workspace";
64
65
// Parse simple target
66
const simpleTarget = parseTarget("my-app:build");
67
console.log(simpleTarget);
68
// { project: "my-app", target: "build", config: undefined }
69
70
// Parse target with configuration
71
const configTarget = parseTarget("my-app:build:production");
72
console.log(configTarget);
73
// { project: "my-app", target: "build", config: "production" }
74
75
// Parse complex target
76
const complexTarget = parseTarget("libs-shared-ui:test:ci");
77
console.log(complexTarget);
78
// { project: "libs-shared-ui", target: "test", config: "ci" }
79
```
80
81
### Target Manipulation
82
83
Edit target configurations using callback functions.
84
85
```typescript { .api }
86
/**
87
* Edit a target string using callback function
88
* @param targetString - Original target string
89
* @param callback - Function to modify the parsed target
90
* @returns Modified target string
91
*/
92
function editTarget(targetString: string, callback: (target: ParsedTarget) => ParsedTarget): string;
93
```
94
95
**Usage Examples:**
96
97
```typescript
98
import { editTarget } from "@nx/workspace";
99
100
// Change target name
101
const newTarget = editTarget("my-app:build:production", (target) => {
102
target.target = "compile";
103
return target;
104
});
105
console.log(newTarget); // "my-app:compile:production"
106
107
// Add configuration
108
const withConfig = editTarget("my-app:build", (target) => {
109
target.config = "development";
110
return target;
111
});
112
console.log(withConfig); // "my-app:build:development"
113
114
// Change project name
115
const renamedProject = editTarget("old-name:test", (target) => {
116
target.project = "new-name";
117
return target;
118
});
119
console.log(renamedProject); // "new-name:test"
120
```
121
122
### Target Serialization
123
124
Convert target objects back to strings (deprecated utility).
125
126
```typescript { .api }
127
/**
128
* Serialize a target object to string format
129
* @deprecated Use the utility from nx/src/utils instead
130
* @param target - Target object to serialize
131
* @returns Target string in "project:target:config" format
132
*/
133
function serializeTarget(target: ParsedTarget): string;
134
```
135
136
**Usage Example:**
137
138
```typescript
139
import { serializeTarget } from "@nx/workspace";
140
141
const target = {
142
project: "my-app",
143
target: "build",
144
config: "production"
145
};
146
147
const targetString = serializeTarget(target);
148
console.log(targetString); // "my-app:build:production"
149
150
// Without configuration
151
const simpleTarget = {
152
project: "my-lib",
153
target: "test"
154
};
155
156
const simpleString = serializeTarget(simpleTarget);
157
console.log(simpleString); // "my-lib:test"
158
```
159
160
## Target String Format
161
162
### Standard Format
163
164
Target strings follow the format: `project:target[:configuration]`
165
166
- **project**: Name of the Nx project
167
- **target**: Name of the target (build, test, lint, etc.)
168
- **configuration**: Optional configuration name (production, development, ci, etc.)
169
170
### Examples
171
172
```typescript
173
// Simple targets
174
"my-app:build" // Build my-app with default configuration
175
"shared-lib:test" // Test shared-lib
176
"api:serve" // Serve the api project
177
178
// Targets with configuration
179
"my-app:build:production" // Build my-app with production configuration
180
"shared-lib:test:ci" // Test shared-lib with ci configuration
181
"api:serve:development" // Serve api with development configuration
182
183
// Complex project names
184
"libs-shared-ui:build" // Library with dashed name
185
"apps-admin-dashboard:serve:local" // Nested project with configuration
186
```
187
188
## Practical Usage Patterns
189
190
### Batch Target Operations
191
192
```typescript
193
import { parseTarget, editTarget } from "@nx/workspace";
194
195
function updateTargetsForProject(targets: string[], newProjectName: string): string[] {
196
return targets.map(targetString => {
197
return editTarget(targetString, (target) => {
198
target.project = newProjectName;
199
return target;
200
});
201
});
202
}
203
204
const originalTargets = [
205
"old-app:build",
206
"old-app:test",
207
"old-app:serve:development"
208
];
209
210
const updatedTargets = updateTargetsForProject(originalTargets, "new-app");
211
console.log(updatedTargets);
212
// ["new-app:build", "new-app:test", "new-app:serve:development"]
213
```
214
215
### Configuration Management
216
217
```typescript
218
import { parseTarget, serializeTarget } from "@nx/workspace";
219
220
function addProductionConfig(targetString: string): string {
221
const target = parseTarget(targetString);
222
223
// Only add production config if no config exists
224
if (!target.config) {
225
target.config = "production";
226
}
227
228
return serializeTarget(target);
229
}
230
231
const targets = ["my-app:build", "my-app:test", "my-app:serve:development"];
232
const productionTargets = targets.map(addProductionConfig);
233
console.log(productionTargets);
234
// ["my-app:build:production", "my-app:test:production", "my-app:serve:development"]
235
```
236
237
### Target Validation
238
239
```typescript
240
import { parseTarget } from "@nx/workspace";
241
242
function validateTargetString(targetString: string): boolean {
243
try {
244
const target = parseTarget(targetString);
245
return !!(target.project && target.target);
246
} catch {
247
return false;
248
}
249
}
250
251
function getValidTargets(targets: string[]): string[] {
252
return targets.filter(validateTargetString);
253
}
254
255
const mixedTargets = [
256
"valid-app:build",
257
"invalid",
258
"another-app:test:ci",
259
":build", // Missing project
260
"app:" // Missing target
261
];
262
263
const validTargets = getValidTargets(mixedTargets);
264
console.log(validTargets);
265
// ["valid-app:build", "another-app:test:ci"]
266
```
267
268
## Types
269
270
```typescript { .api }
271
interface ParsedTarget {
272
/** Name of the project */
273
project: string;
274
/** Name of the target */
275
target: string;
276
/** Optional configuration name */
277
config?: string;
278
}
279
280
type TargetCallback = (target: ParsedTarget) => ParsedTarget;
281
```