简体   繁体   English

如何在 package.json 的脚本中运行多个 powershell 命令

[英]How to run multiple powershell commands in scripts for package.json

My goal is to run two commands one after another to build Electron.我的目标是一个接一个地运行两个命令来构建 Electron。 I am using Powershell, not bash to achieve this so I can, ultimately, build a win32 binary.我正在使用 Powershell 而不是 bash 来实现这一点,因此我最终可以构建一个 win32 二进制文件。

Running via Powershell:通过 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.出于某种原因,当它应该运行由分号分隔的每个命令时,它会回显整个命令。

Running this on Ubuntu works fine:在 Ubuntu 上运行它可以正常工作:


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

test
test2

I am utilizing https://www.npmjs.com/package/run-script-os我正在使用https://www.npmjs.com/package/run-script-os

OS: Windows 10操作系统:Windows 10

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.默认情况下,npm 使用cmd.exe在 Windows 上运行脚本 - 无论您碰巧哪个 shell 调用npm可执行文件,这解释了您的症状。 [1] [1]

If you want npm to use PowerShell to run scripts with, use (npm v5.1+):如果您希望 npm 使用PowerShell来运行脚本,请使用 (npm v5.1+):

Windows PowerShell: Windows PowerShell:

npm config set script-shell powershell

PowerShell Core: PowerShell 核心:

npm config set script-shell pwsh

Note: The above configures PowerShell for use with all your projects;注意:以上配置了 PowerShell 以用于您的所有项目; you can also do it on a per-project basis - see this answer .您也可以在每个项目的基础上进行 - 请参阅此答案


[1] ; [1] ; is not a stament separator in cmd.exe , and '...' -quoting is not recognized;不是cmd.exe的句号分隔符,并且无法识别'...' -quoting; therefore, the echo command interpreted 'test'; echo 'test2'因此, echo命令解释了'test'; echo 'test2' 'test'; echo 'test2' as a literal string to echo. 'test'; echo 'test2'作为要回显的文字字符串。

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

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