Skip to content

Commit

Permalink
feat: 🎸 added models option in lb4 discover cli command
Browse files Browse the repository at this point in the history
added --models option inside lb4 discover command to generate specific
models

BREAKING CHANGE: 🧨 no

✅ Closes: loopbackio#7104

Signed-off-by: Punit Diwan <[email protected]>
  • Loading branch information
punitdiwan committed Mar 23, 2022
1 parent 3d6dd2e commit 43e96ef
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/site/Discovering-models.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ placed. Default is `src/models`
`--schema`: Specify the schema which the datasource will find the models to
discover

`--models`: generate the specific model

### Interactive Prompts

Based on the option, the tool may prompt you for:
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 @@ -1270,6 +1270,12 @@
"description": "Specify the directory into which the `model.model.ts` files will be placed",
"name": "outDir",
"hide": false
},
"models": {
"type": "String",
"description": "Discover specific models without prompting users to select ex:--models=tale1,table2",
"name": "models",
"hide": false
}
},
"arguments": [
Expand Down
17 changes: 17 additions & 0 deletions packages/cli/generators/discover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
),
default: undefined,
});
this.option('models', {
type: String,
description: g.f(
'Discover specific models without prompting users to select ex:--models=tale1,table2',
),
default: undefined,
});
}

_setupGenerator() {
Expand Down Expand Up @@ -172,6 +179,16 @@ module.exports = class DiscoveryGenerator extends ArtifactGenerator {
this.discoveringModels = this.modelChoices;
}

if (this.options.models) {
const answers = {discoveringModels: this.options.models.split(',')};
debug(`Models chosen: ${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 @@ -1328,6 +1328,12 @@ exports[`cli saves command metadata to .yo-rc.json 1`] = `
"description": "Specify the directory into which the \`model.model.ts\` files will be placed",
"name": "outDir",
"hide": false
},
"models": {
"type": "String",
"description": "Discover specific models without prompting users to select ex:--models=tale1,table2",
"name": "models",
"hide": false
}
},
"arguments": [
Expand Down
20 changes: 20 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,
};

// Expected File Name
const defaultExpectedTestModel = path.join(
Expand Down Expand Up @@ -155,5 +161,19 @@ describe('lb4 discover integration', () => {
.withOptions(missingDataSourceOptions),
).to.be.rejectedWith(/Cannot find datasource/);
});

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 43e96ef

Please sign in to comment.