[英]configuring package.json on windows
I'm trying to manage the configuration of a react project on windows, it was previously running on mac.我试图在 windows 上管理一个反应项目的配置,它以前在 mac 上运行。 I'm using
yarn build
.我正在使用
yarn build
。 inside the package.json
scripts>build
was configured as "rm-rf deployment/static;react-scripts build && mv build deployment/static"
.在
package.json
scripts>build
中配置为"rm-rf deployment/static;react-scripts build && mv build deployment/static"
。 since the rm-rf
and mv
commands are for Linux, I tried using rmdir/del
and move
instead.. but it doesn't seem to work.因为
rm-rf
和mv
命令是针对 Linux 的,所以我尝试使用rmdir/del
并改为move
……但它似乎不起作用。 I'm getting the error: Parameter format not correct - "static"
.我收到错误:
Parameter format not correct - "static"
。
The equivalent of that build
script running via Command Prompt or PowerShell on Windows is:在 Windows 上通过命令提示符或PowerShell运行的
build
脚本的等效项是:
"scripts": {
"build": "rd /s/q \"deployment/static\" 2> nul & react-scripts build && md \"deployment/static\" && move \"build\" \"deployment/static\""
}
Explanation:解释:
rm-rf
equivalent for Windows (cmd.exe) is rd /s/q
rm-rf
等价物是rd /s/q
mv
equivalent for Windows (cmd.exe) is move
mv
等效项是move
All directory paths have been wrapped in escaped double quotes \\"...\\"
.所有目录路径都包含在转义双引号
\\"...\\"
。 For example;例如;
deployment/static
has been rewritten as \\"deployment/static\\"
. deployment/static
已被重写为\\"deployment/static\\"
。
Although escaped double quotes are not entirely necessary in this scenario, it's good practice and necessary to do this when paths may include spaces or punctuation characters.虽然在这种情况下转义双引号不是完全必要的,但当路径可能包含空格或标点字符时,这是一种很好的做法并且有必要这样做。
The semi-colon ;
分号
;
has been replaced with the single &
operator to ensure the react-script build
part runs regardless of whether the initial rd /s/q ...
command fails or succeeds.已被替换为单个
&
运算符,以确保无论初始rd /s/q ...
命令失败还是成功, react-script build
部分都会运行。
The following error message would be printed to the console when using rd
to delete a folder/path which may not exist:使用
rd
删除可能不存在的文件夹/路径时,控制台将打印以下错误消息:
The system cannot find the path specified
该系统找不到指定的路径
To prevent this error message from potentially being printed to the console we redirect the error message to NUL
using the 2> nul
part.为了防止此错误消息可能被打印到控制台,我们使用
2> nul
部分将错误消息重定向到NUL
。
md \\"deployment/static\\"
part utilizes Windows md
command to make the static
directory - which is very similar to the mkdir
command in bash. md \\"deployment/static\\"
部分利用 Windows md
命令创建static
目录 - 这与 bash 中的mkdir
命令非常相似。 Note: The above syntax will fail on nix based operating systems such as macOS and Linux.注意:上述语法在基于nix的操作系统(例如 macOS 和 Linux)上将失败。
To achieve a cross platform solution, (ie one which runs successfully on Windows, Linux, and macOS), I suggest writing two Nodejs utility scripts to substitute the rm -rf
and mv
bash commands.为了实现跨平台解决方案(即在 Windows、Linux 和 macOS 上成功运行的解决方案),我建议编写两个 Nodejs 实用程序脚本来替代
rm -rf
和mv
bash 命令。 These two Nodejs scripts can then be invoked via your npm-script.然后可以通过 npm-script 调用这两个 Nodejs 脚本。
The following steps describe how this can be achieved.以下步骤描述了如何实现这一点。
Install shelljs which provides portable Unix shell commands for Nodejs.安装shelljs ,它为Nodejs提供可移植的 Unix shell 命令。 To do this,
cd
to your project directory an run the following command:为此,请
cd
到您的项目目录并运行以下命令:
npm i -D shelljs
Create a new Nodejs script named rm.js
with the following content:使用以下内容创建一个名为
rm.js
的新 Nodejs 脚本:
rm.js rm.js
const shell = require('shelljs'); const args = process.argv.slice(2); const dir = args[0]; shell.rm('-rf', dir);
Save this file in the root of your project directory, at the same level as where your projects package.json
is stored.将此文件保存在项目目录的根目录中,与存储项目
package.json
级别相同。
Create a another Nodejs script named mv.js
with the following content:使用以下内容创建另一个名为
mv.js
Nodejs 脚本:
mv.js mv.js
const shell = require('shelljs'); const args = process.argv.slice(2); const src = args[0]; const dest = args[1]; // Check src path has been provided and is valid if (!src || !shell.test('-d', src)) { console.log('\\x1b[31m\\x1b[40mERR!\\x1b[0m src path cannot be found: %s', src); process.exit(1); } // Check dest path has been provided. if (!dest) { console.log('\\x1b[31m\\x1b[40mERR!\\x1b[0m dest path must be provided:'); process.exit(1); } // Make dest directory if necessary. shell.mkdir('-p', dest); // Move the file. shell.mv(src, dest);
Also save this file in the root of your project directory, at the same level as where your projects package.json
is stored.还要将此文件保存在项目目录的根目录中,与存储项目
package.json
的位置相同。
Then configure your build
script in package.json
as follows:然后在
package.json
配置您的build
脚本,如下所示:
"scripts": { "build": "node rm \\"deployment/static\\" & react-scripts build && node mv \\"build\\" \\"deployment/static\\"" }
Note笔记
The two utility scripts rm.js
and mv.js
are invoked in the npm-script
named build
via the parts reading;两个实用程序脚本
rm.js
和mv.js
在名为build
的npm-script
通过部件读取调用; node rm ...
and node mv ...
respectively. node rm ...
和node mv ...
分别。
If you decide to store these two scripts in a different folder instead of the projects root directory, (as suggested in steps 2 and 3 previously), then you'll need to change the paths to the files.如果您决定将这两个脚本存储在不同的文件夹而不是项目根目录中(如前面第 2 步和第 3 步所建议的那样),那么您需要更改文件的路径。 For example;
例如; if they were both saved in a folder named
scripts
which is located in the root of your project directory then your build
script would be changed to:如果它们都保存在位于项目目录根目录下的名为
scripts
的文件夹中,那么您的build
脚本将更改为:
"scripts": {
"build": "node scripts/rm \"deployment/static\" & react-scripts build && node scripts/mv \"build\" \"deployment/static\""
}
An alternative cross-platform solution, (which wasn't available when originally posting this answer), is to utilize the shx package, which is described as:另一种跨平台解决方案(最初发布此答案时不可用)是利用shx包,其描述为:
shx
is a wrapper around ShellJS Unix commands, providing an easy solution for simple Unix-like, cross-platform commands in npm package scriptsshx
是ShellJS Unix 命令的包装器,为 npm 包脚本中简单的类 Unix、跨平台命令提供简单的解决方案
Run the following command to install shx
:运行以下命令安装
shx
:
npm i -D shx
Then change your build script in package.json
as follows:然后在
package.json
更改您的构建脚本,如下所示:
"scripts": { "build": "shx rm -rf deployment/static & react-scripts build && shx mv build deployment/static" }
for Windows:对于 Windows:
in client folder在客户文件夹中
},
"scripts": {
"build-win": "react-scripts build && move build ..\\server\\public",
},
that will move folder to server folder这会将文件夹移动到服务器文件夹
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.