@ 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"))