@@ -11,23 +11,20 @@ const {
1111
1212 isPlugin,
1313 resolvePluginId,
14+
1415 loadModule
1516} = require ( '@vue/cli-shared-utils' )
1617
17- const Migrator = require ( './Migrator' )
1818const tryGetNewerRange = require ( './util/tryGetNewerRange' )
19- const readFiles = require ( './util/readFiles' )
20- const getChangedFiles = require ( './util/getChangedFiles' )
21-
22- const getPackageJson = require ( './util/getPackageJson' )
19+ const getPkg = require ( './util/getPkg' )
2320const PackageManager = require ( './util/ProjectPackageManager' )
2421
25- const isTestOrDebug = process . env . VUE_CLI_TEST || process . env . VUE_CLI_DEBUG
22+ const { runMigrator } = require ( './migrate' )
2623
2724module . exports = class Upgrader {
2825 constructor ( context = process . cwd ( ) ) {
2926 this . context = context
30- this . pkg = getPackageJson ( this . context )
27+ this . pkg = getPkg ( this . context )
3128 this . pm = new PackageManager ( { context } )
3229 }
3330
@@ -43,7 +40,8 @@ module.exports = class Upgrader {
4340 }
4441
4542 for ( const p of upgradable ) {
46- this . pkg = getPackageJson ( this . context )
43+ // reread to avoid accidentally writing outdated package.json back
44+ this . pkg = getPkg ( this . context )
4745 await this . upgrade ( p . name , { to : p . latest } )
4846 }
4947
@@ -65,6 +63,14 @@ module.exports = class Upgrader {
6563 throw new Error ( `Can't find ${ chalk . yellow ( packageName ) } in ${ chalk . yellow ( 'package.json' ) } ` )
6664 }
6765
66+ const installed = options . from || this . pm . getInstalledVersion ( packageName )
67+ if ( ! installed ) {
68+ throw new Error (
69+ `Can't find ${ chalk . yellow ( packageName ) } in ${ chalk . yellow ( 'node_modules' ) } . Please install the dependencies first.\n` +
70+ `Or to force upgrade, you can specify your current plugin version with the ${ chalk . cyan ( '--from' ) } option`
71+ )
72+ }
73+
6874 let targetVersion = options . to || 'latest'
6975 // if the targetVersion is not an exact version
7076 if ( ! / \d + \. \d + \. \d + / . test ( targetVersion ) ) {
@@ -84,7 +90,6 @@ module.exports = class Upgrader {
8490 stopSpinner ( )
8591 }
8692
87- const installed = this . pm . getInstalledVersion ( packageName )
8893 if ( targetVersion === installed ) {
8994 log ( `Already installed ${ packageName } @${ targetVersion } ` )
9095
@@ -102,76 +107,19 @@ module.exports = class Upgrader {
102107
103108 // the cached `pkg` field won't automatically update after running `this.pm.upgrade`
104109 this . pkg [ depEntry ] [ packageName ] = `^${ targetVersion } `
105- await this . runMigrator ( packageName , { installed } )
106- }
107-
108- async runMigrator ( packageName , options ) {
109110 const pluginMigrator = loadModule ( `${ packageName } /migrator` , this . context )
110- if ( ! pluginMigrator ) { return }
111-
112- const plugin = {
113- id : packageName ,
114- apply : pluginMigrator ,
115- installed : options . installed
116- }
117-
118- const afterInvokeCbs = [ ]
119- const migrator = new Migrator ( this . context , {
120- plugin : plugin ,
121-
122- pkg : this . pkg ,
123- files : await readFiles ( this . context ) ,
124- afterInvokeCbs,
125- invoking : true
126- } )
127111
128- log ( `🚀 Running migrator of ${ packageName } ` )
129- await migrator . generate ( {
130- extractConfigFiles : true ,
131- checkExisting : true
132- } )
133-
134- const newDeps = migrator . pkg . dependencies
135- const newDevDeps = migrator . pkg . devDependencies
136- const depsChanged =
137- JSON . stringify ( newDeps ) !== JSON . stringify ( this . pkg . dependencies ) ||
138- JSON . stringify ( newDevDeps ) !== JSON . stringify ( this . pkg . devDependencies )
139-
140- if ( ! isTestOrDebug && depsChanged ) {
141- log ( `📦 Installing additional dependencies...` )
142- log ( )
143- await this . pm . install ( )
144- }
145-
146- if ( afterInvokeCbs . length ) {
147- logWithSpinner ( '⚓' , `Running completion hooks...` )
148- for ( const cb of afterInvokeCbs ) {
149- await cb ( )
150- }
151- stopSpinner ( )
152- log ( )
153- }
154-
155- log (
156- `${ chalk . green (
157- '✔'
158- ) } Successfully invoked migrator for plugin: ${ chalk . cyan ( plugin . id ) } `
159- )
160-
161- const changedFiles = getChangedFiles ( this . context )
162- if ( changedFiles . length ) {
163- log ( ` The following files have been updated / added:\n` )
164- log ( chalk . red ( changedFiles . map ( line => ` ${ line } ` ) . join ( '\n' ) ) )
165- log ( )
166- log (
167- ` You should review these changes with ${ chalk . cyan (
168- 'git diff'
169- ) } and commit them.`
112+ if ( pluginMigrator ) {
113+ await runMigrator (
114+ this . context ,
115+ {
116+ id : packageName ,
117+ apply : pluginMigrator ,
118+ baseVersion : installed
119+ } ,
120+ this . pkg
170121 )
171- log ( )
172122 }
173-
174- migrator . printExitLogs ( )
175123 }
176124
177125 async getUpgradable ( includeNext ) {
@@ -188,8 +136,8 @@ module.exports = class Upgrader {
188136 const installed = await this . pm . getInstalledVersion ( name )
189137 const wanted = await this . pm . getRemoteVersion ( name , range )
190138
191- if ( installed === 'N/A' ) {
192- throw new Error ( ' At least one dependency is not installed . Please run npm install or yarn before trying to upgrade' )
139+ if ( ! installed ) {
140+ throw new Error ( ` At least one dependency can't be found . Please install the dependencies before trying to upgrade` )
193141 }
194142
195143 let latest = await this . pm . getRemoteVersion ( name )
@@ -242,7 +190,7 @@ module.exports = class Upgrader {
242190 for ( const p of upgradable ) {
243191 const fields = [
244192 p . name ,
245- p . installed ,
193+ p . installed || 'N/A' ,
246194 p . wanted ,
247195 p . latest ,
248196 `vue upgrade ${ p . name } ${ includeNext ? ' --next' : '' } `
0 commit comments