@antora/expand-path-helper
A node module that exports a helper function to expand a path to an absolute path, first resolving leading shortcuts such as dot (.
), tilde (~
), and tilde plus (~+
).
Developed for use in Antora.
Install
$ npm i @antora/expand-path-helper
or
$ yarn add @antora/expand-path-helper
Usage
function expandPath (path, start = process.cwd(), defaultPrefix = '~+') { ... }
- The first parameter (
path
) is the path to expand.
This parameter is required and must not be falsy. - The second parameter (
start
) is the starting directory to use to expand a leading '.' segment.
This parameter is optional.
It defaults to the current working directory of the process. - The third parameter (
defaultPrefix
) is the prefix (first segment) to prepend to the path if the path is not anchored (e.g., dir/file
).
This parameter is optional.
By default, a path which is not anchored is resolved starting from the current working directory of the process.
This behavior can be changed by passing a different defaultPrefix
value, such as .
(uses the value of start), ~
(uses the user's home directory).
The default is the equivalent of passing ~+
.
*nix
const expandPath = require('@antora/expand-path-helper')
expandPath('/absolute/path')
expandPath('foo/bar')
expandPath('~/foo/bar')
expandPath('~+/foo/bar')
expandPath('./foo/bar', '/start/dir')
expandPath('foo/bar', '/start/dir', '.')
Windows
const expandPath = require('@antora/expand-path-helper')
expandPath('C:\\absolute\\path')
expandPath('foo\\bar')
expandPath('~\\foo\\bar')
expandPath('~+\\foo\\bar')
expandPath('.\\foo\\bar', 'C:\\start\\dir')
expandPath('foo/bar', '/start/dir', '.')
On Windows, the input path may contain forward slashes, but the expanded path will always have backslashes.
Copyright and License
Copyright (C) 2018 OpenDevise Inc. and the Antora Project.
Use of this software is granted under the terms of the Mozilla Public License Version 2.0 (MPL-2.0).