简体   繁体   English

重写 npm 脚本以兼容 windows cmd

[英]Rewrite npm script to be compatible with windows cmd

I'm trying to run a script from a tutorial that was written for a linux command line but I'm running into errors when converting it into something compatible with windows.我正在尝试从为 linux 命令行编写的教程中运行脚本,但是在将其转换为与 windows 兼容的内容时遇到了错误。 This is the the line from the article:这是文章中的一行:

"build": "cd react-spa && yarn build && cd .. && cp -R react-spa/build/ public/ && mv public/index.html public/app.html"

and this is what I have这就是我所拥有的

cd client && yarn build && cd .. && xcopy client/build/ public/ /E && ren public/index.html app.html

This is the error message I get in the terminal这是我在终端中收到的错误消息

Invalid number of parameters npm ERR. code ELIFECYCLE npm ERR. errno 4 npm ERR: api@0.0.0 build. `cd client && yarn build && cd.. && xcopy client/build/ public/ /E && ren public/index.html app.html` npm ERR. Exit status 4 npm ERR. npm ERR: Failed at the api@0:0.0 build script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\user\AppData\Roaming\npm-cache\_logs\2020-05-01T05_29_54_552Z-debug.log

What am I doing wrong here?我在这里做错了什么?

Redefine your build script in package.json as follows:package.json中重新定义build脚本,如下所示:

"build": "cd react-spa && yarn build && cd .. && xcopy /e/h/y/q \"react-spa/build\" \"public\\\" > nul 2>&1 && del \"public\\app.html\" > nul 2>&1 && ren \"public\\index.html\" \"app.html\""

Note: The aforementioned npm script assumes that you are running Windows and the default shell being utilized by npm scripts is cmd.exe . Note: The aforementioned npm script assumes that you are running Windows and the default shell being utilized by npm scripts is cmd.exe .


Explanation:解释:

The following changes were made to match the same behaviour as the original npm build script (ie the one that written using *nix commands):进行了以下更改以匹配与原始 npm build脚本(即使用*nix命令编写的脚本)相同的行为:

  1. The following cp command:以下cp命令:

     cp -R react-spa/build/ public/

    has been refined to utilize the xcopy command as follows:已改进为使用xcopy命令,如下所示:

     xcopy /e/h/y/q \"react-spa/build\" \"public\\\" > nul 2>&1

    Options:选项:

    • /e - Copy folders and subfolders, including empty folders. /e - 复制文件夹和子文件夹,包括空文件夹。
    • /h - Copy hidden and system files and folders. /h - 复制隐藏和系统文件和文件夹。
    • /y - Suppress prompt to confirm overwriting a file. /y - 禁止提示以确认覆盖文件。
    • /q - Do not display file names while copying. /q - 复制时不显示文件名。

    Notes:笔记:

    • Each pathname has been encased in JSON escaped double quotes, ie \"...\"每个路径名都包含在 JSON 转义双引号中,即\"...\"

    • The public\\ part has a trailing backslash ( \ ), which has been JSON escaped ( \\ ), to inform xcopy that the destination is a directory. public\\部分有一个尾部反斜杠( \ ),它已被 JSON 转义( \\ ),以通知xcopy目标是一个目录。 This also ensures the public directory is created if it doesn't already exist.如果public目录不存在,这也可以确保创建它。

    • The > nul 2>&1 part suppresses the confirmation log that states how many files were copied. > nul 2>&1部分禁止显示复制了多少文件的确认日志。

  2. The following mv command:以下mv命令:

     mv public/index.html public/app.html

    has been refined to utilize both del and ren commands as follows:已改进为使用delren命令,如下所示:

     del \"public\\app.html\" > nul 2>&1 && ren \"public\\index.html\" \"app.html\"

    Notes:笔记:

    • We firstly attempt to delete the app.html file to ensure the subsequent ren command can rename the index.html file to app.html without any conflict due to a duplicate file already existing.我们首先尝试删除app.html文件,以确保后续ren命令可以将index.html文件, app.html不会因重复文件而存在任何冲突。

      We redirect using > nul 2>&1 to ensure we prevent any log when the app.html file cannot be found, ie when it can't be found during the first run of the build script.我们使用> nul 2>&1进行重定向,以确保在app.html文件时(即在第一次运行构建脚本时找不到该文件时)阻止任何日志。

    • Each pathname has been encased in JSON escaped double quotes, ie \"...\" .每个路径名都包含在 JSON 转义双引号中,即\"...\"
    • The public\\index.html parts, in both del and ren commands, use a backslash separator ( \ ), which has been JSON escaped ( \\ ). public\\index.html部分,在delren命令中,使用反斜杠分隔符 ( \ ),它已被 JSON 转义 ( \\ )。 instead of a forward slash ( / ).而不是正斜杠 ( / )。

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

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