diff options
author | Rebecca Turner <me@re-becca.org> | 2018-04-20 18:26:37 -0700 |
---|---|---|
committer | Rebecca Turner <me@re-becca.org> | 2018-05-24 23:24:45 -0700 |
commit | 468ab4519e1b92473acefb22801497a1af6aebae (patch) | |
tree | bdac1d062cd4b094bde3a21147bab5d82c792ece /deps/npm/node_modules/configstore/index.js | |
parent | ac8226115e2192a7a46ba07789fa5136f74223e1 (diff) | |
download | android-node-v8-468ab4519e1b92473acefb22801497a1af6aebae.tar.gz android-node-v8-468ab4519e1b92473acefb22801497a1af6aebae.tar.bz2 android-node-v8-468ab4519e1b92473acefb22801497a1af6aebae.zip |
deps: upgrade npm to 6.1.0
PR-URL: https://github.com/nodejs/node/pull/20190
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Diffstat (limited to 'deps/npm/node_modules/configstore/index.js')
-rw-r--r-- | deps/npm/node_modules/configstore/index.js | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/deps/npm/node_modules/configstore/index.js b/deps/npm/node_modules/configstore/index.js new file mode 100644 index 0000000000..fb944204be --- /dev/null +++ b/deps/npm/node_modules/configstore/index.js @@ -0,0 +1,106 @@ +'use strict'; +const path = require('path'); +const os = require('os'); +const fs = require('graceful-fs'); +const makeDir = require('make-dir'); +const xdgBasedir = require('xdg-basedir'); +const writeFileAtomic = require('write-file-atomic'); +const dotProp = require('dot-prop'); +const uniqueString = require('unique-string'); + +const configDir = xdgBasedir.config || path.join(os.tmpdir(), uniqueString()); +const permissionError = 'You don\'t have access to this file.'; +const makeDirOptions = {mode: 0o0700}; +const writeFileOptions = {mode: 0o0600}; + +class Configstore { + constructor(id, defaults, opts) { + opts = opts || {}; + + const pathPrefix = opts.globalConfigPath ? + path.join(id, 'config.json') : + path.join('configstore', `${id}.json`); + + this.path = path.join(configDir, pathPrefix); + this.all = Object.assign({}, defaults, this.all); + } + + get all() { + try { + return JSON.parse(fs.readFileSync(this.path, 'utf8')); + } catch (err) { + // Create dir if it doesn't exist + if (err.code === 'ENOENT') { + makeDir.sync(path.dirname(this.path), makeDirOptions); + return {}; + } + + // Improve the message of permission errors + if (err.code === 'EACCES') { + err.message = `${err.message}\n${permissionError}\n`; + } + + // Empty the file if it encounters invalid JSON + if (err.name === 'SyntaxError') { + writeFileAtomic.sync(this.path, '', writeFileOptions); + return {}; + } + + throw err; + } + } + + set all(val) { + try { + // Make sure the folder exists as it could have been deleted in the meantime + makeDir.sync(path.dirname(this.path), makeDirOptions); + + writeFileAtomic.sync(this.path, JSON.stringify(val, null, '\t'), writeFileOptions); + } catch (err) { + // Improve the message of permission errors + if (err.code === 'EACCES') { + err.message = `${err.message}\n${permissionError}\n`; + } + + throw err; + } + } + + get size() { + return Object.keys(this.all || {}).length; + } + + get(key) { + return dotProp.get(this.all, key); + } + + set(key, val) { + 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, val); + } + + 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 = {}; + } +} + +module.exports = Configstore; |