Skip to content

Commit

Permalink
Make suffix configurable (#3)
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
simonkberg authored and sindresorhus committed Jul 2, 2016
1 parent 88a5908 commit 1413049
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
10 changes: 7 additions & 3 deletions index.js
Expand Up @@ -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);
Expand Down
29 changes: 28 additions & 1 deletion readme.md
Expand Up @@ -22,18 +22,45 @@ 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

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.
Expand Down
39 changes: 36 additions & 3 deletions 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}`));
});
});

0 comments on commit 1413049

Please sign in to comment.