如何使用 Bash shell 命令使 npm 项目在 Windows 上工作? [英] How can I make npm projects with Bash shell commands work on Windows?

查看:41
本文介绍了如何使用 Bash shell 命令使 npm 项目在 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 package时powershell输出的信息是:

Currently the message that gets output in powershell when running yarn package is:

'' was unexpected at this time.

推荐答案

在没有平台特定脚本的情况下跨平台的基本选择 是:

Your basic choices for going cross-platform without platform-specific scripts are:

  • 在 Windows 上也使用 Bash:

  • 从 Bash 通过WSL运行您的 npm 脚本> 和使用现有的 Bash 命令通常包含在 package.json 文件中,例如在您的情况下.

  • 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.

或者,安装 Git for Windows, 将 npm 配置为使用 bash.exe 作为调用命令的 shell - 参见 这个答案 [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(核心)(包括 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 或更高版本),因为使用特定 shell (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.

  • Install PowerShell Core.

然后配置 npm 以使用 PowerShell (Core) 作为 shell(需要 npm 5.1 或更高版本):

Then configure npm to use PowerShell (Core) as the shell (requires npm version 5.1 or higher):

  • 对于所有您的项目(当前用户),或全局(所有用户):

  • For all your projects (current user), or globally (all users):

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.jsonscripts 键中,将命令定义为 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 -s filename) 可能会破坏命令,如果它包含空格.

    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 命令 (v7+):

    to this PowerShell command (v7+):

    yarn package-common && mi './dist/APP® TV.wgt' "./package/$(yarn -s filename)"
    

    注意:

    • mi is a built-in alias for PowerShell's 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(核心),请参阅 学习PowerShell;此外,http://hyperpolyglot.org/shell 将 Bash 等 POSIX 类 shell 的语法与它并列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屋!

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