28

I am trying to add a second part to my npm bundle script. The first part runs great, however I am trying to copy in 3 files along with the bundle.

So right now I have :

"bundle": "NODE_ENV=production webpack --output-file bundledFile.js && cp package.json dist/",

The NODE_ENV=production webpack --output-file bundledFile.js works great by itself. The part that is not working is the && cp package.json dist/, I would like the script to copy my package.json (along with 2 other files actually, but just starting with this one) to the dist folder. Brand new to these scripts, any idea how to fix? Appreciate any advice, thanks!

ajmajmajma
  • 12,868
  • 20
  • 72
  • 126
  • 1
    Careful with using `cp` from the command line, as that won't typically be compatible for windows users (might not be a concern for you, though). Other than that, this looks right to me. You should be able to chain package.json scripts with `&&`. Are you getting any specific error? What happens if you make the two items two separate tasks, and then chain them instead? (`npm run build:bundle && npm run build:package-json` or something similar). – dvlsg Aug 09 '16 at 19:18
  • @dvlsg thanks for the response, would you recommend something other than cp? also can I use the same syntax for multiple single files ( i want to add the readme and npmrc for example) – ajmajmajma Aug 09 '16 at 19:20
  • 1
    If you're copying multiple files, and webpack doesn't support what you need (not sure about that), I would consider adding a build utility file. You don't need to go all the way to `gulp` or anything (although I do love `gulp`) -- you could just create a node file called `copy-files.js` and have it use the `fs` package to copy files. That would be (mostly) cross-platform since it's using node to handle copying files. Then you could call it with just `node copy-files.js` in your npm scripts. You'd still have to assume that `node` is available, but that's not too far of a stretch. – dvlsg Aug 09 '16 at 19:26
  • @dvlsg that's perfect if you want to add that as an answer, that is exactly what I am looking for. – ajmajmajma Aug 09 '16 at 19:28
  • I can add it as an answer, but maybe first check to see if splitting up the commands into two tasks `npm run build:bundle`, `npm run build:package-json`) and then setting `build: npm run build:bundle && npm run build:package-json` works for you. I still think we're going to have a problem with chaining commands, for whatever reason. – dvlsg Aug 09 '16 at 19:28
  • 1
    @dvlsg I added `&& npm run copy` , and the copy script looks like, `"cp package.json dist/ && cp README.md dist/ && cp .npmrc dist/"` , this seems to work great. – ajmajmajma Aug 09 '16 at 19:32

4 Answers4

41

The syntax should work (and seems to, looking at your comments). I would suggest splitting your npm scripts across multiple points, though:

{
  "bundle": "NODE_ENV=production webpack --output-file bundledFile.js",
  "copy": "cp package.json dist/ && cp README.md dist/ && cp .npmrc dist/",
  "build": "npm run bundle && npm run copy"
}

In order to be cross-platform compatible (cp is not typically available on windows), I would also suggest adding a build file somewhere such as ./tools/copy-distrubution-files.js which would make use of fs to copy the necessary files, then call it in the npm scripts with node ./tools/copy-distribution-files.js. That will be (mostly) platform independent (you still have to assume that node is available as the nodejs executable, but that seems fairly reasonable to me).

Community
  • 1
  • 1
dvlsg
  • 4,988
  • 2
  • 28
  • 34
4

If you're running on windows use the following command:

"copy": "copy \"package.json\" \"dist\" && copy \"README.md\" \"dist\" && copy \".npmrc\" \"dist\""

copy instead of cp. Don't forget to use "" for each path (escape them with \ within the quoted command). and if you need to define a long path, don't use / (slashes) but \ (backslashes)

like:

copy "devices\\VS-88UT\\index.html" "devices\\VS-88UT\\dist"

Also, if you prefere ther is a nice plugin to run bash command before and after each build

Wang Tang
  • 509
  • 6
  • 16
IsraGab
  • 4,293
  • 3
  • 23
  • 40
4

The quickest way for me was to reference powershell in a package.json script like this:

"copyFile": "@powershell copy './source/package.json' './deploy'",
Dustin Spengler
  • 3,383
  • 4
  • 21
  • 32
0

To copy folders and files in windows just use

xcopy git\\* dist\\ /e /i /h

I think this might help someone.

sisco
  • 29
  • 3