Skip to content

fix(generator): avoid doing redundant write operations #6011

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 2 commits into from
Dec 20, 2020
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
46 changes: 46 additions & 0 deletions packages/@vue/cli/__tests__/Generator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,52 @@ test('api: addConfigTransform transform vue warn', async () => {
})).toBe(true)
})

test('avoid overwriting files that have not been modified', async () => {
const generator = new Generator('/', {
plugins: [
{
id: 'test1',
apply: (api, options) => {
api.render((files, render) => {
files['foo.js'] = render('foo()')
})
}
}
],
files: {
// skip writing to this file
'existFile.js': 'existFile()'
}
})

await generator.generate()

expect(fs.readFileSync('/foo.js', 'utf-8')).toMatch('foo()')
expect(fs.existsSync('/existFile.js')).toBe(false)
})

test('overwrite files that have been modified', async () => {
const generator = new Generator('/', {
plugins: [
{
id: 'test1',
apply: (api, options) => {
api.render((files, render) => {
files['existFile.js'] = render('foo()')
})
}
}
],
files: {
'existFile.js': 'existFile()'
}
})

await generator.generate()

expect(fs.readFileSync('/existFile.js', 'utf-8')).toMatch('foo()')
})

test('extract config files', async () => {
const configs = {
vue: {
Expand Down
26 changes: 24 additions & 2 deletions packages/@vue/cli/lib/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ const ensureEOL = str => {
return str
}

/**
* Collect created/modified files into set
* @param {Record<string,string|Buffer>} files
* @param {Set<string>} set
*/
const watchFiles = (files, set) => {
return new Proxy(files, {
set (target, key, value, receiver) {
set.add(key)
return Reflect.set(target, key, value, receiver)
},
deleteProperty (target, key) {
set.delete(key)
return Reflect.deleteProperty(target, key)
}
})
}

module.exports = class Generator {
constructor (context, {
pkg = {},
Expand Down Expand Up @@ -101,7 +119,11 @@ module.exports = class Generator {
// for conflict resolution
this.depSources = {}
// virtual file tree
this.files = files
this.files = Object.keys(files).length
// when execute `vue add/invoke`, only created/modified files are written to disk
? watchFiles(files, this.filesModifyRecord = new Set())
// all files need to be written to disk
: files
this.fileMiddlewares = []
this.postProcessFilesCbs = []
// exit messages
Expand Down Expand Up @@ -177,7 +199,7 @@ module.exports = class Generator {
this.sortPkg()
this.files['package.json'] = JSON.stringify(this.pkg, null, 2) + '\n'
// write/update file tree to disk
await writeFileTree(this.context, this.files, initialFiles)
await writeFileTree(this.context, this.files, initialFiles, this.filesModifyRecord)
}

extractConfigFiles (extractAll, checkExisting) {
Expand Down
10 changes: 9 additions & 1 deletion packages/@vue/cli/lib/util/writeFileTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@ function deleteRemovedFiles (directory, newFiles, previousFiles) {
}))
}

module.exports = async function writeFileTree (dir, files, previousFiles) {
/**
*
* @param {string} dir
* @param {Record<string,string|Buffer>} files
* @param {Record<string,string|Buffer>} [previousFiles]
* @param {Set<string>} [include]
*/
module.exports = async function writeFileTree (dir, files, previousFiles, include) {
if (process.env.VUE_CLI_SKIP_WRITE) {
return
}
if (previousFiles) {
await deleteRemovedFiles(dir, files, previousFiles)
}
Object.keys(files).forEach((name) => {
if (include && !include.has(name)) return
const filePath = path.join(dir, name)
fs.ensureDirSync(path.dirname(filePath))
fs.writeFileSync(filePath, files[name])
Expand Down