【若川视野 x 源码共读】第10期 | configstore 存储

21 阅读1分钟
import path from "path";
import os from "os";
//graceful-fs是node模块fs的增强版
import fs from "graceful-fs";
//xdg-basedir用于获取XDG Base Directory路径
import { xdgConfig } from "xdg-basedir";
//write-file-atomic这是node的fs.writeFile的一个扩展,它使其操作成为原子性的,并允许您设置所有权(文件的uid/gid)
import writeFileAtomic from "write-file-atomic";
import dotProp from "dot-prop";
import uniqueString from "unique-string";
//configstore轻松地加载和写入配置,而不必考虑在哪里和如何进行
const configDirectory = xdgConfig || path.join(os.tmpdir(), uniqueString());
const permissionError = "You don't have access to this file.";
const mkdirOptions = { mode: 0o0700, recursive: true };
const writeFileOptions = { mode: 0o0600 };

export default class Configstore {
  constructor(id, defaults, options = {}) {
    const pathPrefix = options.globalConfigPath
    ? path.join(id, "config.json")
    : path.join("configstore", `${id}.json`);
    
    this._path = options.configPath || path.join(configDirectory, pathPrefix);
    
    if (defaults) {
      this.all = {
        ...defaults,
        ...this.all,
      };
    }
  }
  
  get all() {
    try {
      return JSON.parse(fs.readFileSync(this._path, "utf8"));
    } catch (error) {
      // Create directory if it doesn't exist  // 目录不存在
      if (error.code === "ENOENT") {
        return {};
      }
      
      // Improve the message of permission errors // 没有读取权限
      if (error.code === "EACCES") {
        error.message = `${error.message}\n${permissionError}\n`;
      }
      
      // Empty the file if it encounters invalid JSON  //json错误
      if (error.name === "SyntaxError") {
        writeFileAtomic.sync(this._path, "", writeFileOptions);
        return {};
      }
      
      throw error;
    }
  }
  
  set all(value) {
    try {
      // Make sure the folder exists as it could have been deleted in the meantime
      fs.mkdirSync(path.dirname(this._path), mkdirOptions);
      
      writeFileAtomic.sync(
        this._path,
        JSON.stringify(value, undefined, "\t"),
        writeFileOptions
      );
    } catch (error) {
      // Improve the message of permission errors
      if (error.code === "EACCES") {
        error.message = `${error.message}\n${permissionError}\n`;
      }
      
      throw error;
    }
  }
  
  get size() {
    return Object.keys(this.all || {}).length;
  }
  
  get(key) {
    return dotProp.get(this.all, key);
  }
  
  set(key, value) {
    const config = this.all;
    
    if (arguments.length === 1) {
      for (const k of Object.keys(key)) {
        dotProp.set(config, k, key[k]);
      }
    } else {
      dotProp.set(config, key, value);
    }
    
    this.all = config;
  }
  
  has(key) {
    return dotProp.has(this.all, key);
  }
  
  delete(key) {
    const config = this.all;
    dotProp.delete(config, key);
    this.all = config;
  }
  
  clear() {
    this.all = {};
  }
  
  get path() {
    return this._path;
  }
}