const fs = require("fs");

const asd = /id: (["'][\w\d]+["']),(\n?\s+(.+)){0,2}\n?\s+params:\s/;
const asdg = /id: ["'][\w\d]+["'],(\n?\s+(.+)){0,2}\n?\s+params:\s/g;

class Main {
  pathList = [];
  pathListLength = 0;

  read_dir(path) {
    this.__read(path);
    this.handleFile();
  }

  __read(path) {
    fs.readdirSync(path).forEach(filename => {
      let sub_path = path + '/' + filename;
      let stat = fs.statSync(sub_path);
      if (stat.isDirectory()) {
        this.__read(sub_path);
      } else if (stat.isFile()) {
        if (!this.pathList.some(tt => tt === sub_path)) {
          this.pathList.push(sub_path);
        }
      }
    });
    this.pathListLength = this.pathList.length
  }

  handleFile() {
    let path = this.pathList.pop();
    let content = fs.readFileSync(path, {
      encoding: 'utf-8',
    });
    this.appendProps(path, content);
    console.log(`正在替换${this.pathListLength - this.pathList.length}/${this.pathListLength}`)
    if (this.pathList.length > 0) this.handleFile();
  }

  appendProps(path, content) {
    if (!asd.test(content)) { return; }
    let newContent = content.replace(asdg, (substring) => {
      return substring.replace('id:', "num:");
    });
    fs.writeFileSync(path, newContent);
  }

}

let main = new Main();
main.read_dir('/Users/mlie/Documents/prolog/upcloud-wms-hnsy-web/src/views/kzyWMS');
main.read_dir('/Users/mlie/Documents/prolog/upcloud-wms-hnsy-web/src/views/EIS');