"I assume that it wouldn't work since copy
statement is native on windows but not on other OS"
Yes, your assumption is correct.
On UNIX-like platforms (Linux, macOS, etc...) the cp
command is analogous to the copy
command available on Windows.
For cross-platform compatibility consider utilizing either of the following two solutions - both essentially use node.js to copy the file (personally I'd choose "Solution A").
Solution A: Using a third party package
Utilize the shx
package which is described as follows:
"shx
is a wrapper around ShellJS Unix commands, providing an easy solution for simple Unix-like, cross-platform commands in npm package scripts."
Firstly cd
to your project directory and run the following command to install it:
npm i -D shx
Then redefine your copy
script in the scripts section of your package.json as follows:
"scripts": {
...
"copy": "shx cp Staticfile build"
}
Note: There are other packages that will meet your cross-platform requirement too, such as; copyfiles.
Solution B: Using a node.js built-in
Alternatively, if you wanted to avoid adding more dependencies to your project you could consider using the node.js built-in fs
module. For example the following script utilizes the fs.readFileSync()
and fs.writeFileSync()
methods.
"scripts": {
...
"copy": "node -e \"const fs = require('fs'); const data = fs.readFileSync('Staticfile'); fs.writeFileSync('build/Staticfile', data);\""
}
This copy
script (above) utilizes the node.js command line option -e
to evaluate the inline JavaScript as follows:
const fs = require('fs');
const data = fs.readFileSync('Staticfile');
fs.writeFileSync('build/Staticfile', data);
Note: If you wanted to take an approach similar to this previous example, i.e. excecute a custom node.js inside an npm script, you should probably consider using the fs.copyFile()
method instead. I've just provided an example using fs.readFileSync()
and fs.writeFileSync()
because I know those methods work with very early versions on node.js - unlike fs.copyFile()
which requires node.js >=8.5.0