简体   繁体   English

使用 cp.spawnSync 时的正则表达式/shell 参数扩展

[英]Regex/shell parameters expensions when using cp.spawnSync

I'm testing in the command line a jq command:我正在命令行中测试 jq 命令:

jq -f config/jest.jq -s reports/jest/*.json

Which outputs the content of 3 json files located under /reports/jest/ , accordings to the filters I've specified under /config/jest.jq .根据我在/config/jest.jq下指定的过滤器,它输出位于/reports/jest/下的 3 个 json 文件的内容。

If I try to run the same thing within a node script like so however:但是,如果我尝试像这样在节点脚本中运行相同的东西:

  const jq = await cp.spawnSync('jq', ['-f', configPath, '-s', folderPath]);

It fails - jq.stderr tells me he can't finds " /reports/jest/*.json ".它失败了——jq.stderr 告诉我他找不到“ /reports/jest/*.json ”。 If I do this however:但是,如果我这样做:

  const jq = await cp.spawnSync('jq', ['-f', configPath, '-s', 'reports/jest/app.json', 'reports/jest/backend.json', ]);

Then it properly pipes both files into jq.然后它将两个文件正确地通过管道传输到 jq。 Why is my regular expression working in the command line, but not under spawnSync?为什么我的正则表达式在命令行中有效,但在 spawnSync 下却无效? How do I need to adapt it so it reads all my json file & parse it as a single large input?我需要如何调整它才能读取我所有的 json 文件并将其解析为单个大输入?

That's not a regular expression, but a shell glob (or shell wildcard pattern).这不是正则表达式,而是 shell glob(或 shell 通配符模式)。 It is evaluated by your shell (sh, bash, zsh, …).它由您的 shell(sh,bash,zsh,...)评估。 spawnSync is not running a shell, but executing the binary directly, passing all arguments verbatim. spawnSync没有运行 shell,而是直接执行二进制文件,逐字传递所有 arguments。

If you need a shell's behavior, you must execute a shell or resolve the wildcard somehow else.如果您需要 shell 的行为,则必须执行 shell 或以其他方式解析通配符。 Here's a version which uses a shell:这是一个使用 shell 的版本:

const jq = await cp.spawnSync(
  'sh',
  [
    '-c',
    'jq -f ' + configPath + ' -s ' folderPath
  ]);

Or要么

const jq = await cp.spawnSync(
  'sh',
  [
    '-c',
    ['jq -f', configPath, '-s', folderPath].join(' ')
  ]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM