os-paths
Determine common OS/platform paths (home, temp, …)
Installation (CJS/ESM/TypeScript)
npm install os-paths
# or... `npm install "git:github.com/rivy/js.os-paths"`
# or... `npm install "git:github.com/rivy/js.os-paths#v7.0.0"`
# or... `npm install "https://cdn.jsdelivr.net/gh/rivy/js.os-paths/dist/os-paths.tgz"`
Usage
CommonJS (CJS)
const osPaths = require('os-paths/cjs');
const home = osPaths.home();
const temp = osPaths.temp();
ECMAScript (ESM)/TypeScript
import osPaths from 'os-paths';
const home = osPaths.home();
const temp = osPaths.temp();
Deno
import osPaths from 'https://deno.land/x/os_paths/src/mod.deno.ts';
//or...
//import osPaths from 'https://deno.land/x/os_paths@v7.0.0/src/mod.deno.ts';
//or (via CDN, with optional version/version-range/latest/commit support)...
//import osPaths from 'https://cdn.jsdelivr.net/gh/rivy/js.os-paths@7.0.0/src/mod.deno.ts'; // v7.0.0
//import osPaths from 'https://cdn.jsdelivr.net/gh/rivy/js.os-paths@7/src/mod.deno.ts'; // v7.x.y
//import osPaths from 'https://cdn.jsdelivr.net/gh/rivy/js.os-paths/src/mod.deno.ts'; // latest
//import osPaths from 'https://cdn.jsdelivr.net/gh/rivy/js.os-paths@latest/src/mod.deno.ts'; // latest
//import osPaths from 'https://cdn.jsdelivr.net/gh/rivy/js.os-paths@COMMIT/src/mod.deno.ts'; // commit
const home = osPaths.home();
const temp = osPaths.temp();
API
Construction/Initialization
OSPaths()
const osPaths = require('os-paths/cjs'); // CJS
//or...
//import osPaths from 'os-paths'; // ESM/TypeScript
//import osPaths from 'https://deno.land/x/os_paths/src/mod.deno.ts'; // Deno
When importing this module, the object returned is a function object, OSPaths
, augmented with attached methods. Additional OSPaths
objects may be constructed by direct call of the imported OSPaths
object (eg, const p = osPaths()
) or by using new
(eg, const p = new osPaths()
). Notably, since the OSPaths
object contains no instance state, all OSPaths
objects will be functionally identical.
Methods
All methods return simple, platform-specific, and platform-compatible path strings which are normalized and have no trailing path separators.
The returned path strings are not guaranteed to already exist on the file system. So, the user application is responsible for directory construction, if/when needed. However, since all of these are standard OS directories, they should all exist without the need for user intervention.
If/when necessary, make-dir
or mkdirp
can be used to create the directories.
osPaths.home(): string | undefined
- Returns the path string of the user’s home directory (or
undefined
if the user’s home directory is not resolvable).
console.log(osPaths.home());
//(*nix) => '/home/rivy
//(windows) => 'C:\Users\rivy'
osPaths.temp(): string
- Returns the path string of the system’s default directory for temporary files.
console.log(osPaths.temp());
//(*nix) => '/tmp'
//(windows) => 'C:\Users\rivy\AppData\Local\Temp'
temp()
will always return a non-empty path string (as sanely as possible).
Supported Platforms
NodeJS
Requirements
NodeJS >= 4.0[^*]
[^*]: With the conversion to a TypeScript-based project, due to tooling constraints, building and testing are more difficult and more limited on Node platforms earlier than NodeJS-v10. However, the generated CommonJS/UMD project code is fully tested (for NodeJS-v10+) and continues to be compatible with NodeJS-v4+.
*.js
and *.cjs
)
CommonJS modules (CJS; CJS is the basic supported output (with support for NodeJS versions as early as NodeJS-v4).
const osPaths = require('os-paths/cjs');
console.log(osPaths.home());
console.log(osPaths.temp());
Note: for CJS,
require('os-paths')
is supported for backward-compatibility and will execute correctly at run-time. However,require('os-paths')
links to the default package type declarations which, though correct for Deno/ESM/TypeScript, are incorrect for CJS. This, then, leads to incorrect analysis of CJS files by static analysis tools such as TypeScript and Intellisense.Using
require('os-paths/cjs')
is preferred as it associates the proper CJS type declarations and provides correct information to static analysis tools.
*.mjs
)
ECMAScript modules (ESM; - Requires
OSPaths
v6.0
+.
OSPaths
fully supports ESM imports.
import osPaths from 'os-paths';
console.log(osPaths.home());
console.log(osPaths.temp());
*.ts
)
TypeScript (- Requires
OSPaths
v5.0
+.
As of v5.0
+, OSPaths
has been converted to a TypeScript-based module.
As a consequence, TypeScript type definitions are automatically generated, bundled, and exported by the module.
Deno
Requirements
Deno >= v1.8.0[^deno-version-req]
[^deno-version-req]: The Deno.permissions
API (stabilized in Deno v1.8.0) is required to avoid needless panics or prompts by Deno during static imports of this module/package. Note: Deno v1.3.0+ may be used if the run flag --unstable
is also used.
Required Permissions
--allow-env
· allow access to the process environment variables
This module/package requires access to various environment variables to determine platform configuration (eg, location of temp and user directories).
- Requires
OSPaths
v6.0
+.
OSPaths
also fully supports use by Deno.
import osPaths from 'https://deno.land/x/os_paths/src/mod.deno.ts';
console.log(osPaths.home());
console.log(osPaths.temp());
Building and Contributing
Build requirements
optional
git-changelog
(v1.1+) … enables changelog automationperl
… enables automated version updates topackage.json
during packaging
Quick build/test
npm install-test
Contributions/development
Reproducible setup (for CI or local development)
git clone "https://github.com/rivy/js.os-paths"
cd js.os-paths
# * note: for WinOS, replace `cp` with `copy`
# npm
cp .deps-lock/package-lock.json .
npm clean-install
# yarn
cp .deps-lock/yarn.lock .
yarn --immutable --immutable-cache --check-cache
Project development scripts
> npm run help
...
usage: `npm run TARGET` or `npx run-s TARGET [TARGET..]`
TARGETs:
build build/compile package
clean remove build artifacts
coverage calculate and display (or send) code coverage [alias: 'cov']
fix fix package issues (automated/non-interactive)
fix:lint fix ESLint issues
fix:style fix Prettier formatting issues
help display help
lint check for package code 'lint'
lint:commits check for commit flaws (using `commitlint` and `cspell`)
lint:editorconfig check for EditorConfig format flaws (using `editorconfig-checker`)
lint:lint check for code 'lint' (using `eslint`)
lint:markdown check for markdown errors (using `remark`)
lint:spell check for spelling errors (using `cspell`)
lint:style check for format imperfections (using `prettier`)
prerelease clean, rebuild, and fully test (useful prior to publish/release)
realclean remove all generated files
rebuild clean and (re-)build project
refresh clean and rebuild/regenerate all project artifacts
refresh:dist clean, rebuild, and regenerate project distribution
retest clean and (re-)test project
reset:hard remove *all* generated files and reinstall dependencies
show:deps show package dependencies
test test package
test:code test package code
test:types test for type declaration errors (using `tsd`)
update update/prepare for distribution [alias: 'dist']
update:changelog update CHANGELOG (using `git changelog ...`)
update:dist update distribution content
verify fully (and verbosely) test package
Packaging & Publishing
Package
#=== * POSIX
# next VERSION in M.m.r (semver) format
VERSION=...
# update to VERSION in package.json
perl -i -E 'use open IO => q/:raw:utf8/} while(<>) { s/^(\s*\x22version\x22\s*:\s*)\x22(?:\d+(?:[.]\d+)*)?\x22/$1\x22$ENV{VERSION}\x22/ims; print }' package.json
git-changelog --next-tag "v${VERSION}" > CHANGELOG.mkd
# create/commit updates and distribution
git add package.json CHANGELOG.mkd
git commit -m "${VERSION}"
npm run clean && npm run update:dist
git add dist
git commit --amend --no-edit
# tag VERSION commit
git tag -f "v${VERSION}"
@rem ::# (optional) update/save dependency locks
npm install --package-lock
rm yarn.lock && yarn import
mkdir .deps-lock 2> /dev/null
cp package-lock.json .deps-lock/
cp yarn.lock .deps-lock/
# (optional) prerelease checkup
npm run prerelease
#=== * WinOS
@rem ::# next VERSION in M.m.r (semver) format
set VERSION=...
@rem ::# update to VERSION in package.json
perl -i -E "use open IO => q/:raw:utf8/; while(<>) { s/^(\s*\x22version\x22\s*:\s*)\x22(?:\d+(?:[.]\d+)*)?\x22/$1\x22$ENV{VERSION}\x22/ims; print }" package.json
git-changelog --next-tag "v%VERSION%" > CHANGELOG.mkd
@rem ::# create/commit updates and distribution
git add package.json CHANGELOG.mkd
git commit -m "%VERSION%"
npm run clean && npm run update:dist
git add dist
git commit --amend --no-edit
@rem ::# tag VERSION commit
git tag -f "v%VERSION%"
@rem ::# (optional) update/save dependency locks
npm install --package-lock
del yarn.lock && yarn import
mkdir .deps-lock 2>NUL
copy /y package-lock.json .deps-lock
copy /y yarn.lock .deps-lock
@rem ::# (optional) prerelease checkup
npm run prerelease
Publish
# publish
# * optional (will be done in 'prePublishOnly' by `npm publish`)
npm run clean && npm run test && npm run dist && git-changelog > CHANGELOG.mkd
npm run _:v_tag:exists || echo "[lint] ERROR Missing version matching commit tag" # expect no output and exit code == 0
git diff-index --quiet HEAD || echo "[lint] ERROR uncommitted changes" # expect no output and exit code == 0
# *
npm publish # `npm publish --dry-run` will perform all prepublication actions and stop just before the actual publish push
# * if published to NPMjs with no ERRORs; push to deno.land with tag push
git push origin --tags
Contributions
Contributions are welcome.
Any pull requests should be based off of the default branch (master
). And, whenever possible, please include tests for any new code, ensuring that local (via npm test
) and remote CI testing passes.
By contributing to the project, you are agreeing to provide your contributions under the same license as the project itself.
Related
xdg-app-paths
… easy XDG for applicationsxdg-portable
… XDG Base Directory paths (cross-platform)