Skip to content

Commit d69dcec

Browse files
authored
fix(@angular/cli): keep schematic positional arguments in index order
sort checked `positional` for truthiness, so argument at index 0 was ordered like plain option and help showed it after index 1 when that argument has `x-prompt` both are optional for yargs and values go to wrong argumnets, now positionals are sorted by index
1 parent 566abde commit d69dcec

3 files changed

Lines changed: 52 additions & 9 deletions

File tree

‎packages/angular/cli/src/command-builder/utilities/json-schema.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,10 @@ export async function parseJsonSchemaToOptions(
390390

391391
// Sort by positional and name.
392392
return options.sort((a, b) => {
393-
if (a.positional) {
394-
return b.positional ? a.positional - b.positional : a.name.localeCompare(b.name);
395-
} else if (b.positional) {
396-
return -1;
393+
if (a.positional !== undefined) {
394+
return b.positional !== undefined ? a.positional - b.positional : -1;
395+
} else if (b.positional !== undefined) {
396+
return 1;
397397
}
398398

399399
return a.name.localeCompare(b.name);

‎packages/angular/cli/src/command-builder/utilities/json-schema_spec.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,47 @@ describe('parseJsonSchemaToOptions', () => {
346346
);
347347
});
348348
});
349+
350+
describe('with positional arguments not in alphabetical order', () => {
351+
it('sorts positional arguments by their index', async () => {
352+
const jsonSchema = {
353+
'$id': 'FakeSchema',
354+
'title': 'Fake Schema',
355+
'type': 'object',
356+
'required': ['preset'],
357+
'properties': {
358+
'preset': {
359+
'type': 'string',
360+
'enum': ['form', 'table'],
361+
'description': 'preset.',
362+
'$default': {
363+
'$source': 'argv',
364+
'index': 0,
365+
},
366+
'x-prompt': 'Which preset?',
367+
},
368+
'name': {
369+
'type': 'string',
370+
'description': 'name.',
371+
'$default': {
372+
'$source': 'argv',
373+
'index': 1,
374+
},
375+
},
376+
'path': {
377+
'type': 'string',
378+
'description': 'path.',
379+
},
380+
},
381+
};
382+
const registry = new schema.CoreSchemaRegistry();
383+
const options = await parseJsonSchemaToOptions(registry, jsonSchema, /* interactive= */ true);
384+
385+
expect(options).toEqual([
386+
jasmine.objectContaining({ name: 'preset', positional: 0, required: false }),
387+
jasmine.objectContaining({ name: 'name', positional: 1, required: false }),
388+
jasmine.objectContaining({ name: 'path', positional: undefined, required: false }),
389+
]);
390+
});
391+
});
349392
});

‎tests/e2e/tests/generate/help-output.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,22 @@ export default function () {
3232
"$id": "FakeSchema",
3333
"title": "Fake Schema",
3434
"type": "object",
35-
"required": ["a"],
35+
"required": ["b"],
3636
"properties": {
3737
"b": {
3838
"type": "string",
3939
"description": "b.",
4040
"$default": {
4141
"$source": "argv",
42-
"index": 1
42+
"index": 0
4343
}
4444
},
4545
"a": {
4646
"type": "string",
4747
"description": "a.",
4848
"$default": {
4949
"$source": "argv",
50-
"index": 0
50+
"index": 1
5151
}
5252
},
5353
"optC": {
@@ -76,7 +76,7 @@ export default function () {
7676
)
7777
.then(() => ng('generate', 'fake-schematics:fake', '--help'))
7878
.then(({ stdout }) => {
79-
assert.match(stdout, /ng generate fake-schematics:fake <a> \[b\]/);
79+
assert.match(stdout, /ng generate fake-schematics:fake <b> \[a\]/);
8080
assert.match(stdout, /opt-a[\s\S]*opt-b[\s\S]*opt-c/);
8181
})
8282
// set up default collection.
@@ -89,7 +89,7 @@ export default function () {
8989
.then(() => ng('generate', 'fake', '--help'))
9090
// verify same output
9191
.then(({ stdout }) => {
92-
assert.match(stdout, /ng generate fake <a> \[b\]/);
92+
assert.match(stdout, /ng generate fake <b> \[a\]/);
9393
assert.match(stdout, /opt-a[\s\S]*opt-b[\s\S]*opt-c/);
9494
})
9595

0 commit comments

Comments
 (0)