@@ -2,6 +2,7 @@ const fs = require('fs')
2
2
const path = require ( 'path' )
3
3
const {
4
4
chalk,
5
+ execa,
5
6
semver,
6
7
7
8
log,
@@ -12,19 +13,17 @@ const {
12
13
isPlugin,
13
14
resolvePluginId,
14
15
15
- loadModule
16
+ loadModule,
17
+ resolveModule
16
18
} = require ( '@vue/cli-shared-utils' )
17
19
18
20
const tryGetNewerRange = require ( './util/tryGetNewerRange' )
19
21
const getPkg = require ( './util/getPkg' )
20
22
const PackageManager = require ( './util/ProjectPackageManager' )
21
23
22
- const { runMigrator } = require ( './migrate' )
23
-
24
24
function clearRequireCache ( ) {
25
25
Object . keys ( require . cache ) . forEach ( key => delete require . cache [ key ] )
26
26
}
27
-
28
27
module . exports = class Upgrader {
29
28
constructor ( context = process . cwd ( ) ) {
30
29
this . context = context
@@ -108,28 +107,39 @@ module.exports = class Upgrader {
108
107
109
108
log ( `Upgrading ${ packageName } from ${ installed } to ${ targetVersion } ` )
110
109
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 ( )
114
110
115
111
// The cached `pkg` field won't automatically update after running `this.pm.upgrade`.
116
112
// Also, `npm install pkg@~version` won't replace the original `"pkg": "^version"` field.
117
113
// So we have to manually update `this.pkg` and write to the file system in `runMigrator`
118
114
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
+ }
133
143
}
134
144
135
145
async getUpgradable ( includeNext ) {
0 commit comments