Comparing version
// # spell-checker:ignore rivy | ||
declare namespace osPaths { | ||
export interface Options { | ||
/** | ||
__Don't use this option unless you really have to!__ | ||
Suffix appended to the project name to avoid name conflicts with native apps. Pass an empty string to disable it. | ||
@default 'nodejs' | ||
*/ | ||
readonly suffix?: string; | ||
} | ||
export interface Paths { | ||
/** | ||
Directory for data files. | ||
*/ | ||
readonly data: string; | ||
/** | ||
Directory for data files. | ||
*/ | ||
readonly config: string; | ||
/** | ||
Directory for non-essential data files. | ||
*/ | ||
readonly cache: string; | ||
/** | ||
Directory for log files. | ||
*/ | ||
readonly log: string; | ||
/** | ||
Directory for temporary files. | ||
*/ | ||
readonly temp: string; | ||
} | ||
declare namespace OSPaths { | ||
function home(): string; | ||
function temp(): string; | ||
} | ||
declare const osPaths: { | ||
/** | ||
Get OS-specific (and [XDG](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html)-compatible) paths for storing things like data, config, cache, etc | ||
Note: It only generates the path strings. It doesn't create the directories for you. You could use [`make-dir`](https://github.com/sindresorhus/make-dir) to create the directories. | ||
@param name - Name of your project. Used to generate the paths. | ||
@returns The paths to use for your project on current OS. | ||
@example | ||
``` | ||
import osPaths = require('os-paths'); | ||
const paths = osPaths('MyApp'); | ||
paths.data; | ||
//(*nix)=> '/home/rivy/.local/share/MyApp-nodejs' | ||
paths.config | ||
//(*nix)=> '/home/rivy/.config/MyApp-nodejs' | ||
``` | ||
*/ | ||
(name: string, options?: osPaths.Options): osPaths.Paths; | ||
// TODO: Remove this for the next major release, refactor the whole definition to: | ||
// declare function osPaths(name: string, options?: osPaths.Options): osPaths.Paths; | ||
// export = osPaths; | ||
default: typeof osPaths; | ||
}; | ||
export = osPaths; | ||
declare function OSPaths(): typeof OSPaths; | ||
export = OSPaths; |
179
index.js
@@ -1,127 +0,100 @@ | ||
// # spell-checker:ignore macos APPDATA LOCALAPPDATA | ||
// # spell-checker:ignore UserProfile HomeDrive HomePath WinDir | ||
/* eslint-env es6, node */ | ||
'use strict'; | ||
'use strict'; | ||
const path = require('path'); | ||
const os = require('os'); | ||
const homedir = os.homedir(); | ||
const tmpdir = os.tmpdir(); | ||
const {env} = process; | ||
const isWinOS = /^win/i.test(process.platform); | ||
// XDG references | ||
// # ref: <https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html> @@ <https://archive.is/aAhtw> | ||
// # ref: <https://specifications.freedesktop.org/basedir-spec/latest/ar01s03.html> @@ <https://archive.is/7N0TN> | ||
// # ref: <https://wiki.archlinux.org/index.php/XDG_Base_Directory> @@ <https://archive.is/VdO9n> | ||
// # ref: <https://wiki.debian.org/XDGBaseDirectorySpecification#state> @@ <http://archive.is/pahId> | ||
// # ref: <https://ploum.net/207-modify-your-application-to-use-xdg-folders> @@ <https://archive.is/f43Gk> | ||
const base = () => { | ||
const {env} = process; | ||
const macos = name => { | ||
const library = path.join(homedir, 'Library'); | ||
const object = {}; | ||
const _config = path.join(env.XDG_CONFIG_HOME ? env.XDG_CONFIG_HOME : path.join(library, 'Preferences'), name); | ||
const _data = path.join(env.XDG_DATA_HOME ? env.XDG_DATA_HOME : path.join(library, 'Application Support'), name); | ||
object.home = os.homedir ? | ||
() => { | ||
return os.homedir(); | ||
} : | ||
() => { | ||
let path = env.HOME; | ||
if (path.length > 1 && path.endsWith('/')) { | ||
path = path.slice(0, -1); | ||
} | ||
const _configDirs = [_config]; | ||
if (env.XDG_CONFIG_DIRS) { | ||
_configDirs.push(...env.XDG_CONFIG_DIRS.split(path.delimiter).map(s => path.join(s, name))); | ||
} | ||
return path; | ||
}; | ||
const _dataDirs = [_data]; | ||
if (env.XDG_DATA_DIRS) { | ||
_dataDirs.push(...env.XDG_DATA_DIRS.split(path.delimiter).map(s => path.join(s, name))); | ||
} | ||
object.temp = os.tmpdir ? | ||
() => { | ||
return os.tmpdir(); | ||
} : | ||
() => { | ||
let path = env.TMPDIR || | ||
env.TEMP || | ||
env.TMP || | ||
'/tmp'; | ||
if (path.length > 1 && path.endsWith('/')) { | ||
path = path.slice(0, -1); | ||
} | ||
return { | ||
cache: env.XDG_CACHE_HOME ? path.join(env.XDG_CACHE_HOME, name) : path.join(library, 'Caches', name), | ||
config: _config, | ||
data: _data, | ||
log: env.XDG_STATE_HOME ? path.join(env.XDG_STATE_HOME, name) : path.join(library, 'Logs', name), | ||
temp: path.join(tmpdir, name), | ||
configDirs: _configDirs, | ||
dataDirs: _dataDirs | ||
}; | ||
return path; | ||
}; | ||
return object; | ||
}; | ||
const windows = name => { | ||
// #ref: <https://www.thewindowsclub.com/local-localnow-roaming-folders-windows-10> @@ <http://archive.is/tDEPl> | ||
const appData = env.APPDATA || path.join(homedir, 'AppData', 'Roaming'); // "AppData/Roaming" contains data which may follow user between machines | ||
const localAppData = env.LOCALAPPDATA || path.join(homedir, 'AppData', 'Local'); // "AppData/Local" contains local-machine-only user data | ||
const windows = () => { | ||
const {env} = process; | ||
const _config = env.XDG_CONFIG_HOME ? path.join(env.XDG_CONFIG_HOME, name) : path.join(appData, name, 'Config'); | ||
const _data = env.XDG_DATA_HOME ? path.join(env.XDG_DATA_HOME, name) : path.join(appData, name, 'Data'); | ||
const object = {}; | ||
const _configDirs = [_config]; | ||
if (env.XDG_CONFIG_DIRS) { | ||
_configDirs.push(...env.XDG_CONFIG_DIRS.split(path.delimiter).map(s => path.join(s, name))); | ||
} | ||
object.home = os.homedir ? | ||
() => { | ||
return os.homedir(); | ||
} : | ||
() => { | ||
let path = env.USERPROFILE || env.HOMEDRIVE + env.HOMEPATH || env.HOME; | ||
if (path.length > 1 && ((path.endsWith('\\') && !path.endsWith(':\\')) || (path.endsWith('/') && !path.endsWith(':/')))) { | ||
path = path.slice(0, -1); | ||
} | ||
const _dataDirs = [_data]; | ||
if (env.XDG_DATA_DIRS) { | ||
_dataDirs.push(...env.XDG_DATA_DIRS.split(path.delimiter).map(s => path.join(s, name))); | ||
} | ||
return path; | ||
}; | ||
return { | ||
// Locations for data/config/cache/log are invented (Windows doesn't have a popular convention) | ||
cache: env.XDG_CACHE_HOME ? path.join(env.XDG_CACHE_HOME, name) : path.join(localAppData, name, 'Cache'), | ||
config: _config, | ||
data: _data, | ||
log: env.XDG_STATE_HOME ? path.join(env.XDG_STATE_HOME, name) : path.join(localAppData, name, 'Log'), | ||
temp: path.join(tmpdir, name), | ||
configDirs: _configDirs, | ||
dataDirs: _dataDirs | ||
}; | ||
}; | ||
object.temp = os.tmpdir ? | ||
() => { | ||
return os.tmpdir(); | ||
} : | ||
() => { | ||
let path = env.TEMP || | ||
env.TMP || | ||
(env.SystemRoot || env.windir) + '\\temp'; | ||
if (path.length > 1 && ((path.endsWith('\\') && !path.endsWith(':\\')) || (path.endsWith('/') && !path.endsWith(':/')))) { | ||
path = path.slice(0, -1); | ||
} | ||
const linux = name => { | ||
const username = path.basename(homedir); | ||
return path; | ||
}; | ||
const _config = path.join(env.XDG_CONFIG_HOME ? env.XDG_CONFIG_HOME : path.join(homedir, '.config'), name); | ||
const _data = path.join(env.XDG_DATA_HOME ? env.XDG_DATA_HOME : path.join(homedir, '.local', 'share'), name); | ||
const _configDirs = [_config]; | ||
if (env.XDG_CONFIG_DIRS) { | ||
_configDirs.push(...env.XDG_CONFIG_DIRS.split(path.delimiter).map(s => path.join(s, name))); | ||
} | ||
const _dataDirs = [_data]; | ||
if (env.XDG_DATA_DIRS) { | ||
_dataDirs.push(...env.XDG_DATA_DIRS.split(path.delimiter).map(s => path.join(s, name))); | ||
} | ||
return { | ||
cache: path.join(env.XDG_CACHE_HOME || path.join(homedir, '.cache'), name), | ||
config: _config, | ||
data: _data, | ||
log: path.join(env.XDG_STATE_HOME || path.join(homedir, '.local', 'state'), name), | ||
temp: path.join(tmpdir, username, name), | ||
configDirs: _configDirs, | ||
dataDirs: _dataDirs | ||
}; | ||
return object; | ||
}; | ||
const osPaths = (name, options) => { | ||
if (typeof name !== 'string') { | ||
throw new TypeError(`Expected string, got ${typeof name}`); | ||
} | ||
class _OSPaths { | ||
constructor() { | ||
const OSPaths = function () { | ||
return new _OSPaths(); | ||
}; | ||
options = Object.assign({suffix: 'nodejs'}, options); | ||
this._fn = OSPaths; | ||
if (options.suffix) { | ||
// Add suffix to prevent possible conflict with native apps | ||
name += `-${options.suffix}`; | ||
} | ||
// Connect to platform-specific API functions by extension | ||
const extension = isWinOS ? windows() : base(); | ||
Object.keys(extension).forEach(key => { | ||
this._fn[key] = extension[key]; | ||
}); | ||
if (process.platform === 'darwin') { | ||
return macos(name); | ||
return this._fn; | ||
} | ||
} | ||
if (process.platform === 'win32') { | ||
return windows(name); | ||
} | ||
return linux(name); | ||
}; | ||
module.exports = osPaths; | ||
// #TODO: Remove this for the next major release | ||
module.exports.default = osPaths; | ||
module.exports = new _OSPaths(); |
{ | ||
"name": "os-paths", | ||
"version": "3.0.2", | ||
"description": "Generate portable (and XDG-compatible) paths for storing cache, config, data, etc", | ||
"version": "4.0.0", | ||
"description": "Generate portable basic OS paths (home, temp, ...)", | ||
"license": "MIT", | ||
@@ -15,3 +15,14 @@ "repository": "rivy/js.os-paths", | ||
"scripts": { | ||
"test": "xo && ava && tsd" | ||
"coverage": "nyc report --reporter=text-lcov | codecov --disable=gcov --pipe", | ||
"lint": "run-p lint:*", | ||
"lint:spell": "run-s -s _:min-node-8 _:spellcheck || run-s -s _:max-node-6 _:spellcheck-warn", | ||
"lint:style": "xo", | ||
"pretest": "npm run lint", | ||
"test": "run-p test:*", | ||
"test:code": "nyc --silent ava", | ||
"test:types": "tsd", | ||
"_:min-node-8": "is-node-modern 8", | ||
"_:max-node-6": "is-node-not-modern 8", | ||
"_:spellcheck": "cspell *.js *.ts readme.md --no-summary", | ||
"_:spellcheck-warn": "echo-cli \"lint:spell WARN Spell-check skipped [for NodeJS < v8]\"" | ||
}, | ||
@@ -23,8 +34,4 @@ "files": [ | ||
"keywords": [ | ||
"appdir", | ||
"cache", | ||
"common", | ||
"config", | ||
"data", | ||
"dir", | ||
"cross-platform", | ||
"directory", | ||
@@ -34,16 +41,25 @@ "env", | ||
"linux", | ||
"logs", | ||
"mac", | ||
"macos", | ||
"osx", | ||
"path", | ||
"paths", | ||
"temp", | ||
"portable", | ||
"unix", | ||
"user", | ||
"windows", | ||
"xdg" | ||
"windows" | ||
], | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"codecov": "^3.5.0", | ||
"cspell": "^4.0.30", | ||
"echo-cli": "^1.0.8", | ||
"eslint": "^5.16.0", | ||
"is-node-modern": "^1.0.0", | ||
"lodash": "^4.17.15", | ||
"npm-run-all": "^4.1.5", | ||
"nyc": "^14.1.1", | ||
"tsd": "^0.7.1", | ||
"util": "^0.12.1", | ||
"xo": "^0.24.0" | ||
} | ||
} |
103
readme.md
@@ -8,8 +8,18 @@ <!DOCTYPE markdown><!-- markdownlint-disable no-inline-html --> | ||
# os-paths [](https://travis-ci.org/rivy/js.os-paths) | ||
# [os-paths](https://github.com/rivy/js.os-paths) | ||
> Get OS-specific (and [XDG](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html)-compatible) paths for storing things like data, config, cache, etc | ||
> Generate portable basic OS paths (home, temp, ...) | ||
## Install | ||
[![Build status][travis-image]][travis-url] | ||
[![Build status][appveyor-image]][appveyor-url] | ||
[![Coverage status][coverage-image]][coverage-url] | ||
[![Javascript Style Guide][style-image]][style-url] | ||
[![License][license-image]][license-url] | ||
<br/> | ||
[![Repository][repository-image]][repository-url] | ||
[![NPM version][npm-image]][npm-url] | ||
[![Downloads][downloads-image]][downloads-url] | ||
## Installation | ||
```shell | ||
@@ -24,9 +34,9 @@ npm install os-paths | ||
const paths = osPaths('MyApp'); | ||
osPaths.home(); | ||
//(*nix) => '/home/rivy' | ||
//(win) => 'C:\Users\RIvy' | ||
paths.data; | ||
//(*nix)=> '/home/rivy/.local/share/MyApp-nodejs' | ||
paths.config | ||
//(*nix)=> '/home/rivy/.config/MyApp-nodejs' | ||
osPaths.temp(); | ||
//(*nix) => '/tmp' | ||
//(win) => 'C:\temp' | ||
``` | ||
@@ -36,47 +46,70 @@ | ||
### paths = osPaths(name, [options]) | ||
### Initialization | ||
Note: It only generates the path strings. It doesn't create the directories for you. You could use [`make-dir`](https://github.com/sindresorhus/make-dir) to create the directories. | ||
#### `require('os-paths'): OSPaths()` | ||
#### name | ||
```js | ||
const osPaths = require('os-paths'); | ||
// or ... | ||
const osPaths = require('os-paths')( options ); | ||
``` | ||
Type: `string` | ||
The object returned by the module constructor is an OSPaths Function object, augmented with attached methods. When called directly (eg, `const p = xdg()`), it returns a newly constructed OSPaths object. Since the OSPaths object contains no instance state, all constructed objects will be functionally identical. | ||
Name of your project. Used to generate the paths. | ||
### Methods | ||
#### options | ||
All module methods return platform-compatible path strings. | ||
Type: `Object` | ||
Note: It only generates the path strings. It doesn't create the directories for you. You could use [`make-dir`](https://github.com/sindresorhus/make-dir) to create the directories. | ||
##### suffix | ||
#### `osPaths.home(): string` | ||
Type: `string`<br> | ||
Default: `'nodejs'` | ||
Returns the home directory for user | ||
**Don't use this option unless you really have to!**<br> | ||
Suffix appended to the project name to avoid name conflicts with native | ||
apps. Pass an empty string to disable it. | ||
#### `osPaths.temp(): string` | ||
### paths.data | ||
Returns the directory for temporary files | ||
Directory for data files. | ||
### XDG | ||
### paths.config | ||
All XDG-related methods have been relocated to the [`xdg-portable`](https://www.npmjs.com/package/xdg-portable) and [`xdg-app-paths`](https://www.npmjs.com/package/xdg-app-paths) modules. | ||
Directory for config files. | ||
## Related | ||
### paths.cache | ||
- [`xdg-app-paths`](https://www.npmjs.com/package/xdg-app-paths) ... easy XDG for applications | ||
- [`xdg-portable`](https://www.npmjs.com/package/xdg-portable) ... XDG Base Directory paths (cross-platform) | ||
Directory for non-essential data files. | ||
## License | ||
### paths.log | ||
MIT © [Roy Ivy III](https://github.com/rivy), [Sindre Sorhus](https://sindresorhus.com) | ||
Directory for log files. | ||
<!-- badge references --> | ||
### paths.temp | ||
[npm-image]: https://img.shields.io/npm/v/os-paths.svg?style=flat | ||
[npm-url]: https://npmjs.org/package/os-paths | ||
Directory for temporary files. | ||
<!-- [appveyor-image]: https://ci.appveyor.com/api/projects/status/.../branch/master?svg=true --> | ||
[appveyor-image]: https://img.shields.io/appveyor/ci/rivy/js-os-paths/master.svg?style=flat&logo=AppVeyor&logoColor=silver | ||
[appveyor-url]: https://ci.appveyor.com/project/rivy/js-os-paths | ||
<!-- [travis-image]: https://travis-ci.org/rivy/js.os-paths.svg?branch=master --> | ||
<!-- [travis-image]: https://img.shields.io/travis/rivy/js.os-paths/master.svg?style=flat&logo=Travis-CI&logoColor=silver --> | ||
[travis-image]: https://img.shields.io/travis/rivy/js.os-paths/master.svg?style=flat | ||
[travis-url]: https://travis-ci.org/rivy/js.os-paths | ||
## License | ||
MIT © Roy Ivy III, [Sindre Sorhus](https://sindresorhus.com) | ||
<!-- [coverage-image]: https://img.shields.io/coveralls/github/rivy/os-paths/master.svg --> | ||
<!-- [coverage-url]: https://coveralls.io/github/rivy/os-paths --> | ||
[coverage-image]: https://img.shields.io/codecov/c/github/rivy/js.os-paths/master.svg | ||
[coverage-url]: https://codecov.io/gh/rivy/js.os-paths | ||
[downloads-image]: http://img.shields.io/npm/dm/os-paths.svg?style=flat | ||
[downloads-url]: https://npmjs.org/package/os-paths | ||
[issues-image]: https://img.shields.io/github/issues/rivy/js.os-paths?logo=github | ||
[issues-url]: https://github.com/rivy/js.os-paths/issues | ||
[license-image]: https://img.shields.io/npm/l/os-paths.svg?style=flat | ||
[license-url]: license | ||
<!-- [repository-image]:https://img.shields.io/badge/%E2%9D%A4-darkcyan?style=flat&logo=github --> | ||
[repository-image]:https://img.shields.io/github/v/tag/rivy/js.os-paths?label=@&logo=github | ||
[repository-url]:https://github.com/rivy/js.os-paths | ||
<!-- [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 |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
15801
54.25%6
20%113
41.25%12
300%86
-45.57%1
Infinity%