javascript - Using NPM ShellJS to execute a Git command in Node returns an empty string inside `stdout`
问题描述
I'm using NPM ShellJS to execute the following Git command inside a Node script:
git for-each-ref --sort=v:refname --format "tag: %(refname:strip=2) message: %(subject) date: %(authordate:iso)" refs/tags
If I run this command inside a normal command line window, it works perfectly and results in an output similar to this:
tag: v2.20.0-rc0 message: Git 2.20-rc0 date: 2018-11-18 18:25:38 +0900
tag: v2.20.0-rc1 message: Git 2.20-rc1 date: 2018-11-21 23:25:15 +0900
tag: v2.20.0-rc2 message: Git 2.20-rc2 date: 2018-12-01 21:45:08 +0900
However, when I run this command in Node using ShellJS, the contents of stdout
is an empty string:
const shell = require('shelljs');
let tagInfo = shell.exec(`
git for-each-ref --sort=v:refname --format "tag: %(refname:strip=2) message: %(subject) date: %(authordate:iso)" refs/tags
`);
console.log('tagInfo', tagInfo.stdout); // empty string
How do I get the output as specified about, instead of the empty string?
解决方案
在您的情况下,您使用的是多行字符串 - 它不太正确。尝试提供一个字符串(一行):
let tagInfo = shell.exec(`git for-each-ref --sort=v:refname --format "tag: %(refname:strip=2) message: %(subject) date: %(authordate:iso)" refs/tags`);
推荐阅读
- flutter - 在 Flutter(Dart 2)中使用 const 关键字是否会更好地提高性能?
- xml - Eclipse 在红色圆圈中显示令人讨厌的白色十字,没有任何消息
- php - 显示记录时无法触发vue-select中选择的数据
- perl - Perl:从考虑夏令时的纪元开始以秒为单位输入时间时,获取 gmtime 和本地时间之间的偏移量
- sql-server - sintaxis 如何使用 ignore_dup_key?
- c# - 与 c# 字典插入相比,为什么 std::map 插入这么慢?
- ios - 拒绝时显示麦克风权限
- c# - 在带有 MVVM 的 WPF 中,在 ListBox 中显示 List<> 成员
- python - 如何转换列表以返回 Json?
- c - C:将文件中的汽车艺术保存在二维字符数组中