I'm using this in my generator
this.fs.copy(this.templatePath('index.html'),this.destinationPath('index.html') );
I want it to skip the overwrite confirmation everytime it finds a confilict (like force overwrite option)
I'm using this in my generator
this.fs.copy(this.templatePath('index.html'),this.destinationPath('index.html') );
I want it to skip the overwrite confirmation everytime it finds a confilict (like force overwrite option)
This is not possible. Yeoman will always ask the user for confirmation before overwriting a file. This is a contract the tool takes with its users: it won't overwrite a file without their acknowledgement.
As a user, if you trust your generator, you can run it with the --force
flag to automatically overwrite the conflicting files.
If this is a must to do, you can still force
overwriting files by using fs.copyFile(), fs.writeFile() or fs.writeFileSync() function from fs
. both functions write data to a file, replacing the file if it already exists.
const yosay = require("yosay");
....
fs.writeFile(filePath, fileContent, 'utf8', err => yosay(err));
If you get error File already exists
, you may need to set explicit write
flag in the the third param to make it work:
fs.writeFile(filePath,fileContent,{encoding:'utf8',flag:'w'}, err => yosay(err));
OR
const fs = require('fs');
// destination.txt will be created or overwritten by default.
fs.copyFile('source.txt', 'destination.txt', (err) => {
if (err) throw err;
console.log('source.txt was copied to destination.txt');
});
© 2022 - 2024 — McMap. All rights reserved.