2 const path = require('path');
3 const os = require('os');
4 const fs = require('graceful-fs');
5 const makeDir = require('make-dir');
6 const xdgBasedir = require('xdg-basedir');
7 const writeFileAtomic = require('write-file-atomic');
8 const dotProp = require('dot-prop');
9 const uniqueString = require('unique-string');
11 const configDir = xdgBasedir.config || path.join(os.tmpdir(), uniqueString());
12 const permissionError = 'You don\'t have access to this file.';
13 const makeDirOptions = {mode: 0o0700};
14 const writeFileOptions = {mode: 0o0600};
17 constructor(id, defaults, opts) {
20 const pathPrefix = opts.globalConfigPath ?
21 path.join(id, 'config.json') :
22 path.join('configstore', `${id}.json`);
24 this.path = path.join(configDir, pathPrefix);
25 this.all = Object.assign({}, defaults, this.all);
30 return JSON.parse(fs.readFileSync(this.path, 'utf8'));
32 // Create dir if it doesn't exist
33 if (err.code === 'ENOENT') {
34 makeDir.sync(path.dirname(this.path), makeDirOptions);
38 // Improve the message of permission errors
39 if (err.code === 'EACCES') {
40 err.message = `${err.message}\n${permissionError}\n`;
43 // Empty the file if it encounters invalid JSON
44 if (err.name === 'SyntaxError') {
45 writeFileAtomic.sync(this.path, '', writeFileOptions);
55 // Make sure the folder exists as it could have been deleted in the meantime
56 makeDir.sync(path.dirname(this.path), makeDirOptions);
58 writeFileAtomic.sync(this.path, JSON.stringify(val, null, '\t'), writeFileOptions);
60 // Improve the message of permission errors
61 if (err.code === 'EACCES') {
62 err.message = `${err.message}\n${permissionError}\n`;
70 return Object.keys(this.all || {}).length;
74 return dotProp.get(this.all, key);
78 const config = this.all;
80 if (arguments.length === 1) {
81 for (const k of Object.keys(key)) {
82 dotProp.set(config, k, key[k]);
85 dotProp.set(config, key, val);
92 return dotProp.has(this.all, key);
96 const config = this.all;
97 dotProp.delete(config, key);
106 module.exports = Configstore;