Skip to content

Commit

Permalink
feat(cli): automatically include base models when importing LB3 models
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
bajtos committed Feb 27, 2020
1 parent b9716d1 commit 8c5f3cf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
26 changes: 26 additions & 0 deletions packages/cli/generators/import-lb3-models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ module.exports = class Lb3ModelImporter extends BaseGenerator {
this.destinationPath(),
this.artifactInfo.outDir,
);
this.artifactInfo.modelDir = path.resolve(
this.artifactInfo.rootDir,
utils.modelsDir,
);
return super.setOptions();
}

Expand Down Expand Up @@ -99,6 +103,14 @@ Learn more at https://loopback.io/doc/en/lb4/Importing-LB3-models.html
this.modelNames = answers.modelNames;
}

async loadExistingLb4Models() {
debug(`model list dir ${this.artifactInfo.modelDir}`);
this.existingLb4ModelNames = await utils.getArtifactList(
this.artifactInfo.modelDir,
'model',
);
}

async migrateSelectedModels() {
if (this.shouldExit()) return;
this.modelFiles = [];
Expand Down Expand Up @@ -136,6 +148,20 @@ Learn more at https://loopback.io/doc/en/lb4/Importing-LB3-models.html
);
debug('LB4 model data', templateData);

if (!templateData.isModelBaseBuiltin) {
const baseName = templateData.modelBaseClass;
if (
!this.existingLb4ModelNames.includes(baseName) &&
!this.modelNames.includes(baseName)
) {
this.log(
'Adding %s (base of %s) to the list of imported models.',
chalk.yellow(baseName),
chalk.yellow(name),
);
this.modelNames.push(baseName);
}
}
const fileName = utils.getModelFileName(name);
const fullTargetPath = path.resolve(this.artifactInfo.relPath, fileName);
debug('Model %s output file', name, fullTargetPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,7 @@ describe('lb4 import-lb3-models', function() {
.inDir(SANDBOX_PATH, () => testUtils.givenLBProject(SANDBOX_PATH))
.withArguments(APP_USING_MODEL_INHERITANCE)
.withPrompts({
modelNames: [
'Customer',
// FIXME: generator should include the following models automatically
'User',
'UserBase',
],
modelNames: ['Customer'],
})
.withOptions({outDir});

Expand Down

0 comments on commit 8c5f3cf

Please sign in to comment.