@@ -10,9 +10,8 @@ const chalk = require('chalk');
1010const { StatusConflicter, readTextFromStdin} = require ( './utils' ) ;
1111const path = require ( 'path' ) ;
1212const fs = require ( 'fs' ) ;
13- const readline = require ( 'readline' ) ;
1413const debug = require ( './debug' ) ( 'base-generator' ) ;
15- const assert = require ( 'assert ' ) ;
14+ const semver = require ( 'semver ' ) ;
1615
1716/**
1817 * Base Generator for LoopBack 4
@@ -130,17 +129,16 @@ module.exports = class BaseGenerator extends Generator {
130129 * @param {* } question
131130 */
132131 async _getDefaultAnswer ( question , answers ) {
132+ // First check existing answers
133+ let defaultVal = answers [ question . name ] ;
134+ if ( defaultVal != null ) return defaultVal ;
135+
136+ // Now check the `default` of the prompt
133137 let def = question . default ;
134138 if ( typeof question . default === 'function' ) {
135139 def = await question . default ( answers ) ;
136140 }
137- let defaultVal = def ;
138-
139- if ( def == null ) {
140- // No `default` is set for the question, check existing answers
141- defaultVal = answers [ question . name ] ;
142- if ( defaultVal != null ) return defaultVal ;
143- }
141+ defaultVal = def ;
144142
145143 if ( question . type === 'confirm' ) {
146144 return defaultVal != null ? defaultVal : true ;
@@ -274,7 +272,7 @@ module.exports = class BaseGenerator extends Generator {
274272 * keyword 'loopback' under 'keywords' attribute in package.json.
275273 * 'keywords' is an array
276274 */
277- checkLoopBackProject ( ) {
275+ async checkLoopBackProject ( ) {
278276 debug ( 'Checking for loopback project' ) ;
279277 if ( this . shouldExit ( ) ) return false ;
280278 const pkg = this . fs . readJSON ( this . destinationPath ( 'package.json' ) ) ;
@@ -297,8 +295,48 @@ module.exports = class BaseGenerator extends Generator {
297295 'The command must be run in a LoopBack project.' ,
298296 ) ;
299297 this . exit ( err ) ;
298+ return ;
300299 }
301300 this . packageJson = pkg ;
301+
302+ const projectDeps = pkg . dependencies || { } ;
303+ const projectDevDeps = pkg . devDependencies || { } ;
304+
305+ const cliPkg = require ( '../package.json' ) ;
306+ const templateDeps = cliPkg . config . templateDependencies ;
307+ const incompatibleDeps = { } ;
308+ for ( const d in templateDeps ) {
309+ const versionRange = projectDeps [ d ] || projectDevDeps [ d ] ;
310+ if ( ! versionRange || semver . intersects ( versionRange , templateDeps [ d ] ) )
311+ continue ;
312+ incompatibleDeps [ d ] = [ versionRange , templateDeps [ d ] ] ;
313+ }
314+
315+ if ( Object . keys ( incompatibleDeps ) . length === 0 ) {
316+ // No incompatible dependencies
317+ return ;
318+ }
319+ this . log (
320+ chalk . red (
321+ 'The project has dependencies with incompatible versions required by the CLI:' ,
322+ ) ,
323+ ) ;
324+ for ( const d in incompatibleDeps ) {
325+ this . log ( chalk . yellow ( '- %s: %s (cli %s)' ) , d , ...incompatibleDeps [ d ] ) ;
326+ }
327+ const prompts = [
328+ {
329+ name : 'ignoreIncompatibleDependencies' ,
330+ message : `Continue to run the command?` ,
331+ type : 'confirm' ,
332+ default : false ,
333+ } ,
334+ ] ;
335+ const answers = await this . prompt ( prompts ) ;
336+ if ( answers && answers . ignoreIncompatibleDependencies ) {
337+ return ;
338+ }
339+ this . exit ( new Error ( 'Incompatible dependencies' ) ) ;
302340 }
303341
304342 _runNpmScript ( projectDir , args ) {
0 commit comments