如何使用Bash shell命令使npm项目在Windows上工作? [英] How can I make npm projects with Bash shell commands work on Windows?
问题描述
我最近接手的一个项目上有一些NPM脚本,像这样:
I have some NPM scripts on a project i recently took over that go like this:
{
"package": "yarn package-common && mv './dist/APP® TV.wgt' ''./package/$(yarn -s filename)''",
"package-common": "tizen package -t wgt -s APP -- ./dist && rimraf package && mkdir package",
"filename": "cross-env-shell echo APP_${npm_package_version}.wgt",
}
此项目是某人在MAC上编写的。我如何将以下部分转换为可正常运行的Powershell / cmd命令?我还没有发现任何关于此的信息。它运行命令,并将回显的值附加到从其调用的字符串中。
This project was written by someone on a MAC. How can i translate the following part into a functioning Powershell / cmd command? I havent found anything anywhere about this. It runs the command and the echoed value gets appended to the string it got called from.
''./package/$(yarn -s filename)''
甚至更好,除了此程序包可以跨平台运行吗?
Or even better, is there a way besides this package to do it cross-platform?
当前在运行yarn包时在powershell中获得输出的消息是:
Currently the message that gets output in powershell when running yarn package is:
'' was unexpected at this time.
推荐答案
您跨跨平台的基本选择没有平台专用脚本的是:
-
在Windows上也使用Bash :
-
通过
运行npm脚本/ Windows_Subsystem_for_Linux rel = noreferrer> WSL ,并使用通常包含在 package.json
中的现有Bash命令文件,例如您的情况。
Run your npm scripts from Bash via WSL and use the existing Bash commands typically contained in
package.json
files, such as in your case.
或者,使用 安装了Windows版Git ,将npm配置为使用 bash.exe
作为调用命令的外壳-请参见此答案 [npm v5.1 +]。
Alternatively, with Git for Windows installed, configure npm to use bash.exe
as the shell for invoking commands - see this answer [npm v5.1+].
在以下位置安装 PowerShell Core 所有平台(包括Windows),并将命令定义为PowerShell命令(见下文)[npm v5.1 +]。
Install PowerShell Core on all your platforms (including Windows), and define the commands as PowerShell commands (see below) [npm v5.1+].
请注意此处的npm版本版本要求(5.1或更高版本),这是由于使用特定外壳程序的配置选项( script-shell
)在早期版本中不可用。运行 npm -v
获取已安装的版本,并运行
npm install -g npm
进行更新。
Note the npm version version requirement (version 5.1 or higher) where noted, due to the configuration option to use a specific shell (script-shell
) not being available in earlier versions. Run npm -v
to get the installed version, and npm install -g npm
to update, if possible.
-
安装 PowerShell Core。
Install PowerShell Core.
然后将npm配置为使用PowerShell Core作为外壳(需要npm 5.1或更高版本):
Then configure npm to use PowerShell Core as the shell (requires npm version 5.1 or higher):
-
对于 all 您的项目或全局:
npm config set script-shell pwsh [--global]
仅对于给定项目(从该项目的根目录):
For a given project only (from that project's root directory):
npm config set script-shell pwsh --userconfig ./.npmrc
最后,在项目的 package.json
的脚本
键中,定义的命令作为PowerShell命令。
Finally, in your projects' package.json
's scripts
key, define the commands as PowerShell commands.
在当前情况下:
例如,翻译此Bash命令:
For instance, translate this Bash command:
yarn package-common && mv './dist/APP® TV.wgt' "./package/$(yarn -s filename)"
注意:我已将''
替换为
,因为后者更有意义;如最初所写,''
被Bash有效丢弃,命令替换的结果 $(yarn-
Note: I've replaced ''
with "
, because the latter make more sense; as originally written, the ''
are effectively discarded by Bash, and the result of command substitution $(yarn -s filename)
could break the command if it contained whitespace.
此PowerShell命令的
to this PowerShell command:
yarn package-common; if ($?) { mi './dist/APP® TV.wgt' "./package/$(yarn -s filename)" }
注意:
-
从v6.2.0开始,PowerShell缺少Bash样式的
&&
和||
运算符,因此使用if($?)
来显式检查先前命令的成功执行-请参见在GitHub上的建议使PowerShell支持& ;&
和|||
。
As of v6.2.0, PowerShell lacks Bash-style
&&
and||
operators, hence the use ofif ($?)
to check for successful execution of the previous command explicitly - see this suggestion on GitHub for making PowerShell support&&
and||
too.
mi
是 Move-Item
cmdlet的内置别名。
mi
is a built-in alias for the Move-Item
cmdlet.
虽然也可以从PowerShell调用脚本 ,但这不是必需的-从 cmd.exe
或批处理文件中调用
While it makes sense to call your scripts from PowerShell also, that's not a requirement - calling from cmd.exe
or a batch file will work too.
要开始使用PowerShell Core,请参见学习PowerShell ;另外, http://hyperpolyglot.org/shell 将类似于POSIX的shell(例如Bash)的语法与 cmd.exe
和PowerShell以简明的表格形式。
To get started with PowerShell Core, see Learning PowerShell; also, http://hyperpolyglot.org/shell juxtaposes the syntax of POSIX-like shells such as Bash with that of cmd.exe
and PowerShell in concise, tabular form.
这篇关于如何使用Bash shell命令使npm项目在Windows上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!