简体   繁体   English

无法从Windows上的Node.js脚本生成`gcloud app deploy`

[英]Can't spawn `gcloud app deploy` from a Node.js script on Windows

I'm building an Electron application (Node.js) which needs to spawn gcloud app deploy from the application with realtime feedback (stdin/stdout/stderr). 我正在构建一个Electron应用程序(Node.js),该应用程序需要从具有实时反馈(stdin / stdout / stderr)的应用程序中生成gcloud app deploy

I rapidly switched from child_process to execa because I had some issues on Mac OS X with the child_process buffer which is limited to 200kb (and gcloud app deploy sends some big chunk of string > 200kb which crash the command). child_process迅速从child_process切换为child_process因为在Mac OS X上,child_process缓冲区限制为200kb(在gcloud app deploy发送一些大的字符串> 200kb,这会使命令崩溃)时出现了一些问题。

Now, with execa everything seems to work normally on OSX but not on Windows. 现在,有了execa一切似乎都可以在OSX上正常运行,但不能在Windows上正常运行。

The code looks something like this: 代码看起来像这样:

let bin = `gcloud${/^win/.test(process.platform) ? '.cmd' : ''}`

//which: https://github.com/npm/node-which
which(bin, (err, fullpath) => {
  let proc = execa(fullpath, ['app', 'deploy'], {
    cwd: appPath
  })
  proc.stdout.on('data', data => {
    parseDeploy(data.toString())
  })
  proc.stderr.on('data', data => {
    parseDeploy(data.toString())
  })
  proc.then(() => {
    ...
  }).catch(e => {
    ...
  })
})

This code works perfectly on Mac OS X while I haven't the same result on Windows 这段代码在Mac OS X上可以完美运行,而在Windows上却无法获得相同的结果

I have tried lots of thing: 我尝试了很多事情:

  • execa() execa()
  • execa.shell() execa.shell()
  • options shell:true 选项壳:真
  • I tried maxBuffer to 1GB (just in case) 我尝试将maxBuffer设置为1GB(以防万一)
  • It works with detached:true BUT I can't read stdout / stderr in realtime in the application as it prompts a new cmd.exe without interaction with the Node.js application 它适用于detached:true但我无法在应用程序中实时读取stdout / stderr,因为它提示一个新的cmd.exe而没有与Node.js应用程序进行交互
  • Lots of child_process variant. 很多child_process变体。

I have made a GIST to show the responses I get for some tests I have done on Windows with basic Child Process scripts: https://gist.github.com/thyb/9b53b65c25cd964bbe962d8a9754e31f 我做了一个GIST,以显示我在Windows上使用基本的子进程脚本进行的一些测试所得到的响应: https : //gist.github.com/thyb/9b53b65c25cd964bbe962d8a9754e31f

I also opened an issue on execa repository: https://github.com/sindresorhus/execa/issues/97 我还在execa存储库上打开了一个问题: https : //github.com/sindresorhus/execa/issues/97

Does someone already got this issue ? 有人已经遇到这个问题了吗? I've searched around and found nothing promising except this reddit thread which doesn't solve this issue. 我四处搜寻,除了没有解决这个问题的Reddit线程外,没有其他希望。

Behind the scene, gcloud.cmd is running a python script. 在后台,gcloud.cmd正在运行python脚本。 After reading tons of Node.js issue with ChildProcess / Python and Windows, I fell on this thread: https://github.com/nodejs/node-v0.x-archive/issues/8298 在阅读了有关ChildProcess / Python和Windows的大量Node.js问题之后,我落入了这个线程: https : //github.com/nodejs/node-v0.x-archive/issues/8298

There is some known issue about running Python scripts from a Node.js Child Process. 关于从Node.js子进程运行Python脚本存在一些已知问题。 They talk in this comment about an unbuffered option for python. 他们在此评论中谈到python的无缓冲选项。 After updating the shell script in gcloud.cmd by adding the -u option, I noticed everything was working as expected 通过添加-u选项更新gcloud.cmd的shell脚本后,我注意到一切都按预期工作

This comment explains how to set this option as an environment variable (to not modify the windows shell script directly): https://docs.python.org/2/using/cmdline.html#envvar-PYTHONUNBUFFERED 注释说明了如何将此选项设置为环境变量(不直接修改Windows Shell脚本): https : //docs.python.org/2/using/cmdline.html#envvar-PYTHONUNBUFFERED

So adding PYTHONUNBUFFERED to the environment variable fix this issue ! 因此,将PYTHONUNBUFFERED添加到环境变量可以解决此问题!

execa(fullpath, ['app', 'deploy'], {
  cwd: appPath,
  env: Object.assign({}, process.env, {
    PYTHONUNBUFFERED: true
  })
})

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

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