Skip to content

feat(invoke): allow invoke to replace/rename/delete files #1049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/@vue/cli/__tests__/invoke.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
6 changes: 4 additions & 2 deletions packages/@vue/cli/lib/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,17 @@ 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
await this.resolveFiles()
// 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) {
Expand Down
16 changes: 15 additions & 1 deletion packages/@vue/cli/lib/util/writeFileTree.js
Original file line number Diff line number Diff line change
@@ -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))
Expand Down