From 14130495dfa33a9dacf8bb03b3124f86ce8618ba Mon Sep 17 00:00:00 2001 From: Simon Kjellberg Date: Sat, 2 Jul 2016 21:37:02 +0200 Subject: [PATCH] Make suffix configurable (#3) Fixes #2 --- index.js | 10 +++++++--- readme.md | 29 ++++++++++++++++++++++++++++- test.js | 39 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 71 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 90aabaa..e9aa484 100644 --- a/index.js +++ b/index.js @@ -44,13 +44,17 @@ const linux = name => { }; }; -module.exports = name => { +module.exports = (name, opts) => { if (typeof name !== 'string') { throw new TypeError(`Expected string, got ${typeof name}`); } - // add suffix to prevent possible conflict with native apps - name += '-nodejs'; + opts = Object.assign({suffix: 'nodejs'}, opts); + + if (opts.suffix) { + // add suffix to prevent possible conflict with native apps + name += `-${opts.suffix}`; + } if (process.platform === 'darwin') { return macos(name); diff --git a/readme.md b/readme.md index 4055e8b..e7f3de8 100644 --- a/readme.md +++ b/readme.md @@ -22,11 +22,27 @@ paths.data; paths.config //=> '/home/sindresorhus/.config/MyApp-nodejs' ``` +### You can optionally pass a custom suffix +```js +const paths = envPaths('MyApp', {suffix: 'electron'}); + +paths.data; +//=> '/home/sindresorhus/.local/share/MyApp-electron' +``` + +### ...or disable it completely + +```js +const paths = envPaths('MyApp', {suffix: ''}); + +paths.data; +//=> '/home/sindresorhus/.local/share/MyApp' +``` ## API -### paths = envPaths(name) +### paths = envPaths(name, [options]) #### name @@ -34,6 +50,17 @@ Type: `string` Name of your project. Used to generate the paths. +#### options + +##### suffix + +Type: `string` +Default: `nodejs` + +Optional suffix appended to the project name, to avoid conflicts with native +apps using the same name. Pass an empty string to disable. + + ### paths.data Directory for data files. diff --git a/test.js b/test.js index fc96255..b057fd8 100644 --- a/test.js +++ b/test.js @@ -1,13 +1,46 @@ import test from 'ava'; import m from './'; -test(t => { +test('default options', t => { const name = 'unicorn'; const paths = m(name); + console.log(`name: '${name}' opts = undefined`); + + Object.keys(paths).forEach(key => { + const val = paths[key]; + console.log(` ${key}: ${val}`); + t.true(val.endsWith(`${name}-nodejs`)); + }); + + console.log('\n'); +}); + +test('custom suffix', t => { + const name = 'unicorn'; + const opts = {suffix: 'horn'}; + const paths = m(name, opts); + + console.log(`name: '${name}' opts = {suffix: '${opts.suffix}'}`); + + Object.keys(paths).forEach(key => { + const val = paths[key]; + console.log(` ${key}: ${val}`); + t.true(val.endsWith(`${name}-${opts.suffix}`)); + }); + + console.log('\n'); +}); + +test('no suffix', t => { + const name = 'unicorn'; + const paths = m(name, {suffix: ''}); + + console.log(`name: '${name}' opts = {suffix: ''}`); + Object.keys(paths).forEach(key => { const val = paths[key]; - console.log(`${key}: ${val}`); - t.true(val.includes(name)); + console.log(` ${key}: ${val}`); + t.true(val.endsWith(`${name}`)); }); });