Skip to content

Commit

Permalink
feat(cli): specify models with --models in lb4 discover
Browse files Browse the repository at this point in the history
This PR adds an option to lb4 discover command. --models is an option to pass models that lb4
discover should create without prompt.

Co-authored-by: Sohaib Ahsan <[email protected]>
Signed-off-by: Muhammad Aaqil <[email protected]>
  • Loading branch information
aaqilniz and sohaibahsan007 committed Dec 24, 2022
1 parent 191bca7 commit e3e3660
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/site/Discovering-models.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ placed. Default is `src/models`
`--schema`: Specify the schema which the datasource will find the models to
discover

**`--models`**: Specify the models to be generated

`--optionalId`: Specify if the Id property of generated models will be marked as

not required

### Interactive Prompts
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/.yo-rc.json
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,12 @@
"name": "outDir",
"hide": false
},
"models": {
"type": "String",
"description": "Discover specific models without prompting users to select e.g:--models=table1,table2",
"name": "models",
"hide": false
},
"optionalId": {
"type": "Boolean",
"description": "Boolean to mark id property as optional field",
Expand Down
18 changes: 18 additions & 0 deletions packages/cli/generators/discover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
default: undefined,
});

this.option('models', {
type: String,
description: g.f(
'Discover specific models without prompting users to select e.g:--models=table1,table2',
),
default: undefined,
});

this.option('optionalId', {
type: Boolean,
description: g.f('Boolean to mark id property as optional field'),
Expand Down Expand Up @@ -201,6 +209,16 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
this.discoveringModels = this.modelChoices;
}

if (this.options.models) {
const answers = {discoveringModels: this.options.models.split(',')};
debug(`Models specified: ${JSON.stringify(answers)}`);
this.discoveringModels = [];
answers.discoveringModels.forEach(m => {
this.discoveringModels.push(this.modelChoices.find(c => c.name === m));
});
return;
}

const prompts = [
{
name: 'discoveringModels',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,12 @@ exports[`cli saves command metadata to .yo-rc.json 1`] = `
"name": "outDir",
"hide": false
},
"models": {
"type": "String",
"description": "Discover specific models without prompting users to select e.g:--models=table1,table2",
"name": "models",
"hide": false
},
"optionalId": {
"type": "Boolean",
"description": "Boolean to mark id property as optional field",
Expand Down
19 changes: 19 additions & 0 deletions packages/cli/test/integration/generators/discover.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ const disableCamelCaseOptions = {
const missingDataSourceOptions = {
dataSource: 'foo',
};
const specificmodelsOptions = {
models: 'Test',
dataSource: 'mem',
views: false,
disableCamelCase: true,
};
const optionalIdOptions = {
...baseOptions,
optionalId: true,
Expand Down Expand Up @@ -174,4 +180,17 @@ describe('lb4 discover integration', () => {
expectFileToMatchSnapshot(defaultExpectedTestModel);
});
});
it('generates specific models without prompts using --models', async () => {
await testUtils
.executeGenerator(generator)
.inDir(sandbox.path, () =>
testUtils.givenLBProject(sandbox.path, {
additionalFiles: SANDBOX_FILES,
}),
)
.withOptions(specificmodelsOptions);

basicModelFileChecks(defaultExpectedTestModel, defaultExpectedIndexFile);
assert.file(defaultExpectedTestModel);
});
});

0 comments on commit e3e3660

Please sign in to comment.