Search code examples
reactjsnpmnpm-run

NPM/React: copyfiles not running correctly


Django 2.0, Node.js 8.11.4

I have a djanog project of the format:

reactify/
└── src/
    ├── reactify-ui/
    |   ├── build/
    |   |   └── static/
    |   |       └── js/
    |   |           └── main.3425.js
    |   └── package.json
    └── staticfiles/
        └── js/
            └── reactify-django-us.js

I want to replace the content of \reactify\src\staticfiles\js with what is in \reactify\src\reactify-ui\build\static\js\*

My packages.json looks like this

    "scripts": {
     ...
    "copy-build-js": "copyfiles -f 'build/static/js/*.js' '../staticfiles/js/'",,
     ...
  }

When I run npm copy-build-js I get the following output:

> reactify-ui@0.1.0 copy-build-js    C:\Users\Owner\dev\reactifydjango\src\reactify-ui
> copyfiles -f 'build/static/js/*.js' '../staticfiles/js/'

It looks like it works, but when I inspect the file in the target location ../staticfiles/js/, it hasn't changed. I validate it by

changing the file before I run the command,
do an `ls -lrth` to get the timetamp, 
wait a minute so the timestamp changes, 
run the command `npm copy-build-js`,  
and then doing an `ls -lrth` on the target location and seeing that the timestamp isn't post hasn't changed.
I also look at the file and it is the same.

Why would the copyfiles not work?


Solution

  • You have the structure correct, but the copyfiles package has some quirks that you missed. Use this as your script:

    "copy-build-js": "del /F \"../staticfiles/js\" && copyfiles -E -f \"./build/static/js/*.js\" ../staticfiles/js"

    The double quotes here are required, so you need black slashes before each to escape them.

    The copyfiles package does not have an option to replace all files in a directory even if the filenames don't exist, so first you have to run del /F \"../staticfiles/js\" to delete all files in the src/staticfiles/js directory. This command assumes you're on Windows.

    Then you run copyfiles -E -f \"./build/static/js/*.js\" ../staticfiles/js. What you missed was that when using wildcards/globs with this package (*), you have to double quote the location. If a location doesn't have a wildcard in it, you don't need quotes at all. I added in the -E flag that will throw an error if files aren't copied, which could potentially save you trouble later on.