feat: add transformScript to GeneratorAPI (#4188)

(cherry picked from commit 5460ca498f)
This commit is contained in:
Haoqun Jiang
2019-06-26 18:25:47 +08:00
parent 818e5ddccd
commit 113eb65e6d
3 changed files with 51 additions and 1 deletions
+17
View File
@@ -6,6 +6,7 @@ const resolve = require('resolve')
const { isBinaryFileSync } = require('isbinaryfile')
const semver = require('semver')
const mergeDeps = require('./util/mergeDeps')
const runCodemod = require('./util/runCodemod')
const stringifyJS = require('./util/stringifyJS')
const ConfigTransform = require('./ConfigTransform')
const { getPluginLink, toShortPluginId, loadModule } = require('@vue/cli-shared-utils')
@@ -300,6 +301,22 @@ class GeneratorAPI {
return fn
}
/**
* Run codemod on a script file or the script part of a .vue file
* @param {string} file the path to the file to transform
* @param {Codemod} codemod the codemod module to run
* @param {object} options additional options for the codemod
*/
transformScript (file, codemod, options) {
this._injectFileMiddleware(files => {
files[file] = runCodemod(
codemod,
{ path: this.resolve(file), source: files[file] },
options
)
})
}
/**
* Add import statements to a file.
*/