强制覆盖文件

时间:2016-05-03 14:20:53

标签: yeoman yeoman-generator

我在我的发电机中使用它

this.fs.copy(this.templatePath('index.html'),this.destinationPath('index.html') );

我想让它在每次发现一个confilict时都跳过覆盖确认(比如强制覆盖选项)

2 个答案:

答案 0 :(得分:9)

这是不可能的。在覆盖文件之前,Yeoman将始终要求用户进行确认。这是该工具与其用户签订的合同:它不会在没有确认的情况下覆盖文件。

作为用户,如果您信任您的生成器,则可以使用--force标志运行它以自动覆盖冲突文件。

答案 1 :(得分:0)

如果必须这样做,则仍可以使用force中的fs.copyFile()fs.writeFile()fs.writeFileSync()函数fs覆盖文件。这两个函数都将数据写入文件,如果文件已经存在,则将其替换。

const yosay = require("yosay");

....

fs.writeFile(filePath, fileContent, 'utf8', err => yosay(err)); 

如果出现错误File already exists,则可能需要在第三个参数中设置显式write标志以使其起作用:

fs.writeFile(filePath,fileContent,{encoding:'utf8',flag:'w'}, err => yosay(err));

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');
});
相关问题