批量添加copyRight脚本

238 阅读1分钟

项目上线前需要添加给每个文件添加copyRight信息,本来想写bat脚本,看了下有点难,写了个node版本的,记录下

var gFilePath = ".";

const fs = require("fs");
// 递归读取文件
const readdirp = require("readdirp");

const result = dirContentReplace(gFilePath);

function dirContentReplace(filePath) {
  readdirp(filePath, {
    fileFilter: ["*.js", "*.ts"],
    directoryFilter: ["!.git", "!.idea", "!node_modules"],
  }).on("data", (entry) => {
    appendCopyright(entry.path);
  });
}

function appendCopyright(path) {
  fs.readFile(path, "utf8", function(err, data) {
    var copyright = `/*\n * Copyright (c) 2021 company. All rights reserved.\n */\n`;
    const result = copyright + data;
    //writeFile改写文件内容
    fs.writeFile(path, result, "utf8", function(err) {
      if (err) return console.log(err);
    });
  });
}