Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
The home directory used for Unix systems is `process.env.HOME` and `process.env.USERPROFILE` for Windows.
133 changes: 66 additions & 67 deletions index.js
Original file line number Diff line number Diff line change
@@ -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);
};