如何在 package.json 的脚本中运行多个 powershell 命令 [英] How to run multiple powershell commands in scripts for package.json

查看:96
本文介绍了如何在 package.json 的脚本中运行多个 powershell 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是一个接一个地运行两个命令来构建 Electron.我使用 Powershell 而不是 bash 来实现这一点,所以我最终可以构建一个 win32 二进制文件.

My goal is to run two commands one after another to build Electron. I am using Powershell, not bash to achieve this so I can, ultimately, build a win32 binary.

通过 Powershell 运行:

Running via Powershell:

PS C:\Users\digit\Dropbox\Programming\project\app> npm run build:fake --verbose
npm info it worked if it ends with ok
npm verb cli [ 'C:\\Program Files\\nodejs\\node.exe',
npm verb cli   'C:\\Users\\digit\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js',
npm verb cli   'run',
npm verb cli   'build:fake',
npm verb cli   '--verbose' ]
npm info using npm@6.9.0
npm info using node@v11.11.0
npm verb run-script [ 'prebuild:fake', 'build:fake', 'postbuild:fake' ]
npm info lifecycle app@1.0.0~prebuild:fake: app@1.0.0
npm info lifecycle app@1.0.0~build:fake: app@1.0.0

> app@1.0.0 build:fake C:\Users\digit\Dropbox\Programming\project\app
> echo 'test'; echo 'test2'

'test'; echo 'test2'

npm verb lifecycle app@1.0.0~build:fake: unsafe-perm in lifecycle true
npm verb lifecycle app@1.0.0~build:fake: PATH: C:\Users\digit\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;C:\Users\digit\Dropbox\Programming\project\app\node_modules\.bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\libnvvp;C:\Python27\;C:\Python27\Scripts;C:\Program Files (x86)\Razer Chroma SDK\bin;C:\Program Files\Razer Chroma SDK\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\WINDOWS\system32\config\systemprofile\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Git\cmd;C:\ProgramData\chocolatey\bin;C:\Program Files\nodejs\;C:\Program Files (x86)\Yarn\bin\;C:\Program Files\doxygen\bin;C:\Program Files\CMake\bin;C:\Program Files\NVIDIA Corporation\Nsight Compute 2019.1\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Users\digit\AppData\Local\Microsoft\WindowsApps;C:\Users\digit\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\digit\AppData\Local\hyper\app-2.1.2\resources\bin;C:\Users\digit\AppData\Roaming\npm;C:\Users\digit\AppData\Local\Yarn\bin;C:\Program Files\NASM;C:\Program Files\nodejs;C:\MinGW\bin;C:\MinGW\msys\1.0;
npm verb lifecycle app@1.0.0~build:fake: CWD: C:\Users\digit\Dropbox\Programming\project\app
npm info lifecycle app@1.0.0~postbuild:fake: app@1.0.0
npm verb exit [ 0, true ]
npm timing npm Completed in 200ms
npm info ok
PS C:\Users\digit\Dropbox\Programming\project\app> wsl npm run build:fake

出于某种原因,当它应该运行由分号分隔的每个命令时,它会回显整个命令.

For some reason it echos the whole command instead when it should run each command delimited by a semi-colon.

在 Ubuntu 上运行这个工作正常:

Running this on Ubuntu works fine:


> app@1.0.0 build:fake /mnt/c/Users/digit/Dropbox/Programming/project/app
> echo 'test'; echo 'test2'

test
test2

我正在使用 https://www.npmjs.com/package/run-脚本操作系统

操作系统:Windows 10

OS: Windows 10

推荐答案

npm 默认使用 cmd.exe 在 Windows 上运行脚本 - 无论您调用 npm 的 shell 是什么 可执行 来自,它解释了您的症状.[1]

npm uses cmd.exe to run scripts on Windows by default - irrespective of what shell you happen to invoke the npm executable from, which explains your symptom.[1]

如果您希望 npm 使用 PowerShell 来运行脚本,请使用 (npm v5.1+):

If you want npm to use PowerShell to run scripts with, use (npm v5.1+):

Windows PowerShell:

Windows PowerShell:

npm config set script-shell powershell

PowerShell 核心:

PowerShell Core:

npm config set script-shell pwsh

注意:以上配置 PowerShell 以用于所有您的项目;您也可以针对每个项目执行此操作 - 请参阅此答案.

Note: The above configures PowerShell for use with all your projects; you can also do it on a per-project basis - see this answer.

[1] ; 不是 cmd.exe 中的句号分隔符,而 '...'-quoting 是未能识别;因此,echo 命令解释了 'test';echo 'test2' 作为要回显的文字字符串.

[1] ; is not a stament separator in cmd.exe, and '...'-quoting is not recognized; therefore, the echo command interpreted 'test'; echo 'test2' as a literal string to echo.

这篇关于如何在 package.json 的脚本中运行多个 powershell 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆