-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
index.js
118 lines (96 loc) · 2.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import path from 'node:path';
import os from 'node:os';
import fs from 'graceful-fs';
import {xdgConfig} from 'xdg-basedir';
import {writeFileSync} from 'atomically';
import {
getProperty,
setProperty,
hasProperty,
deleteProperty,
} from 'dot-prop';
function getConfigDirectory(id, globalConfigPath) {
const pathPrefix = globalConfigPath
? path.join(id, 'config.json')
: path.join('configstore', `${id}.json`);
const configDirectory = xdgConfig ?? fs.mkdtempSync(fs.realpathSync(os.tmpdir()) + path.sep);
return path.join(configDirectory, pathPrefix);
}
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 = {}) {
this._path = options.configPath ?? getConfigDirectory(id, options.globalConfigPath);
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') {
writeFileSync(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);
writeFileSync(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 getProperty(this.all, key);
}
set(key, value) {
const config = this.all;
if (arguments.length === 1) {
for (const k of Object.keys(key)) {
setProperty(config, k, key[k]);
}
} else {
setProperty(config, key, value);
}
this.all = config;
}
has(key) {
return hasProperty(this.all, key);
}
delete(key) {
const config = this.all;
deleteProperty(config, key);
this.all = config;
}
clear() {
this.all = {};
}
get path() {
return this._path;
}
}