Skip to content

Commit aad5f83

Browse files
committed
feat(@angular-devkit/core): support both 'targets' and 'architect' keys in workspace
1 parent a29a53e commit aad5f83

File tree

4 files changed

+80
-29
lines changed

4 files changed

+80
-29
lines changed

packages/angular_devkit/core/src/workspace/workspace-schema.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
"$ref": "#/definitions/tool",
3434
"default": {}
3535
},
36+
"targets": {
37+
"$ref": "#/definitions/tool",
38+
"default": {}
39+
},
3640
"projects": {
3741
"type": "object",
3842
"description": "A map of project names to project options.",
@@ -83,6 +87,10 @@
8387
"architect": {
8488
"$ref": "#/definitions/tool",
8589
"default": {}
90+
},
91+
"targets": {
92+
"$ref": "#/definitions/tool",
93+
"default": {}
8694
}
8795
},
8896
"additionalProperties": false,

packages/angular_devkit/core/src/workspace/workspace-schema.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ export interface WorkspaceSchema {
3535
* Tool options.
3636
*/
3737
architect?: WorkspaceTool;
38+
/**
39+
* Tool options.
40+
*/
41+
targets?: WorkspaceTool;
3842
/**
3943
* A map of project names to project options.
4044
*/
@@ -74,6 +78,10 @@ export interface WorkspaceProject {
7478
* Tool options.
7579
*/
7680
architect?: WorkspaceTool;
81+
/**
82+
* Tool options.
83+
*/
84+
targets?: WorkspaceTool;
7785
}
7886
/**
7987
* Architect options.

packages/angular_devkit/core/src/workspace/workspace.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,14 @@ export class Workspace {
129129
throw new ProjectNotFoundException(projectName);
130130
}
131131

132-
return {
133-
...workspaceProject,
134-
// Return only the project properties, and remove the tools.
135-
cli: {},
136-
schematics: {},
137-
architect: {},
138-
};
132+
// Return only the project properties, and remove the tools.
133+
const workspaceProjectClone = {...workspaceProject};
134+
delete workspaceProjectClone['cli'];
135+
delete workspaceProjectClone['schematics'];
136+
delete workspaceProjectClone['architect'];
137+
delete workspaceProjectClone['targets'];
138+
139+
return workspaceProjectClone;
139140
}
140141

141142
getDefaultProjectName(): string | null {
@@ -209,8 +210,8 @@ export class Workspace {
209210
return this._getTool('schematics');
210211
}
211212

212-
getArchitect() {
213-
return this._getTool('architect');
213+
getTargets() {
214+
return this._getTool('targets');
214215
}
215216

216217
getProjectCli(projectName: string) {
@@ -221,14 +222,19 @@ export class Workspace {
221222
return this._getProjectTool(projectName, 'schematics');
222223
}
223224

224-
getProjectArchitect(projectName: string) {
225-
return this._getProjectTool(projectName, 'architect');
225+
getProjectTargets(projectName: string) {
226+
return this._getProjectTool(projectName, 'targets');
226227
}
227228

228-
private _getTool(toolName: 'cli' | 'schematics' | 'architect'): WorkspaceTool {
229+
private _getTool(toolName: 'cli' | 'schematics' | 'targets'): WorkspaceTool {
229230
this._assertLoaded();
230231

231-
const workspaceTool = this._workspace[toolName];
232+
let workspaceTool = this._workspace[toolName];
233+
234+
// Try falling back to 'architect' if 'targets' is not there or is empty.
235+
if ((!workspaceTool || Object.keys(workspaceTool).length === 0) && toolName === 'targets') {
236+
workspaceTool = this._workspace['architect'];
237+
}
232238

233239
if (!workspaceTool) {
234240
throw new WorkspaceToolNotFoundException(toolName);
@@ -238,7 +244,7 @@ export class Workspace {
238244
}
239245

240246
private _getProjectTool(
241-
projectName: string, toolName: 'cli' | 'schematics' | 'architect',
247+
projectName: string, toolName: 'cli' | 'schematics' | 'targets',
242248
): WorkspaceTool {
243249
this._assertLoaded();
244250

@@ -248,7 +254,13 @@ export class Workspace {
248254
throw new ProjectNotFoundException(projectName);
249255
}
250256

251-
const projectTool = workspaceProject[toolName];
257+
let projectTool = workspaceProject[toolName];
258+
259+
// Try falling back to 'architect' if 'targets' is not there or is empty.
260+
if ((!projectTool || Object.keys(projectTool).length === 0) && toolName === 'targets') {
261+
projectTool = workspaceProject['architect'];
262+
}
263+
252264

253265
if (!projectTool) {
254266
throw new ProjectToolNotFoundException(toolName);

packages/angular_devkit/core/src/workspace/workspace_spec.ts

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('Workspace', () => {
4848
},
4949
},
5050
},
51-
architect: {},
51+
targets: {},
5252
projects: {
5353
app: {
5454
root: 'projects/app',
@@ -63,7 +63,7 @@ describe('Workspace', () => {
6363
},
6464
},
6565
},
66-
architect: {
66+
targets: {
6767
build: {
6868
builder: '@angular-devkit/build-angular:browser',
6969
transforms: [
@@ -101,13 +101,11 @@ describe('Workspace', () => {
101101
},
102102
},
103103
};
104-
const appProject = {
105-
...workspaceJson.projects['app'],
106-
// Tools should not be returned when getting a project.
107-
cli: {},
108-
schematics: {},
109-
architect: {},
110-
} as {} as WorkspaceProject;
104+
// Tools should not be returned when getting a project.
105+
const appProject = { ...workspaceJson.projects['app'] };
106+
delete appProject['cli'];
107+
delete appProject['schematics'];
108+
delete appProject['targets'];
111109

112110
it('loads workspace from json', (done) => {
113111
const workspace = new Workspace(root, host);
@@ -246,10 +244,20 @@ describe('Workspace', () => {
246244
).toPromise().then(done, done.fail);
247245
});
248246

249-
it('gets workspace architect', (done) => {
247+
it('gets workspace targets', (done) => {
250248
const workspace = new Workspace(root, host);
251249
workspace.loadWorkspaceFromJson(workspaceJson).pipe(
252-
tap((ws) => expect(ws.getArchitect()).toEqual(workspaceJson.architect as WorkspaceTool)),
250+
tap((ws) => expect(ws.getTargets()).toEqual(workspaceJson.targets as WorkspaceTool)),
251+
).toPromise().then(done, done.fail);
252+
});
253+
254+
it('gets workspace architect when targets is not there', (done) => {
255+
const workspace = new Workspace(root, host);
256+
const workspaceJsonClone = { ...workspaceJson };
257+
workspaceJsonClone['architect'] = workspaceJsonClone['targets'];
258+
delete workspaceJsonClone['targets'];
259+
workspace.loadWorkspaceFromJson(workspaceJsonClone).pipe(
260+
tap((ws) => expect(ws.getTargets()).toEqual(workspaceJson.targets as WorkspaceTool)),
253261
).toPromise().then(done, done.fail);
254262
});
255263

@@ -269,11 +277,26 @@ describe('Workspace', () => {
269277
).toPromise().then(done, done.fail);
270278
});
271279

272-
it('gets project architect', (done) => {
280+
it('gets project targets', (done) => {
273281
const workspace = new Workspace(root, host);
274282
workspace.loadWorkspaceFromJson(workspaceJson).pipe(
275-
tap((ws) => expect(ws.getProjectArchitect('app'))
276-
.toEqual(workspaceJson.projects.app.architect as WorkspaceTool)),
283+
tap((ws) => expect(ws.getProjectTargets('app'))
284+
.toEqual(workspaceJson.projects.app.targets as WorkspaceTool)),
285+
).toPromise().then(done, done.fail);
286+
});
287+
288+
it('gets project architect when targets is not there', (done) => {
289+
const workspace = new Workspace(root, host);
290+
const appJsonClone = { ...workspaceJson.projects.app };
291+
appJsonClone['architect'] = appJsonClone['targets'];
292+
delete appJsonClone['targets'];
293+
const simpleWorkspace = {
294+
version: 1,
295+
projects: { app: appJsonClone },
296+
};
297+
workspace.loadWorkspaceFromJson(simpleWorkspace).pipe(
298+
tap((ws) => expect(ws.getProjectTargets('app'))
299+
.toEqual(workspaceJson.projects.app.targets as WorkspaceTool)),
277300
).toPromise().then(done, done.fail);
278301
});
279302

0 commit comments

Comments
 (0)