diff --git a/packages/cli/generators/import-lb3-models/index.js b/packages/cli/generators/import-lb3-models/index.js index 5343e222064b..4808ba8091d1 100644 --- a/packages/cli/generators/import-lb3-models/index.js +++ b/packages/cli/generators/import-lb3-models/index.js @@ -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(); } @@ -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 = []; @@ -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); diff --git a/packages/cli/test/integration/generators/import-lb3-models.integration.js b/packages/cli/test/integration/generators/import-lb3-models.integration.js index 29aec830137a..c1ac32e74153 100644 --- a/packages/cli/test/integration/generators/import-lb3-models.integration.js +++ b/packages/cli/test/integration/generators/import-lb3-models.integration.js @@ -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});