configstore源码分析

https://github.com/yeoman/configstore

configstore轻松地加载和写入配置,而不必考虑在哪里和如何进行

使用

import Configstore from 'configstore';


// Create a Configstore instance.
const config = new Configstore('configstore-test', {foo: 'bar'});

console.log(config.get('foo'));
//=> 'bar'

config.set('awesome', true);
console.log(config.get('awesome'));
//=> true

// Use dot-notation to access nested properties.
config.set('bar.baz', true);
console.log(config.get('bar'));
//=> {baz: true}

config.delete('awesome');
console.log(config.get('awesome'));
//=> undefined

源码分析

  • 源码路径

所以只需要关注下index.js即可
image.png

  • 源码分析

初始化工作:
graceful-fs是node模块fs的增强版,xdg-basedir用于获取XDG Base Directory路径。write-file-atomic这是node的fs.writeFile的一个扩展,它使其操作成为原子性的,并允许您设置所有权(文件的uid/gid)

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';

// '/Users/xxx/.config'
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};

configstore包的核心是dot-prop这个包,用法如下:

const dotProp = require('dot-prop');
 
// Getter
dotProp.get({foo: {bar: 'unicorn'}}, 'foo.bar');
//=> 'unicorn'
 
dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value');
//=> 'default value'
 
// Setter
const object = {foo: {bar: 'a'}};
dotProp.set(object, 'foo.bar', 'b');
console.log(object);
//=> {foo: {bar: 'b'}}
 
const foo = dotProp.set({}, 'foo.bar', 'c');
console.log(foo);
//=> {foo: {bar: 'c'}}
 
// Has
dotProp.has({foo: {bar: 'unicorn'}}, 'foo.bar');
//=> true
 
// Deleter
const object = {foo: {bar: 'a'}};
dotProp.delete(object, 'foo.bar');
console.log(object);
//=> {foo: {}}
 
object.foo.bar = {x: 'y', y: 'x'};
dotProp.delete(object, 'foo.bar.x');
console.log(object);
//=> {foo: {bar: {y: 'x'}}}

Configstore类:
构造函数主要做的是根据传入的id,设置文件的存储路径_path;如果有默认的存取内容defaults,就去创建对应文件,写入默认内容;不存在默认内容时,文件暂时不会创建
对应的实例方法set、get、delete、has内部都是调用dot-prop这个包的相关方法,只不过就是在set时可以传入单个对象参数,设置多个key/value值
Configstore类的核心主要是围绕实例属性all的处理,设置了all的存值函数和取值函数
get all取值函数主要做的是读取对应_path的内容,如果路径不存在或者文件内容不符合json格式,抛出对应的错误
set all存值函数主要是创建文件,写入内容,如果无写入权限,抛出对应的错误

export default class Configstore {
	constructor(id, defaults, options = {}) {
		const pathPrefix = options.globalConfigPath ?
			path.join(id, 'config.json') :
			path.join('configstore', `${id}.json`);
		// /Users/xxx/.config/configstore/configstore-test.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') { // json错误
				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;
	}
}

总结

这个包的代码很简洁,利用prop 的get set对文件的读取和写入进行了统一的错误拦截,然后充分利用dot-prop这个包对对象进行操作

posted @ 2021-10-28 16:49  raindi  阅读(229)  评论(0编辑  收藏  举报