@@ -8,7 +8,7 @@ const mergeDeps = require('./util/mergeDeps')
8
8
const runCodemod = require ( './util/runCodemod' )
9
9
const stringifyJS = require ( './util/stringifyJS' )
10
10
const ConfigTransform = require ( './ConfigTransform' )
11
- const { semver, getPluginLink, toShortPluginId, loadModule } = require ( '@vue/cli-shared-utils' )
11
+ const { semver, error , getPluginLink, toShortPluginId, loadModule } = require ( '@vue/cli-shared-utils' )
12
12
13
13
const isString = val => typeof val === 'string'
14
14
const isFunction = val => typeof val === 'function'
@@ -81,6 +81,20 @@ class GeneratorAPI {
81
81
this . generator . fileMiddlewares . push ( middleware )
82
82
}
83
83
84
+ /**
85
+ * Normalize absolute path, Windows-style path
86
+ * to the relative path used as index in this.files
87
+ * @param {string } p the path to normalize
88
+ */
89
+ _normalizePath ( p ) {
90
+ if ( path . isAbsolute ( p ) ) {
91
+ p = path . relative ( this . generator . context , p )
92
+ }
93
+ // The `files` tree always use `/` in its index.
94
+ // So we need to normalize the path string in case the user passes a Windows path.
95
+ return p . replace ( / \\ / g, '/' )
96
+ }
97
+
84
98
/**
85
99
* Resolve path for a project.
86
100
*
@@ -373,10 +387,20 @@ class GeneratorAPI {
373
387
* @param {object } options additional options for the codemod
374
388
*/
375
389
transformScript ( file , codemod , options ) {
390
+ const normalizedPath = this . _normalizePath ( file )
391
+
376
392
this . _injectFileMiddleware ( files => {
377
- files [ file ] = runCodemod (
393
+ if ( typeof files [ normalizedPath ] === 'undefined' ) {
394
+ error ( `Cannot find file ${ normalizedPath } ` )
395
+ return
396
+ }
397
+
398
+ files [ normalizedPath ] = runCodemod (
378
399
codemod ,
379
- { path : this . resolve ( file ) , source : files [ file ] } ,
400
+ {
401
+ path : this . resolve ( normalizedPath ) ,
402
+ source : files [ normalizedPath ]
403
+ } ,
380
404
options
381
405
)
382
406
} )
0 commit comments