FS 模块使用 Promise 改造

9 min read

封装 fs的工具类

const fs = window.require("fs").promises;

const fileHelper = {
  readFile: (path) => {
    return fs.readFile(path, { encoding: "utf8" });
  },
  writeFile: (path, content) => {
    return fs.writeFile(path, content, { encoding: "utf8" });
  },
  renameFile: (path, newPath) => {
    return fs.rename(path, newPath);
  },
  deleteFile: (path) => {
    return fs.unlink(path);
  },
};

export default fileHelper;

使用

fileHelper.readFile(currentFile.path).then(value => {})