# configstore 存储
# 环境准备
git clone https://github.com/yeoman/configstore.git
# package.json
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
# index.js
源码
import path from 'path';
import os from 'os';
import fs from 'graceful-fs';
import {xdgConfig} from 'xdg-basedir';
import writeFileAtomic from 'write-file-atomic';
import dotProp from 'dot-prop';
import uniqueString from 'unique-string';
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
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;
}
}
# 关于引入的包
import path from 'path';
import os from 'os';
import fs from 'graceful-fs';
import { xdgConfig } from 'xdg-basedir';
import writeFileAtomic from 'write-file-atomic';
import dotProp from 'dot-prop';
import uniqueString from 'unique-string';
- path: 用于处理文件和目录的路径。
- os: 提供了与操作系统相关的使用方法和属性。
- graceful-fs: 三方的包,优化原生的fs模块,规范不同操作系统或环境的行为
- xdg-basedir:获取 XDG 基本目录路径,<适用于linux系统>
- write-file-atomic: 作为 fs.writeFile的扩展,使得写入操作具有原子性且允许设置文件所有权
- dot-prop:用扁平的方式去获取, 设置或者删除嵌套对象中的一个属性
- unique-string:返回一个 32 个字符的唯一字符串
# 几个变量
// 获取 xdgConfig的路径,如果没有则在tmpdir随机生成一个
const configDirectory = xdgConfig || path.join(os.tmpdir(), uniqueString());
const permissionError = 'You don\'t have access to this file.';
// rwx
const mkdirOptions = {mode: 0o0700, recursive: true};
// rw-
const writeFileOptions = {mode: 0o0600};
# constructor
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 or set all
get all() {
try {
// 将文件 path 内容读出,并将可能出现的错误呈现出来
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
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 or set key
get(key) {
// 借助 dotProp 从 this.all 获取数据
return dotProp.get(this.all, key);
}
set(key, value) {
// 设置值
const config = this.all;
// length === 1 以 对象的方式进行 设置
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;
}
# detele has clear
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
get path() {
return this._path;
}
# 参考资料
← 第九期 第十一期 玩具vite →