Force overwrite file
Asked Answered
F

2

6

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)

Fifi answered 3/5, 2016 at 14:20 Comment(0)
S
13

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.

Scorper answered 3/5, 2016 at 18:18 Comment(0)
M
0

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');
});
Mas answered 17/2, 2020 at 3:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.