mirror of
https://github.com/vuejs/vue-cli.git
synced 2026-03-13 12:40:18 -05:00
fix: vue invoke should delete renamed/removed files (#1049)
This commit is contained in:
committed by
Evan You
parent
a705998add
commit
c648301743
@@ -128,3 +128,12 @@ extends:
|
||||
- '@vue/airbnb'
|
||||
`.trim())
|
||||
})
|
||||
|
||||
test('invoking a plugin that renames files', async () => {
|
||||
const project = await create(`invoke-rename`, { plugins: {}})
|
||||
const pkg = JSON.parse(await project.read('package.json'))
|
||||
pkg.devDependencies['@vue/cli-plugin-typescript'] = '*'
|
||||
await project.write('package.json', JSON.stringify(pkg, null, 2))
|
||||
await project.run(`${require.resolve('../bin/vue')} invoke typescript -d`)
|
||||
expect(project.has('src/main.js')).toBe(false)
|
||||
})
|
||||
|
||||
@@ -51,6 +51,8 @@ module.exports = class Generator {
|
||||
extractConfigFiles = false,
|
||||
checkExisting = false
|
||||
} = {}) {
|
||||
// save the file system before applying plugin for comparison
|
||||
const initialFiles = Object.assign({}, this.files)
|
||||
// extract configs from package.json into dedicated files.
|
||||
this.extractConfigFiles(extractConfigFiles, checkExisting)
|
||||
// wait for file resolve
|
||||
@@ -58,8 +60,8 @@ module.exports = class Generator {
|
||||
// set package.json
|
||||
this.sortPkg()
|
||||
this.files['package.json'] = JSON.stringify(this.pkg, null, 2)
|
||||
// write file tree to disk
|
||||
await writeFileTree(this.context, this.files)
|
||||
// write/update file tree to disk
|
||||
await writeFileTree(this.context, this.files, initialFiles)
|
||||
}
|
||||
|
||||
extractConfigFiles (extractAll, checkExisting) {
|
||||
|
||||
@@ -1,13 +1,27 @@
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const { promisify } = require('util')
|
||||
const unlink = promisify(fs.unlink)
|
||||
const mkdirp = promisify(require('mkdirp'))
|
||||
const write = promisify(fs.writeFile)
|
||||
|
||||
module.exports = function writeFileTree (dir, files) {
|
||||
async function deleteRemovedFiles (directory, newFiles, previousFiles) {
|
||||
// get all files that are not in the new filesystem and are still existing
|
||||
const filesToDelete = Object.keys(previousFiles)
|
||||
.filter(filename => !newFiles[filename])
|
||||
|
||||
// delete each of these files
|
||||
const unlinkPromises = filesToDelete.map(filename => unlink(path.join(directory, filename)))
|
||||
return Promise.all(unlinkPromises)
|
||||
}
|
||||
|
||||
module.exports = async function writeFileTree (dir, files, previousFiles) {
|
||||
if (process.env.VUE_CLI_SKIP_WRITE) {
|
||||
return
|
||||
}
|
||||
if (previousFiles) {
|
||||
await deleteRemovedFiles(dir, files, previousFiles)
|
||||
}
|
||||
return Promise.all(Object.keys(files).map(async (name) => {
|
||||
const filePath = path.join(dir, name)
|
||||
await mkdirp(path.dirname(filePath))
|
||||
|
||||
Reference in New Issue
Block a user