Skip to content

Commit 9e7391c

Browse files
clydinKeen Yee Liau
authored and
Keen Yee Liau
committed
fix(@angular/cli): warn if targets is present when using a schematic
1 parent 57c049e commit 9e7391c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

packages/angular/cli/models/schematic-command.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
import {
99
experimental,
10+
json,
1011
logging,
1112
normalize,
1213
schema,
@@ -42,6 +43,7 @@ import {
4243
getProjectByCwd,
4344
getSchematicDefaults,
4445
getWorkspace,
46+
getWorkspaceRaw,
4547
} from '../utilities/config';
4648
import { parseJsonSchemaToOptions } from '../utilities/json-schema';
4749
import { BaseCommandOptions, Command } from './command';
@@ -357,6 +359,43 @@ export abstract class SchematicCommand<
357359
collectionName = schematic.collection.description.name;
358360
schematicName = schematic.description.name;
359361

362+
// TODO: Remove warning check when 'targets' is default
363+
if (collectionName !== '@schematics/angular') {
364+
const [ast, configPath] = getWorkspaceRaw('local');
365+
if (ast) {
366+
const projectsKeyValue = ast.properties.find(p => p.key.value === 'projects');
367+
if (!projectsKeyValue || projectsKeyValue.value.kind !== 'object') {
368+
return;
369+
}
370+
371+
const positions: json.Position[] = [];
372+
for (const projectKeyValue of projectsKeyValue.value.properties) {
373+
const projectNode = projectKeyValue.value;
374+
if (projectNode.kind !== 'object') {
375+
continue;
376+
}
377+
const targetsKeyValue = projectNode.properties.find(p => p.key.value === 'targets');
378+
if (targetsKeyValue) {
379+
positions.push(targetsKeyValue.start);
380+
}
381+
}
382+
383+
if (positions.length > 0) {
384+
const warning = tags.oneLine`
385+
WARNING: This command may not execute successfully.
386+
The package/collection may not support the 'targets' field within '${configPath}'.
387+
This can be corrected by renaming the following 'targets' fields to 'architect':
388+
`;
389+
390+
const locations = positions
391+
.map((p, i) => `${i + 1}) Line: ${p.line + 1}; Column: ${p.character + 1}`)
392+
.join('\n');
393+
394+
this.logger.warn(warning + '\n' + locations + '\n');
395+
}
396+
}
397+
}
398+
360399
// Set the options of format "path".
361400
let o: Option[] | null = null;
362401
let args: Arguments;

0 commit comments

Comments
 (0)