Skip to content

Commit

Permalink
Add projectSuffix option (#52)
Browse files Browse the repository at this point in the history
Fixes #35


Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
niktekusho and sindresorhus committed Jan 18, 2019
1 parent da4ce66 commit 949586a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ class Conf {

options = Object.assign({
configName: 'config',
fileExtension: 'json'
fileExtension: 'json',
projectSuffix: 'nodejs'
}, options);

if (!options.cwd) {
options.cwd = envPaths(options.projectName).config;
options.cwd = envPaths(options.projectName, {suffix: options.projectSuffix}).config;
}

this.events = new EventEmitter();
Expand Down
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ Extension of the config file.

You would usually not need this, but could be useful if you want to interact with a file with a custom file extension that can be associated with your app. These might be simple save/export/preference files that are intended to be shareable or saved outside of the app.

#### projectSuffix

Type: `string`<br>
Default: `nodejs`

**You most likely don't need this. Please don't use it unless you really have to.**

Suffix appended to `projectName` during config file creation to avoid name conflicts with native apps.

You can pass an empty string to remove the suffix.

For example, on macOS, the config file will be stored in the `~/Library/Preferences/foo-nodejs` directory, where `foo` is the `projectName`.

### Instance

You can use [dot-notation](https://github.com/sindresorhus/dot-prop) in a `key` to access nested properties.
Expand Down
24 changes: 24 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,30 @@ test('`configName` option', t => {
t.is(path.basename(conf.path, '.json'), configName);
});

test('no `suffix` option', t => {
const conf = new Conf();
t.true(conf.path.includes('-nodejs'));
});

test('with `suffix` option set to empty string', t => {
const projectSuffix = '';
const projectName = 'conf-temp1-project';
const conf = new Conf({projectSuffix, projectName});
const configPathSegments = conf.path.split(path.sep);
const configRootIndex = configPathSegments.findIndex(segment => segment === projectName);
t.true(configRootIndex >= 0 && configRootIndex < configPathSegments.length);
});

test('with `projectSuffix` option set to non-empty string', t => {
const projectSuffix = 'new-projectSuffix';
const projectName = 'conf-temp2-project';
const conf = new Conf({projectSuffix, projectName});
const configPathSegments = conf.path.split(path.sep);
const expectedRootName = `${projectName}-${projectSuffix}`;
const configRootIndex = configPathSegments.findIndex(segment => segment === expectedRootName);
t.true(configRootIndex >= 0 && configRootIndex < configPathSegments.length);
});

test('`fileExtension` option', t => {
const fileExtension = 'alt-ext';
const conf = new Conf({
Expand Down

0 comments on commit 949586a

Please sign in to comment.