简体   繁体   English

在 windows 上配置 package.json

[英]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-rfmv命令是针对 Linux 的,所以我尝试使用rmdir/del并改为move ……但它似乎不起作用。 I'm getting the error: Parameter format not correct - "static" .我收到错误: Parameter format not correct - "static"

Windows Solution (cmd.exe) Windows 解决方案 (cmd.exe)

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:解释:

  1. The rm-rf equivalent for Windows (cmd.exe) is rd /s/q Windows (cmd.exe) 的rm-rf等价物是rd /s/q
  2. The mv equivalent for Windows (cmd.exe) is move Windows (cmd.exe) 的mv等效项是move
  3. 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.虽然在这种情况下转义双引号不是完全必要的,但当路径可能包含空格或标点字符时,这是一种很好的做法并且有必要这样做。

  4. 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部分都会运行。

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

  6. The 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)上将失败。


Cross Platform Solution (Windows/Linux/macOS...)跨平台解决方案(Windows/Linux/macOS...)

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 -rfmv 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.以下步骤描述了如何实现这一点。

  1. 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
  2. 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级别相同。

  3. 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的位置相同。

  4. 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.jsmv.js在名为buildnpm-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\""
}

Edit / Update:编辑/更新:

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 scripts shxShellJS Unix 命令的包装器,为 npm 包脚本中简单的类 Unix、跨平台命令提供简单的解决方案

  1. Run the following command to install shx :运行以下命令安装shx

     npm i -D shx
  2. 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" }

I use rimraf for this very reason.正是因为这个原因,我才使用rimraf Install it globally全局安装

    npm i -g rimraf

and update your script as follows并按如下方式更新您的脚本

    "rimraf deployment/static;react-scripts build && mv build deployment/static"

The shx package should work very well; shx包应该可以很好地工作; they even show rm -rf in the topmost package.json demo他们甚至在最顶层的 package.json 演示中显示rm -rf

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.

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