From ae58b38b64b560754ae4560abfa7107e92215ce7 Mon Sep 17 00:00:00 2001 From: Tim Sandberg Date: Mon, 22 Apr 2019 13:43:15 -0700 Subject: [PATCH 1/2] Allow user to pass in absolute filepath, rather than force home directory location --- index.js | 133 +++++++++++++++++++++++++++---------------------------- 1 file changed, 66 insertions(+), 67 deletions(-) diff --git a/index.js b/index.js index 8fc25a3..6cd8b18 100644 --- a/index.js +++ b/index.js @@ -1,77 +1,76 @@ /* global require, process, module */ -'use strict'; +"use strict"; -var fs = require('fs'), - path = require('path'); +var fs = require("fs"), + path = require("path"); function DotConfig(filepath) { - - var options = null; - - function readFile(filepath) { - - var rawData = '{}'; - - try { - rawData = fs.readFileSync(filepath); - } catch(err) { - if (err.code === 'ENOENT') { - fs.writeFileSync(filepath, rawData); - } else { - throw err; - } - } - - try { - var options = JSON.parse(rawData); - } catch(err) { - err.filepath = filepath; - throw err; - } - - return options; - - } - - this.set = function(key, value) { - options = options || readFile(filepath); - options[key] = value; - fs.writeFileSync(filepath, JSON.stringify(options, null, 2)); - return this; - }; - - this.get = function(key) { - options = options || readFile(filepath); - - if (!key) { - return options; - } - - if (key in options) { - return options[key]; - } else { - return undefined; - } - }; - - this.unset = function(key) { - options = options || readFile(filepath); - delete options[key]; - fs.writeFileSync(filepath, JSON.stringify(options, null, 2)); - }; - + var options = null; + + function readFile(filepath) { + var rawData = "{}"; + + try { + rawData = fs.readFileSync(filepath); + } catch (err) { + if (err.code === "ENOENT") { + fs.writeFileSync(filepath, rawData); + } else { + throw err; + } + } + + try { + var options = JSON.parse(rawData); + } catch (err) { + err.filepath = filepath; + throw err; + } + + return options; + } + + this.set = function(key, value) { + options = options || readFile(filepath); + options[key] = value; + fs.writeFileSync(filepath, JSON.stringify(options, null, 2)); + return this; + }; + + this.get = function(key) { + options = options || readFile(filepath); + + if (!key) { + return options; + } + + if (key in options) { + return options[key]; + } else { + return undefined; + } + }; + + this.unset = function(key) { + options = options || readFile(filepath); + delete options[key]; + fs.writeFileSync(filepath, JSON.stringify(options, null, 2)); + }; } module.exports = module.exports || {}; module.exports.file = function(filename) { - - if (!filename || typeof filename !== typeof '') { - throw new Error('Invalid filename'); - } - - var homedir = process.env.HOME || process.env.USERPROFILE, - filepath = path.join(homedir, filename); - - return new DotConfig(filepath); + if (!filename || typeof filename !== typeof "") { + throw new Error("Invalid filename"); + } + var filepath; + if (!path.isAbsolute(filename)) { + var homedir = process.env.HOME || process.env.USERPROFILE; + filepath = path.join(homedir, filename); + } else { + filepath = filename; + } + + return new DotConfig(filepath); }; From 68c76cc7bb4702b444392eec40d7b37047c08731 Mon Sep 17 00:00:00 2001 From: Tim Sandberg Date: Mon, 22 Apr 2019 13:48:24 -0700 Subject: [PATCH 2/2] Update readme --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e807fb4..abf5059 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,14 @@ settings.unset('username'); ``` +Settings file location can be customized by passing in an absolute filepath: + +```js +// Store settings in the process root. Useful for directory-specific configurations. +var path = require('path') +var settings = require('user-settings').file(path.resolve(process.cwd(), '.myAppSettings)) +``` + ## Home directory location -The home directory used for Unix systems is `process.env.HOME` and `process.env.USERPROFILE` for Windows. \ No newline at end of file +The home directory used for Unix systems is `process.env.HOME` and `process.env.USERPROFILE` for Windows.