Skip to content

Commit

Permalink
Merge 570223d into 5f1cd3d
Browse files Browse the repository at this point in the history
  • Loading branch information
dhmlau committed Jan 17, 2019
2 parents 5f1cd3d + 570223d commit f0c7c03
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 9 deletions.
66 changes: 57 additions & 9 deletions packages/cli/generators/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ module.exports = class ModelGenerator extends ArtifactGenerator {
description: 'A valid based model',
});

// The base class can be specified:
// 1. From the prompt
// 2. using the --base flag
// 3. in the json when using the --config flag
// This flag is to indicate whether the base class has been validated.
this.isBaseClassChecked = false;

return super._setupGenerator();
}

Expand Down Expand Up @@ -123,14 +130,17 @@ module.exports = class ModelGenerator extends ArtifactGenerator {
return this.exit(err);
}

if (
this.options.base &&
availableModelBaseClasses.includes(this.options.base)
) {
this.artifactInfo.modelBaseClass = utils.toClassName(this.options.base);
} else {
if (this.options.base) {
// the model specified in the command line does not exists
if (this.options.base) {
this.isBaseClassChecked = true;
if (
this.isValidBaseClass(
availableModelBaseClasses,
this.options.base,
true,
)
) {
this.artifactInfo.modelBaseClass = utils.toClassName(this.options.base);
} else {
return this.exit(
new Error(
`${ERROR_NO_MODELS_FOUND} ${
Expand All @@ -155,8 +165,26 @@ module.exports = class ModelGenerator extends ArtifactGenerator {
},
])
.then(props => {
if (typeof props.modelBaseClass === 'object') {
if (this.isBaseClassChecked) return;
if (typeof props.modelBaseClass === 'object')
props.modelBaseClass = props.modelBaseClass.value;
// Find whether the specified base class is one of the available base
// class list
const isValidBase = this.isValidBaseClass(
availableModelBaseClasses,
props.modelBaseClass,
false,
);
if (!props.modelBaseClass && !isValidBase) {
this.exit(
new Error(
`${ERROR_NO_MODELS_FOUND} ${
this.artifactInfo.modelDir
}.${chalk.yellow(
'Please visit https://loopback.io/doc/en/lb4/Model-generator.html for information on how models are discovered',
)}`,
),
);
}

Object.assign(this.artifactInfo, props);
Expand Down Expand Up @@ -199,6 +227,26 @@ module.exports = class ModelGenerator extends ArtifactGenerator {
});
}

// Check whether the base class name is a valid one.
// It is either one of the predefined base classes,
// or an existing user defined class
// @isClassNameNullable - true if it is valid to have classname as null
isValidBaseClass(availableModelBaseClasses, classname, isClassNameNullable) {
if (!classname && !isClassNameNullable) return false;

for (var i in availableModelBaseClasses) {
var baseClass = '';
if (typeof availableModelBaseClasses[i] == 'object')
baseClass = availableModelBaseClasses[i].value;
else baseClass = availableModelBaseClasses[i];

if (classname == baseClass) {
return true;
}
}
return false;
}

// Prompt for a Property Name
async promptPropertyName() {
if (this.shouldExit()) return false;
Expand Down
33 changes: 33 additions & 0 deletions packages/cli/test/integration/generators/model.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ describe('lb4 model integration', () => {
).to.be.rejectedWith(/Model was not found in/);
});

it('run if passed a valid base model from command line', async () => {
await testUtils
.executeGenerator(generator)
.inDir(SANDBOX_PATH, () => testUtils.givenLBProject(SANDBOX_PATH))
.withArguments('test --base Model');

assert.file(expectedModelFile);
});

describe('model generator', () => {
it('scaffolds correct files with input', async () => {
await testUtils
Expand Down Expand Up @@ -180,6 +189,30 @@ describe('lb4 model integration', () => {
});
});

describe('model generator using --config option', () => {
it('create models with valid json', async () => {
await testUtils
.executeGenerator(generator)
.inDir(SANDBOX_PATH, () => testUtils.givenLBProject(SANDBOX_PATH))
.withArguments(['--config', '{"name":"test", "base":"Entity"}', '--yes']);

basicModelFileChecks();
});

it('does not run if pass invalid json', () => {
return expect(
testUtils
.executeGenerator(generator)
.inDir(SANDBOX_PATH, () => testUtils.givenLBProject(SANDBOX_PATH))
.withArguments([
'--config',
'{"name":"test", "base":"InvalidBaseModel"}',
'--yes',
]),
).to.be.rejectedWith(/Model was not found in/);
});
});

// Checks to ensure expected files exist with the current file contents
function basicModelFileChecks() {
assert.file(expectedModelFile);
Expand Down

0 comments on commit f0c7c03

Please sign in to comment.