我试图运行一个为linux命令行编写的教程中的脚本,但在将其转换为与windows兼容的东西时遇到了错误。这是文章中的一行:
"build": "cd react-spa && yarn build && cd .. && cp -R react-spa/build/ public/ && mv public/index.html public/app.html"
这就是我的
cd client && yarn build && cd .. && xcopy client/build/ public/ /E && ren public/index.html app.html
这是我在终端中收到的错误消息
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:UsersuserAppDataRoamingnpm-cache_logs2020-05-01T05_29_54_552Z-debug.log
我做错了什么?
在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""
注意:前面提到的npm脚本假设您运行的是Windows,并且npm脚本使用的默认shell是cmd.exe
说明:
进行了以下更改,以匹配与原始npmbuild
脚本(即使用*nix命令编写的脚本(相同的行为:
以下
cp
命令:cp -R react-spa/build/ public/
已细化为使用
xcopy
命令,如下所示:xcopy /e/h/y/q "react-spa/build" "public\" > nul 2>&1
选项:
/e
-复制文件夹和子文件夹,包括空文件夹/h
-复制隐藏文件和系统文件及文件夹/y
-取消显示确认覆盖文件的提示/q
-复制时不显示文件名
注意:
每个路径名都被封装在JSON转义的双引号中,即
"..."
public\
部分有一个尾部反斜杠((,它已被JSON转义(
\
(,以通知xcopy
目标是一个目录。这也确保了public
目录在不存在的情况下被创建。> nul 2>&1
部分禁止显示说明复制了多少文件的确认日志。
以下
mv
命令:mv public/index.html public/app.html
已被细化为同时使用
del
和ren
命令,如下所示:del "public\app.html" > nul 2>&1 && ren "public\index.html" "app.html"
注意:
我们首先尝试删除
app.html
文件,以确保后续的ren
命令可以将index.html
文件重命名为app.html
,而不会因为已经存在重复文件而产生任何冲突。我们使用
> nul 2>&1
重定向,以确保在找不到app.html
文件时,即在构建脚本的第一次运行期间找不到它时,我们可以防止任何日志。- 每个路径名都被封装在JSON转义的双引号中,即
"..."
del
和ren
命令中的public\index.html
部分都使用反斜杠分隔符((,该分隔符已被JSON转义(
\
(。而不是正斜杠(/
(