Skip to content

Commit acd4925

Browse files
clydinKeen Yee Liau
authored and
Keen Yee Liau
committedSep 14, 2018
fix(@angular/cli): warn if targets is present when using a schematic
1 parent 17d52c6 commit acd4925

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
 

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

+39
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import {
1111
JsonObject,
1212
experimental,
13+
json,
1314
logging,
1415
normalize,
1516
schema,
@@ -43,6 +44,7 @@ import {
4344
getDefaultSchematicCollection,
4445
getPackageManager,
4546
getSchematicDefaults,
47+
getWorkspaceRaw,
4648
} from '../utilities/config';
4749
import { ArgumentStrategy, Command, CommandContext, Option } from './command';
4850

@@ -202,6 +204,43 @@ export abstract class SchematicCommand extends Command {
202204
const defaultOptions = this.readDefaults(collectionName, schematicName, schematicOptions);
203205
schematicOptions = { ...schematicOptions, ...defaultOptions };
204206

207+
// TODO: Remove warning check when 'targets' is default
208+
if (collectionName !== '@schematics/angular') {
209+
const [ast, configPath] = getWorkspaceRaw('local');
210+
if (ast) {
211+
const projectsKeyValue = ast.properties.find(p => p.key.value === 'projects');
212+
if (!projectsKeyValue || projectsKeyValue.value.kind !== 'object') {
213+
return;
214+
}
215+
216+
const positions: json.Position[] = [];
217+
for (const projectKeyValue of projectsKeyValue.value.properties) {
218+
const projectNode = projectKeyValue.value;
219+
if (projectNode.kind !== 'object') {
220+
continue;
221+
}
222+
const targetsKeyValue = projectNode.properties.find(p => p.key.value === 'targets');
223+
if (targetsKeyValue) {
224+
positions.push(targetsKeyValue.start);
225+
}
226+
}
227+
228+
if (positions.length > 0) {
229+
const warning = tags.oneLine`
230+
WARNING: This command may not execute successfully.
231+
The package/collection may not support the 'targets' field within '${configPath}'.
232+
This can be corrected by renaming the following 'targets' fields to 'architect':
233+
`;
234+
235+
const locations = positions
236+
.map((p, i) => `${i + 1}) Line: ${p.line + 1}; Column: ${p.character + 1}`)
237+
.join('\n');
238+
239+
this.logger.warn(warning + '\n' + locations + '\n');
240+
}
241+
}
242+
}
243+
205244
// Remove all of the original arguments which have already been parsed
206245

207246
const argumentCount = this._originalOptions

0 commit comments

Comments
 (0)