Skip to content

Commit

Permalink
refactor/polish ~ npx prettier . --write re-format
Browse files Browse the repository at this point in the history
  • Loading branch information
rivy committed Dec 15, 2020
1 parent 18567d0 commit a4fe2f4
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 87 deletions.
10 changes: 5 additions & 5 deletions .appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# spell-checker:ignore repo

version: "{build} ~ {branch}"
version: '{build} ~ {branch}'

branches:
except:
- gh-pages

environment:
matrix:
- nodejs_version: "12"
- nodejs_version: "10"
- nodejs_version: "8"
- nodejs_version: "6"
- nodejs_version: '12'
- nodejs_version: '10'
- nodejs_version: '8'
- nodejs_version: '6'
# - nodejs_version: "4"

# platform:
Expand Down
51 changes: 26 additions & 25 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module.exports = {
"env": {
// "browser": true,
"commonjs": true,
"es6": true,
"node": true
},
"ignorePatterns": ['.eslintrc.js', '.nyc_output', 'build', 'coverage', 'dist', 'node_modules'],
"plugins": ['import'],
"extends": [
env: {
// "browser": true,
commonjs: true,
es6: true,
node: true,
},
ignorePatterns: ['.eslintrc.js', '.nyc_output', 'build', 'coverage', 'dist', 'node_modules'],
plugins: ['import'],
extends: [
'eslint:recommended',
// 'plugin:@typescript-eslint/recommended',
'plugin:eslint-comments/recommended',
Expand All @@ -16,26 +16,27 @@ module.exports = {
'prettier',
// 'prettier/@typescript-eslint',
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
parserOptions: {
ecmaVersion: 2018,
},
rules: {
'@typescript-eslint/explicit-module-boundary-types': 'off',
'eslint-comments/disable-enable-pair': ['error', { allowWholeFile: true }],
'eslint-comments/no-unused-disable': 'error',
'import/order': ['error', { 'newlines-between': 'always', alphabetize: { order: 'asc' } }],
'sort-imports': ['error', { ignoreDeclarationSort: true, ignoreCase: true }],
},
"overrides": [
{ // "test.js" (for `ava`) is in module format (ESM)
"files": [ "unit.test.js" ],
"parserOptions": {
"sourceType": "module"
}
}
]
overrides: [
{
// "test.js" (for `ava`) is in module format (ESM)
files: ['unit.test.js'],
parserOptions: {
sourceType: 'module',
},
},
],
};
37 changes: 23 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,29 @@ npm install xdg-portable
## Usage

```js
const xdg = require('xdg-portable')
const xdg = require('xdg-portable');

const locatePath = require('locatePath')
const mkdirp = require('mkdirp')
const locatePath = require('locatePath');
const mkdirp = require('mkdirp');

configDir = xdg.config()
configDir = xdg.config();
//(mac)=> '/Users/rivy/Library/Preferences'
//(nix)=> '/home/rivy/.config'
//(win)=> 'C:\\Users\\rivy\\AppData\\Roaming\\xdg.config'

dataDirs = xdg.dataDirs()
dataDirs = xdg.dataDirs();
//(mac)=> ['/Users/rivy/Library/Application Support']
//(nix)=> ['/home/rivy/.local/share', '/usr/local/share/', '/usr/share/']
//(win)=> ['C:\\Users\\rivy\\AppData\\Roaming\\xdg.data']

stateDir = xdg.state()
stateDir = xdg.state();
//(mac)=> '/Users/rivy/Library/State'
//(nix)=> '/home/rivy/.local/state'
//(win)=> 'C:\\Users\\rivy\\AppData\\Local\\xdg.state'

mkdirp.sync(configDir, 0o700);

const dataDir = locatePath.sync(dataDirs) || dataDirs[0]
const dataDir = locatePath.sync(dataDirs) || dataDirs[0];
mkdirp.sync(dataDir, 0o700);

mkdirp.sync(stateDir, 0o700);
Expand All @@ -74,7 +74,7 @@ mkdirp.sync(stateDir, 0o700);
#### `require('xdg-portable'): XDGPortable()`

```js
const xdg = require('xdg-portable')
const xdg = require('xdg-portable');
```

The object returned by the module constructor is an XDGPortable Function object, augmented with attached methods. When called directly (eg, `const p = xdg()`), it returns a newly constructed XDGPortable object. Since the XDGPortable object contains no instance state, all constructed objects will be functionally identical.
Expand All @@ -83,15 +83,15 @@ The object returned by the module constructor is an XDGPortable Function object,

All module methods return platform-compatible path strings.

The returned paths are simple strings and are *not* guaranteed to exist. The application is responsible for construction of the directories when needed. If needed, [`make-dir`](https://www.npmjs.com/package/make-dir) or [`mkdirp`](https://www.npmjs.com/package/mkdirp) can be used to create the directories.
The returned paths are simple strings and are _not_ guaranteed to exist. The application is responsible for construction of the directories when needed. If needed, [`make-dir`](https://www.npmjs.com/package/make-dir) or [`mkdirp`](https://www.npmjs.com/package/mkdirp) can be used to create the directories.

#### `xdg.cache(): string`

Returns the directory for user-specific non-essential (cached) data files

> Deletion of the data contained here might cause the application to slow down.
This location would be analogous to */var/cache* for *nix.
This location would be analogous to _/var/cache_ for \*nix.

`%LocalAppData%\xdg.cache` is the default for the windows platform.

Expand All @@ -101,7 +101,7 @@ Returns the directory for user-specific configuration files

> Deletion of the data contained here might require the user to reconfigure the application.
This location would be analogous to */etc* for *nix.
This location would be analogous to _/etc_ for \*nix.

`%AppData%\xdg.config` is the default for the windows platform.

Expand All @@ -111,7 +111,7 @@ Returns the directory for user-specific data files

> Deletion of the data contained here might force the user to restore from backups.
This location would be analogous to */usr/share* for *nix.
This location would be analogous to _/usr/share_ for \*nix.

`%AppData%\xdg.data` is the default for the windows platform.

Expand Down Expand Up @@ -181,30 +181,39 @@ MIT © [Roy Ivy III](https://github.com/rivy), [Sindre Sorhus](https://sindresor
<!-- badge references -->

<!-- [npm-image]: https://img.shields.io/npm/v/xdg-portable.svg?style=flat&label=%E2%81%A3&logo=NPM&logoColor=linen -->

[npm-image]: https://img.shields.io/npm/v/xdg-portable.svg?style=flat
[npm-url]: https://npmjs.org/package/xdg-portable

<!-- [appveyor-image]: https://ci.appveyor.com/api/projects/status/.../branch/master?svg=true -->

[appveyor-image]: https://img.shields.io/appveyor/ci/rivy/js-xdg-portable/master.svg?style=flat&logo=AppVeyor&logoColor=deepskyblue
[appveyor-url]: https://ci.appveyor.com/project/rivy/js-xdg-portable

<!-- [travis-image]: https://travis-ci.org/rivy/js.xdg-portable.svg?branch=master -->
<!-- [travis-image]: https://img.shields.io/travis/rivy/js.xdg-portable/master.svg?style=flat&logo=Travis-CI&logoColor=silver -->

[travis-image]: https://img.shields.io/travis/rivy/js.xdg-portable/master.svg?style=flat&logo=travis
[travis-url]: https://travis-ci.org/rivy/js.xdg-portable

<!-- [coverage-image]: https://img.shields.io/coveralls/github/rivy/xdg-portable/master.svg -->
<!-- [coverage-url]: https://coveralls.io/github/rivy/xdg-portable -->

[coverage-image]: https://img.shields.io/codecov/c/github/rivy/js.xdg-portable/master.svg
[coverage-url]: https://codecov.io/gh/rivy/js.xdg-portable
[downloads-image]: http://img.shields.io/npm/dm/xdg-portable.svg?style=flat
[downloads-url]: https://npmjs.org/package/xdg-portable
[license-image]: https://img.shields.io/npm/l/xdg-portable.svg?style=flat
[license-url]: license

<!-- [repository-image]:https://img.shields.io/badge/%E2%9D%A4-darkcyan?style=flat&logo=github -->
<!-- note: %E2%81%A3 == utf-8 sequence of 'Unicode Character 'INVISIBLE SEPARATOR' (U+2063)' -->
[repository-image]:https://img.shields.io/github/v/tag/rivy/js.xdg-portable?label=%E2%81%A3&logo=github&logoColor=white
[repository-url]:https://github.com/rivy/js.xdg-portable

[repository-image]: https://img.shields.io/github/v/tag/rivy/js.xdg-portable?label=%E2%81%A3&logo=github&logoColor=white
[repository-url]: https://github.com/rivy/js.xdg-portable

<!-- [style-image]: https://img.shields.io/badge/code_style-standard-darkcyan.svg -->
<!-- [style-url]: https://standardjs.com -->

[style-image]: https://img.shields.io/badge/code_style-XO-darkcyan.svg
[style-url]: https://github.com/xojs/xo
44 changes: 30 additions & 14 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,35 @@ const osPaths = require('os-paths');
const linux = () => {
const object = {};

object.cache = () => process.env.XDG_CACHE_HOME || path.join(osPaths.home() || osPaths.temp(), '.cache');
object.config = () => process.env.XDG_CONFIG_HOME || path.join(osPaths.home() || osPaths.temp(), '.config');
object.data = () => process.env.XDG_DATA_HOME || path.join(osPaths.home() || osPaths.temp(), '.local', 'share');
object.cache = () =>
process.env.XDG_CACHE_HOME || path.join(osPaths.home() || osPaths.temp(), '.cache');
object.config = () =>
process.env.XDG_CONFIG_HOME || path.join(osPaths.home() || osPaths.temp(), '.config');
object.data = () =>
process.env.XDG_DATA_HOME || path.join(osPaths.home() || osPaths.temp(), '.local', 'share');
object.runtime = () => process.env.XDG_RUNTIME_DIR || undefined;
object.state = () => process.env.XDG_STATE_HOME || path.join(osPaths.home() || osPaths.temp(), '.local', 'state');
object.state = () =>
process.env.XDG_STATE_HOME || path.join(osPaths.home() || osPaths.temp(), '.local', 'state');

return object;
};

const macos = () => {
const object = {};

object.cache = () => process.env.XDG_CACHE_HOME || path.join(path.join(osPaths.home() || osPaths.temp(), 'Library'), 'Caches');
object.config = () => process.env.XDG_CONFIG_HOME || path.join(path.join(osPaths.home() || osPaths.temp(), 'Library'), 'Preferences');
object.data = () => process.env.XDG_DATA_HOME || path.join(path.join(osPaths.home() || osPaths.temp(), 'Library'), 'Application Support');
object.cache = () =>
process.env.XDG_CACHE_HOME ||
path.join(path.join(osPaths.home() || osPaths.temp(), 'Library'), 'Caches');
object.config = () =>
process.env.XDG_CONFIG_HOME ||
path.join(path.join(osPaths.home() || osPaths.temp(), 'Library'), 'Preferences');
object.data = () =>
process.env.XDG_DATA_HOME ||
path.join(path.join(osPaths.home() || osPaths.temp(), 'Library'), 'Application Support');
object.runtime = () => process.env.XDG_RUNTIME_DIR || undefined;
object.state = () => process.env.XDG_STATE_HOME || path.join(path.join(osPaths.home() || osPaths.temp(), 'Library'), 'State');
object.state = () =>
process.env.XDG_STATE_HOME ||
path.join(path.join(osPaths.home() || osPaths.temp(), 'Library'), 'State');

return object;
};
Expand All @@ -43,24 +55,28 @@ const windows = () => {
// Locations for cache/config/data/state are invented (Windows doesn't have a popular convention)

object.cache = () => {
const localAppData = process.env.LOCALAPPDATA || path.join(osPaths.home() || osPaths.temp(), 'AppData', 'Local'); // "AppData/Local" contains local-machine-only user data
const localAppData =
process.env.LOCALAPPDATA || path.join(osPaths.home() || osPaths.temp(), 'AppData', 'Local'); // "AppData/Local" contains local-machine-only user data
return process.env.XDG_CACHE_HOME || path.join(localAppData, 'xdg.cache');
};

object.config = () => {
const appData = process.env.APPDATA || path.join(osPaths.home() || osPaths.temp(), 'AppData', 'Roaming'); // "AppData/Roaming" contains data which may follow user between machines
const appData =
process.env.APPDATA || path.join(osPaths.home() || osPaths.temp(), 'AppData', 'Roaming'); // "AppData/Roaming" contains data which may follow user between machines
return process.env.XDG_CONFIG_HOME || path.join(appData, 'xdg.config');
};

object.data = () => {
const appData = process.env.APPDATA || path.join(osPaths.home() || osPaths.temp(), 'AppData', 'Roaming'); // "AppData/Roaming" contains data which may follow user between machines
const appData =
process.env.APPDATA || path.join(osPaths.home() || osPaths.temp(), 'AppData', 'Roaming'); // "AppData/Roaming" contains data which may follow user between machines
return process.env.XDG_DATA_HOME || path.join(appData, 'xdg.data');
};

object.runtime = () => process.env.XDG_RUNTIME_DIR || undefined;

object.state = () => {
const localAppData = process.env.LOCALAPPDATA || path.join(osPaths.home() || osPaths.temp(), 'AppData', 'Local'); // "AppData/Local" contains local-machine-only user data
const localAppData =
process.env.LOCALAPPDATA || path.join(osPaths.home() || osPaths.temp(), 'AppData', 'Local'); // "AppData/Local" contains local-machine-only user data
return process.env.XDG_STATE_HOME || path.join(localAppData, 'xdg.state');
};

Expand All @@ -75,7 +91,7 @@ const _XDGPortable = () => {
let extension = {};
if (/^darwin$/i.test(process.platform)) {
extension = macos();
} else if (/^win/i.test(process.platform)) {
} else if (/^win/i.test(process.platform)) {
extension = windows();
} else {
extension = linux();
Expand All @@ -101,7 +117,7 @@ const _XDGPortable = () => {
return dirs;
};

Object.keys(extension).forEach(key => {
Object.keys(extension).forEach((key) => {
XDGPortable[key] = extension[key];
});

Expand Down
30 changes: 11 additions & 19 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,39 @@ const module_ = require('../src/lib');

// Integration tests

test('api', t => {
test('api', (t) => {
const paths = module_;
const api = [
'cache',
'config',
'data',
'runtime',
'state',
'configDirs',
'dataDirs'
];
const api = ['cache', 'config', 'data', 'runtime', 'state', 'configDirs', 'dataDirs'];

t.is(typeof paths, 'function');
t.is(Object.keys(paths).length, api.length);
api.forEach(key => {
api.forEach((key) => {
t.is(typeof paths[key], 'function');
});
});

test('examples are executable without error (JavaScript)', t => {
test('examples are executable without error (JavaScript)', (t) => {
const egDirPath = 'eg';
const extensions = ['.js', '.cjs', '.mjs'];

const files = fs.readdirSync(egDirPath);

files
.filter(file => {
.filter((file) => {
return extensions.includes(path.extname(file));
})
.forEach(file => {
.forEach((file) => {
const command = 'node';
const script = path.join(egDirPath, file);
const args = [script];
const options = {shell: true};
const options = { shell: true };

t.log({script});
t.log({ script });

const {error, status} = spawn.sync(command, args, options);
const { error, status } = spawn.sync(command, args, options);

t.log({error, status});
t.log({ error, status });

t.deepEqual({error, status}, {error: null, status: 0});
t.deepEqual({ error, status }, { error: null, status: 0 });
});
});
2 changes: 1 addition & 1 deletion test/types.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expectType} from 'tsd';
import { expectType } from 'tsd';
import xdg = require('../src/lib');

expectType<typeof xdg>(xdg());
Expand Down
Loading

0 comments on commit a4fe2f4

Please sign in to comment.