node skills

使用execSync模块执行命令行指令
const execSync = require('child_process').execSync;
const GIT_BRANCH_NAME = 'git rev-parse --abbrev-ref HEAD';
const branchName = execSync(GIT_BRANCH_NAME, { encoding: 'utf8' }).replace('\n', '');
如果存在文件则删除文件
fs.existsSync(FILES_LIST_URL) && fs.unlinkSync(FILES_LIST_URL);
同步写文件
fs.writeFileSync(FILES_LIST_URL, content);
同步读文件
const res = fs.readFileSync(path.join(__dirname, 'files_list.json'), { encoding: 'utf8' });
递归删除文件
export function deleteFolder(path) {
  let files = [];
  if (fs.existsSync(path)) {
    files = fs.readdirSync(path);
    files.forEach(function(file, index) {
      let curPath = path + '/' + file;
      if (fs.statSync(curPath).isDirectory()) {
        deleteFolder(curPath);
      } else {
        fs.unlinkSync(curPath);
      }
    });
    fs.rmdirSync(path);
  }
}
递归创建文件夹
fs.mkdir(path.join(__dirname, '../dist/update_dist'), { recursive: true }, err => {
    if (err) throw err;
})
复制文件,读/写 文件
cacheIndexUmi(source_umi_file) {
    fs.copyFile(
      path.join(`${this.ASSETS_URL}/temp`, '/', this.umi_file),
      path.join(`${this.ASSETS_URL}/temp`, '/', source_umi_file),
      err => {
        if (err) {
          console.log(err);
        }
        fs.unlinkSync(path.join(`${this.ASSETS_URL}/temp`, '/', this.umi_file)); // 删除改名之前的
      }
    );
    fs.readFile(path.join(`${this.ASSETS_URL}/temp`, '/', 'index.html'), 'utf8', (err, data) => {
      if (err) {
        console.log(err);
        return;
      }
      const new_data = data.replace(this.umi_file, source_umi_file);
      fs.writeFile(path.join(`${this.ASSETS_URL}/temp`, '/', 'index.html'), new_data, 'utf8', err => {
        if (err) {
          console.log(err);
          return;
        }
        this.copyIt(`${this.ASSETS_URL}/temp/.`, this.ASSETS_URL)
      });
    });
  }
线程复制
copyIt(from, to) {
    child_process.spawn('cp', ['-rf', from, to]); 
}