@getvim/execute

中文文档
1.0.0发布于 2019/12/02ISCTypeScriptCommonJS零依赖近 30 天下载 1.6万

来自Node.js的极端批判处决

中文使用文档
文档目录10 节
  1. 所需资源
  2. 使用量
  3. 安装
  4. 基本用途
  5. 环境变量
  6. 管道
  7. 错误
  8. 行为
  9. 为什么?
  10. 常见巴什陷阱

@ getvim/ 执行

这是一个极有见解的零依赖性,小模块,用于使用 Bash shell 执行来自 Node.js 的命令行程序.

这个模块背后的设计哲学 旨在抽象出许多巴什陷阱 我们大多数人从未知道存在.

所需资源

你一定是在一个环境里工作 /bin/bash 已提供.

最小节点.js版本为8.0.0.

使用量

安装

npm install --save @getvim/execute
yarn add @getvim/execute

基本用途

const { execute } = require('@getvim/execute');
execute('ls')
    .then(console.log); // result: "feature.js\nfeature.spec.js"

环境变量

const { execute } = require('@getvim/execute');
execute('echo $MY_NAME', { env: { MY_NAME: 'Jake Chambers' } })
    .then(console.log); // result: "Jake Chambers"

管道

Piping允许您与现代JS特性交互出贝壳命令,这些特性比许多贝壳替代品,如使用外部模块,通量,回环或字符串格式化器更安全,更容易被使用.

const { execute, pipe } = require('@getvim/execute');
const mailer = require('@fictional/mail-client')
execute('ls | grep temp')
    .then(tempFiles => {
        mailer('[email protected]', 'Temp files being deleted', tempFiles);
        return tempFiles;
    })
    .then(pipe('xargs rm'));

错误

const { execute } = require('@getvim/execute');
execute('npm install')
    .then(() => execute('npm test')) // Has a failing test
    .then(() => execute('npm publish'))
    .catch(error => console.error('Failed CI/CD!', error));
    /* result:
        Failed CI/CD! 
        { 
            killed: false, 
            code: 1, 
            signal: null, 
            cmd: "set -euo pipefail\nnpm test" 
            stdout: "... test results ..."
            stderr: "... failing test ..."
        } 
    */

行为

  • 执行承诺将拒绝下列情况:
    • 使用未定义环境变量
    • 命令,在管子或其他处,返回除 0以外的退出代码(甚至 grep无果而作则会如此)
  • 结果总是一串.
  • 进程不交互,也不打印到屏幕上. 如果一个进程正在等待用户输入,则承诺永远不会解决/拒绝.

为什么?

巴什有许多陷阱. 随着代码组织日益依赖CI/CD脚本或内部工具进行日常工作,这种依赖也日益增长。 大多数组织用纯巴什语写这些剧本. 大部分阅读/写这些剧本的人会发现自己是那些反倒直观的巴什陷阱的受害者. 这个模块旨在做出一些非常有见解的决定,使其用户免受这些常见错误的80%.

这个工具意图解决的每个问题都只能使用 Bash 解决方案来解决,但它需要学习时间 大多数开发者不会投资. 使用这个模块,你可以使用你已经熟悉的语言特征,而不是学习过时和不直观的巴什解决方案.

常见巴什陷阱

只有最后的错误才算数

问题:

npm install
npm test # this contains a failing test, the exit code here is nonzero
npm publish # Bash continues to run the script and npm publish will still happen

解决方案 : 执行时运行的任何命令都会随 设置 -e 旗帜和脚本将立即退出,并在任何返回非零退出代码的行上发出拒绝承诺.

失败的中管不算数

问题:

failingCommand | echo "a" # failingCommand does not throw an error because it's in a pipe
echo "b"
# result:
# a
# b

解决方案 : 执行时运行的任何命令都会随 设置 - o 管道故障 旗和脚本将立即退出,并在一个返回非零退出代码的管道链中的任何命令上发出承诺拒绝.

使用未设置的变量不会丢出错误

问题:

rm -rf $BUILD_FOLDER/* # If $BUILD_FOLDER is unset by mistake, this line would delete the entire filesystem

解决方案 : 执行时运行的任何命令都会随 设置 -u 旗和脚本将立即退出,但对于未设置环境变量的任何使用,将否决承诺.

基本内置语言功能,如字符串操控容易出错

问题:

# Trying to extract the Nth word in a string
echo $STRING | awk -v N=$N '{print $N}' # You have to learn awk or other tools

解决方案 : 使用执行.

const { execute, pipe } = require('@getvim/execute');
const N = 5;
execute('echo $STRING', { env: { STRING:'My name is Inigo Montoya'}})
    .then(result => result.split(' ')[N - 1])
    .then(pipe('rev'))
    .then(console.log); // Result: "ayotnoM"

IF-ELSE / 条件性等基本内置语言构造容易出错

问题: 从巴什的语言构造中很容易得到语法错误或出乎意料的行为.

# Checking if a variable is greater than 0
if [$VAR > 0]; then ... # Syntax error, forgot spaces

if [ $VAR > 0 ]; then ... # ">" Means stream redirection

if [[ $VAR > 0 ]]; then ... # ">" Means comparison, but of strings (lexicographs)

if [[ $VAR -gt 0 ]]; then ... # Finally works!

if (( $VAR > 0 )); then ... # Confusingly, this also works...

解决方案 : 执行允许在巴什语背景和JS语背景之间轻松跳出.

const { execute, pipe } = require('@getvim/execute');
execute('wc -l file.txt')
    .then(lineCount => lineCount > 0 ? "Success" : "Failure")
    .then(pipe("tee SomeFile.txt"))

版本与发布记录

@getvim/execute 共发布 7 个版本,首次发布于 2019/11/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0latest2019/12/026 年前
0.12.22019/12/016 年前
0.12.12019/12/016 年前
0.12.02019/12/016 年前
0.11.02019/11/286 年前
0.10.02019/11/286 年前

关于 @getvim/execute 的常见问题

@getvim/execute 是什么?

来自Node.js的极端批判处决

如何安装 @getvim/execute?

在项目目录运行 npm install @getvim/execute,或使用 pnpm add / yarn add / bun add。

@getvim/execute 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@getvim/execute 支持 ESM 还是 CommonJS?

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

@getvim/execute 可以商用吗?

@getvim/execute 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@getvim/execute 的下载量有多少?

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

@getvim/execute 还在维护吗?

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