Skip to content

Commit

Permalink
handle not being able to find the users home directory
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 13, 2015
1 parent 8116a2f commit ec77240
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
20 changes: 14 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@ var path = require('path');
var userHome = require('user-home');
var env = process.env;

exports.data = env.XDG_DATA_HOME || path.join(userHome, '.local', 'share');
exports.data = env.XDG_DATA_HOME ||
(userHome ? path.join(userHome, '.local', 'share') : null);

exports.config = env.XDG_CONFIG_HOME || path.join(userHome, '.config');
exports.config = env.XDG_CONFIG_HOME ||
(userHome ? path.join(userHome, '.config') : null);

exports.cache = env.XDG_CONFIG_HOME || path.join(userHome, '.cache');
exports.cache = env.XDG_CONFIG_HOME || (userHome ? path.join(userHome, '.cache') : null);

exports.runtime = env.XDG_RUNTIME_DIR;
exports.runtime = env.XDG_RUNTIME_DIR || null;

exports.dataDirs = (env.XDG_DATA_DIRS || '/usr/local/share/:/usr/share/').split(':');
exports.dataDirs.unshift(exports.data);

if (exports.data) {
exports.dataDirs.unshift(exports.data);
}

exports.configDirs = (env.XDG_CONFIG_DIRS || '/etc/xdg').split(':');
exports.configDirs.unshift(exports.config);

if (exports.config) {
exports.configDirs.unshift(exports.config);
}
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ xdgBasedir.dataDirs

## API

The properties `.data`, `.config`, `.cache`, `.runtime` will return `null` in the uncommon case that both the XDG environment variable is not set and the users home directory can't be found. You need to handle this case. A common solution is to [fall back to a temp directory](https://github.com/yeoman/configstore/blob/b82690fc401318ad18dcd7d151a0003a4898a314/index.js#L15).

### .data

Directory for user specific data files.
Expand Down

0 comments on commit ec77240

Please sign in to comment.