我试图运行一个为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! [email protected] 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 [email protected] 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
我到底做错了什么?
重新定义你的 build
脚本中 软件包.json 如下。
"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。 上述的npm脚本假设你运行的是Windows,并且npm脚本使用的默认shell是 cmd.exe
.
为了与原来的npm的行为保持一致,我们做了以下修改。build
脚本(即使用 *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 \"public\\app.html\" > nul 2>&1 && ren \"public\\index.html\" \"app.html\"
备注。
我们首先尝试删除 app.html
档,以确保后续的 ren
命令可以重命名 index.html
归档 app.html
而不会因为已经存在重复的文件而产生任何冲突。
我们使用 > nul 2>&1
以确保我们防止任何日志时 app.html
文件找不到,即在第一次运行构建脚本时找不到。
\"...\"
.public\\index.html
部分,在两个 del
和 ren
命令,使用反斜杠分隔符(\
),它已经被JSON转义(\\
),而不是前斜杠(/
).