@antora/expand-path-helper

中文文档
3.0.0发布于 2024/11/27MPL-2.0CommonJS零依赖近 30 天下载 20.7万

Provides a helper function to expand a path to a normalized absolute path. This function also expands dot, tilde, and tilde plus when used as the first path segment.

原始使用文档
文档目录6 节
  1. Install
  2. API
  3. Usage
  4. *nix
  5. Windows
  6. Copyright and License

@antora/expand-path-helper

A Node.js module that provides a helper function to expand a path to a normalized absolute path. This function also expands the following shorthand expressions when used as the first path segment: dot (.), tilde (~), or tilde plus (~+). The expanded path is system dependent. Developed for use in Antora.

Install

$ npm i @antora/expand-path-helper

API

function expandPath (path[, context])

Expands the specified path to a normalized absolute path. The main purpose of this function is path expansion and normalization.

  • path <string> - The path to expand. This parameter is required. If the path is already absolute, the path is normalized and returned.
  • context <Object> - Named parameters that control how the path is resolved. All named parameters are optional. This parameter can also be specified as a String, in which case it’s used as the base context argument. Default: {}.
    • base <string> - The base directory from which to resolve a relative path instead of the working directory. Default: ~+
    • cwd <string> - The absolute directory to use as the working directory instead of the current working directory. If not specified, the current working directory is used. Default: undefined
    • dot <string> - The value to use to replace a leading dot (.) segment. If the value is ., the value of the base context argument is used instead. Default: .

If the first segment of the path argument, the base context argument, or the dot context argument is ~ or ~+, that value is expanded to the user’s home directory or current working directory, respectively. If the first segment of the path argument is ., that value is replaced with the dot context argument after the dot context argument is expanded.

Usage

The output of expandPath depends on the system on which it is run (specifically on the path.sep value).

*nix

const expandPath = require('@antora/expand-path-helper')

expandPath('/absolute/path')
//=> /absolute/path

expandPath('/absolute/./path/..')
//=> /absolute

expandPath('foo/bar')
//=> $PWD/foo/bar

expandPath('./foo/bar')
//=> $PWD/foo/bar

expandPath('~/foo/bar')
//=> $HOME/foo/bar

expandPath('~+/foo/bar')
//=> $PWD/foo/bar

expandPath('~+/foo/bar', { cwd: '/working/dir' })
//=> /working/dir/foo/bar

expandPath('foo/bar', '/base/dir')
//=> /base/dir/foo/bar

expandPath('foo/bar', { base: '/base/dir' })
//=> /base/dir/foo/bar

expandPath('./foo/bar', { base: '/base/dir' })
//=> /base/dir/foo/bar

expandPath('./foo/bar', { dot: '/dot/dir' })
//=> /dot/dir/foo/bar

Windows

const expandPath = require('@antora/expand-path-helper')

expandPath('C:\\absolute\\path')
//=> C:\absolute\path

expandPath('C:/absolute/path')
//=> C:\absolute\path

expandPath('C:\\absolute\\.\\path\\..')
//=> C:\absolute

expandPath('foo\\bar')
//=> C:\current\directory\foo\bar

expandPath('.\\foo\\bar')
//=> C:\current\directory\foo\bar

expandPath('~\\foo\\bar')
//=> C:\Users\user\foo\bar

expandPath('~+\\foo\\bar')
//=> C:\current\directory\foo\bar

expandPath('~+\\foo\\bar', { cwd: 'C:\\working\\dir' })
//=> C:\working\dir\foo\bar

expandPath('foo\\bar', 'C:\\base\\dir')
//=> C:\base\dir\foo\bar

expandPath('foo\\bar', { base: 'C:\\base\\dir' })
//=> C:\base\dir\foo\bar

expandPath('.\\foo\\bar', { base: 'C:\\base\\dir' })
//=> C:\base\dir\foo\bar

expandPath('.\\foo\\bar', { dot: 'C:\\dot\\dir' })
//=> C:\dot\dir\foo\bar

On Windows, the input path may use forward slashes, but the expanded path will always have backslashes.

Copyright (C) 2018-present by OpenDevise Inc. and the individual contributors to Antora.

Use of this software is granted under the terms of the Mozilla Public License Version 2.0 (MPL-2.0).

版本与发布记录

@antora/expand-path-helper 共发布 6 个版本,首次发布于 2018/02/22;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.0latest2024/11/271.9 年前
2.0.02021/07/175 年前
1.0.02018/02/288 年前
1.0.0-rc.32018/02/248 年前
1.0.0-rc.22018/02/238 年前
1.0.0-rc.12018/02/228 年前

关于 @antora/expand-path-helper 的常见问题

@antora/expand-path-helper 是什么?

Provides a helper function to expand a path to a normalized absolute path. This function also expands dot, tilde, and tilde plus when used as the first path segment.

如何安装 @antora/expand-path-helper?

在项目目录运行 npm install @antora/expand-path-helper,或使用 pnpm add / yarn add / bun add。

@antora/expand-path-helper 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/antora__expand-path-helper 是否存在。

@antora/expand-path-helper 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@antora/expand-path-helper 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=16.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@antora/expand-path-helper 可以商用吗?

@antora/expand-path-helper 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@antora/expand-path-helper 的下载量有多少?

根据 npm 官方下载统计,@antora/expand-path-helper 在 2026/10/06 – 2026/11/04 这 30 天内被下载 207,203 次,日均约 6,907 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@antora/expand-path-helper 还在维护吗?

最新版本 3.0.0 发布于 2024/11/27(1.9 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/02/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。