Skip to content

Commit c0fefbe

Browse files
committed
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 0fd4404 commit c0fefbe

3 files changed

Lines changed: 80 additions & 4 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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,46 @@ 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.filter((opt) => opt.positional !== undefined)).toEqual([
386+
jasmine.objectContaining({ name: 'preset', positional: 0, required: false }),
387+
jasmine.objectContaining({ name: 'name', positional: 1, required: false }),
388+
]);
389+
});
390+
});
349391
});

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ export default function () {
2525
"description": "Fake schematic",
2626
"schema": "./fake-schema.json"
2727
},
28+
"fake-reversed": {
29+
"factory": "./fake",
30+
"description": "Fake schematic",
31+
"schema": "./fake-reversed-schema.json"
32+
},
2833
}
2934
}`,
3035
[join(genRoot, 'fake-schema.json')]: `
@@ -63,6 +68,31 @@ export default function () {
6368
"description": "optB"
6469
}
6570
}
71+
}`,
72+
[join(genRoot, 'fake-reversed-schema.json')]: `
73+
{
74+
"$id": "FakeReversedSchema",
75+
"title": "Fake Reversed Schema",
76+
"type": "object",
77+
"required": ["b"],
78+
"properties": {
79+
"b": {
80+
"type": "string",
81+
"description": "b.",
82+
"$default": {
83+
"$source": "argv",
84+
"index": 0
85+
}
86+
},
87+
"a": {
88+
"type": "string",
89+
"description": "a.",
90+
"$default": {
91+
"$source": "argv",
92+
"index": 1
93+
}
94+
}
95+
}
6696
}`,
6797
[join(genRoot, 'fake.js')]: `
6898
function def(options) {
@@ -79,6 +109,10 @@ export default function () {
79109
assert.match(stdout, /ng generate fake-schematics:fake <a> \[b\]/);
80110
assert.match(stdout, /opt-a[\s\S]*opt-b[\s\S]*opt-c/);
81111
})
112+
.then(() => ng('generate', 'fake-schematics:fake-reversed', '--help'))
113+
.then(({ stdout }) => {
114+
assert.match(stdout, /ng generate fake-schematics:fake-reversed <b> \[a\]/);
115+
})
82116
// set up default collection.
83117
.then(() =>
84118
updateJsonFile('angular.json', (json) => {

0 commit comments

Comments
 (0)