Reputation: 493
I have a project built on MAC and the package.json script is as follows:
"scripts": {
"copyindex": "cp src/index.html build",
"copycss": "cp src/cds.css build",
"copyimages": "cp src/assets/images/** build/assets/images",
"copyfonts": "cp src/fonts/** build/fonts",
"copywc": "cp -r node_modules/@webcomponents/webcomponentsjs/bundles build && cp node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js build",
"build": "rm -rf build && mkdir build && npm run copyindex && npm run copycss && mkdir build/assets && mkdir build/assets/images && npm run copyimages && mkdir build/fonts && npm run copyfonts && npm run copywc && rollup -c",
"start": "serve build"
},
Its running fine on MAC when run npm start. I am using windows and it doesn't run my PC. I changed most of the commands to windows commands as below. But, it still doesn't work. Is there anything else I need to do? I am getting the errors as command is not recognized.
Error: 'rm' is not recognized as an internal or external command,
operable program or batch file.
"scripts": {
"copyindex": "copy src/index.html build",
"copycss": "copy src/cds.css build",
"copyfonts": "copy src/fonts/** build/fonts",
"copywc": "copy -r node_modules/@webcomponents/webcomponentsjs/bundles build && copy node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js build",
"build": "rm -rf || rmdir /s build && mkdir build && npm run copyindex && npm run copycss && mkdir build/fonts && npm run copyfonts && npm run copywc && rollup -c",
"start": "serve build"
},
I don't want to remove the MAC commands. I want the npm script to work in both windows and MAC
Upvotes: 1
Views: 1751
Reputation: 191
The simplest way to achieve that is probably by using shx, which can run Unix commands on any platform.
"scripts": {
"copyindex": "shx cp src/index.html build"
// ...
},
"devDependencies": {
"shx": "latest"
}
Alternatively, you can also install individual packages that provide a single command, such as copyfiles for cp
, rimraf for rm -rf
, etc. There are many of them.
Another approach would be to write these scripts in Javascript directly and run them with Node. This can be an arguably better solution when your scripts get very long. It also doesn't require you to depend on any additional package to run your scripts.
Example for copyindex
:
#!/usr/bin/env node
const fs = require("fs")
fs.copyFileSync("src/index.html", "build/index.html")
In your package.json:
"scripts": {
"copyindex": "node scripts/copyindex.js",
// ...
}
Upvotes: 2
Reputation: 21384
The challenge is that the script needs to be able to work on either Mac or Windows but doesn't know which one it is up front. Since scripting in cmd.exe
, the default shell used by npm run
under windows, seems very limited and the intersection of the language understood by cmd.exe
and /bin/sh
(default under Mac) seems virtually empty, I think your best bet is go straight into node
for scripting. This is an example of how this could work:
{
...
"build_win32": "rmdir /s && build && ...",
"build_darwin": "rm -rf && build && ...",
"build": "node -e \"child_process.exec(`npm run build_${os.platform()}`)\""
}
Upvotes: 2