From 949586aa9bb54c2c77e54a15c4248fb456a0e869 Mon Sep 17 00:00:00 2001 From: Nicola Dal Maso Date: Fri, 18 Jan 2019 08:03:25 +0100 Subject: [PATCH] Add `projectSuffix` option (#52) Fixes #35 Co-authored-by: Sindre Sorhus --- index.js | 5 +++-- readme.md | 13 +++++++++++++ test.js | 24 ++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index b79f75f..f897f11 100644 --- a/index.js +++ b/index.js @@ -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(); diff --git a/readme.md b/readme.md index c53cc9b..96d7808 100644 --- a/readme.md +++ b/readme.md @@ -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`
+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. diff --git a/test.js b/test.js index 2c2afc6..cb729aa 100644 --- a/test.js +++ b/test.js @@ -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({