gpt4 book ai didi

javascript - 获取当前工作目录名称而不是 Node.js 中的路径

转载 作者:行者123 更新时间:2023-11-29 10:28:08 30 4
gpt4 key购买 nike

我正在开发一个需要使用当前目录名称的 CLI 应用程序。

我可以使用 process.cwd() 获取当前目录的路径。如何获取当前目录名称而不是整个路径?

是否可以执行以下操作?

process.cwd().split('/').slice(-1)[0]

它有效,但感觉很脆弱。执行此操作的最佳和最稳健的方法是什么?

最佳答案

即使答案中的代码有效,您也应该使用 path.basename()获取路径最后一部分的名称,因为:

  • 跨操作系统工作(windows 和类 unix 系统使用不同的路径分隔符)
  • 它忽略尾随目录分隔符(即 /path/to/cwd/ 中的最后一个 /)

此外,为了额外的安全,您应该使用 path.resolve()因为它在获取基本名称之前规范化了路径,从而摆脱了与路径相关的怪癖。

如果你可以用process.cwd()得到/path/to/cwd,那么下面会给你目录名( “cwd” 在示例中):

path.basename(process.cwd())

添加 path.resolve() 以提高安全性:

path.basename(path.resolve(process.cwd()))

甚至:

path.basename(path.resolve())

示例:

const path = require('path');

function slice(pathName) {
const res = pathName.split(path.sep).slice(-1)[0];
console.log('slicer ', pathName, '=>', `'${res}'`);
}

function basename(pathName) {
const res = path.basename(path.resolve(pathName));
console.log('basename', pathName, '=>', `'${res}'`);
}

slice('/path/to/cwd'); // cwd
basename('/path/to/cwd'); // cwd

slice('/path/to/cwd/'); // ''
basename('/path/to/cwd/'); // cwd

// Other valid paths
slice('/path/to/cwd/..'); // '..'
basename('path/to/cwd/..'); // cwd

slice('.'); // '.'
basename('.'); // <current directory name>

进程.cwd()

Returns: <string>

The process.cwd() method returns the current working directory of the Node.js process.

console.log(`Current directory: ${process.cwd()}`);

保证是当前工作目录的绝对路径。用这个!

路径.basename(路径[, ext])

path <string>
ext <string> An optional file extension
Returns: <string>

The path.basename() method returns the last portion of a path, similar to the Unix basename command. Trailing directory separators are ignored, see path.sep.

path.basename('/foo/bar/baz/asdf/quux.html');
// Returns: 'quux.html'

path.basename('/foo/bar/baz/asdf/quux.html', '.html');
// Returns: 'quux'

A TypeError is thrown if path is not a string or if ext is given and is not a string.

路径.resolve([...路径])

...paths <string> A sequence of paths or path segments
Returns: <string>

The path.resolve() method resolves a sequence of paths or path segments into an absolute path.

The given sequence of paths is processed from right to left, with each subsequent path prepended until an absolute path is constructed. For instance, given the sequence of path segments: /foo, /bar, baz, calling path.resolve('/foo', '/bar', 'baz') would return /bar/baz.

If after processing all given path segments an absolute path has not yet been generated, the current working directory is used.

The resulting path is normalized and trailing slashes are removed unless the path is resolved to the root directory.

Zero-length path segments are ignored.

If no path segments are passed, path.resolve() will return the absolute path of the current working directory.

如果您可以使用 process.cwd(),在其他情况下您不需要 path.resolve(),请使用它!

关于javascript - 获取当前工作目录名称而不是 Node.js 中的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53295229/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com