@@ -2,6 +2,7 @@ const fs = require('fs')
22const path = require ( 'path' )
33const {
44 chalk,
5+ execa,
56 semver,
67
78 log,
@@ -12,19 +13,17 @@ const {
1213 isPlugin,
1314 resolvePluginId,
1415
15- loadModule
16+ loadModule,
17+ resolveModule
1618} = require ( '@vue/cli-shared-utils' )
1719
1820const tryGetNewerRange = require ( './util/tryGetNewerRange' )
1921const getPkg = require ( './util/getPkg' )
2022const PackageManager = require ( './util/ProjectPackageManager' )
2123
22- const { runMigrator } = require ( './migrate' )
23-
2424function clearRequireCache ( ) {
2525 Object . keys ( require . cache ) . forEach ( key => delete require . cache [ key ] )
2626}
27-
2827module . exports = class Upgrader {
2928 constructor ( context = process . cwd ( ) ) {
3029 this . context = context
@@ -108,28 +107,39 @@ module.exports = class Upgrader {
108107
109108 log ( `Upgrading ${ packageName } from ${ installed } to ${ targetVersion } ` )
110109 await this . pm . upgrade ( `${ packageName } @~${ targetVersion } ` )
111- // as the dependencies have now changed, the require cache must be invalidated
112- // otherwise it may affect the behavior of the migrator
113- clearRequireCache ( )
114110
115111 // The cached `pkg` field won't automatically update after running `this.pm.upgrade`.
116112 // Also, `npm install pkg@~version` won't replace the original `"pkg": "^version"` field.
117113 // So we have to manually update `this.pkg` and write to the file system in `runMigrator`
118114 this . pkg [ depEntry ] [ packageName ] = `~${ targetVersion } `
119- const noop = ( ) => { }
120-
121- const pluginMigrator =
122- loadModule ( `${ packageName } /migrator` , this . context ) || noop
123-
124- await runMigrator (
125- this . context ,
126- {
127- id : packageName ,
128- apply : pluginMigrator ,
129- baseVersion : installed
130- } ,
131- this . pkg
132- )
115+
116+ const resolvedPluginMigrator =
117+ resolveModule ( `${ packageName } /migrator` , this . context )
118+
119+ if ( resolvedPluginMigrator ) {
120+ // for unit tests, need to run migrator in the same process for mocks to work
121+ // TODO: fix the tests and remove this special case
122+ if ( process . env . VUE_CLI_TEST ) {
123+ clearRequireCache ( )
124+ await require ( './migrate' ) . runMigrator (
125+ this . context ,
126+ {
127+ id : packageName ,
128+ apply : loadModule ( `${ packageName } /migrator` , this . context ) ,
129+ baseVersion : installed
130+ } ,
131+ this . pkg
132+ )
133+ return
134+ }
135+
136+ const cliBin = path . resolve ( __dirname , '../bin/vue.js' )
137+ // Run migrator in a separate process to avoid all kinds of require cache issues
138+ await execa ( 'node' , [ cliBin , 'migrate' , packageName , '--from' , installed ] , {
139+ cwd : this . context ,
140+ stdio : 'inherit'
141+ } )
142+ }
133143 }
134144
135145 async getUpgradable ( includeNext ) {
0 commit comments