Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,10 @@ export async function parseJsonSchemaToOptions(

// Sort by positional and name.
return options.sort((a, b) => {
if (a.positional) {
return b.positional ? a.positional - b.positional : a.name.localeCompare(b.name);
} else if (b.positional) {
return -1;
if (a.positional !== undefined) {
return b.positional !== undefined ? a.positional - b.positional : -1;
} else if (b.positional !== undefined) {
return 1;
}

return a.name.localeCompare(b.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,47 @@ describe('parseJsonSchemaToOptions', () => {
);
});
});

describe('with positional arguments not in alphabetical order', () => {
it('sorts positional arguments by their index', async () => {
const jsonSchema = {
'$id': 'FakeSchema',
'title': 'Fake Schema',
'type': 'object',
'required': ['preset'],
'properties': {
'preset': {
'type': 'string',
'enum': ['form', 'table'],
'description': 'preset.',
'$default': {
'$source': 'argv',
'index': 0,
},
'x-prompt': 'Which preset?',
},
'name': {
'type': 'string',
'description': 'name.',
'$default': {
'$source': 'argv',
'index': 1,
},
},
'path': {
'type': 'string',
'description': 'path.',
},
},
};
const registry = new schema.CoreSchemaRegistry();
const options = await parseJsonSchemaToOptions(registry, jsonSchema, /* interactive= */ true);

expect(options).toEqual([
jasmine.objectContaining({ name: 'preset', positional: 0, required: false }),
jasmine.objectContaining({ name: 'name', positional: 1, required: false }),
jasmine.objectContaining({ name: 'path', positional: undefined, required: false }),
]);
});
});
});
10 changes: 5 additions & 5 deletions tests/e2e/tests/generate/help-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ export default function () {
"$id": "FakeSchema",
"title": "Fake Schema",
"type": "object",
"required": ["a"],
"required": ["b"],
"properties": {
"b": {
"type": "string",
"description": "b.",
"$default": {
"$source": "argv",
"index": 1
"index": 0
}
},
"a": {
"type": "string",
"description": "a.",
"$default": {
"$source": "argv",
"index": 0
"index": 1
}
},
"optC": {
Expand Down Expand Up @@ -76,7 +76,7 @@ export default function () {
)
.then(() => ng('generate', 'fake-schematics:fake', '--help'))
.then(({ stdout }) => {
assert.match(stdout, /ng generate fake-schematics:fake <a> \[b\]/);
assert.match(stdout, /ng generate fake-schematics:fake <b> \[a\]/);
assert.match(stdout, /opt-a[\s\S]*opt-b[\s\S]*opt-c/);
})
// set up default collection.
Expand All @@ -89,7 +89,7 @@ export default function () {
.then(() => ng('generate', 'fake', '--help'))
// verify same output
.then(({ stdout }) => {
assert.match(stdout, /ng generate fake <a> \[b\]/);
assert.match(stdout, /ng generate fake <b> \[a\]/);
assert.match(stdout, /opt-a[\s\S]*opt-b[\s\S]*opt-c/);
})

Expand Down
Loading