I am trying out npm as a build tool.
One stumbling block I have encountered is that I need to copy javascript files from one folder to another. The source folder contains typescript files, javascript files and map files, but in the target folder I am only interested in javascript files.
I do not want to make a copy-statement for each file, but would like to copy all .js files. Also, my source folder contains subfolders that also contains javascript files. These need to be copied as well, and maintain the subfolder structure.
What I have tried is using NCP with a filter, but I cannot get the filter to work. I have tested the regex used in the filter and it appears to work fine. The test was done at Regex Tester with regular expression .*\.js$
and test-strings like main.ts
, main.js
main.js.map
etc, and only the .js strings were matched.
My package json contains the following (abbreviated):
{ "scripts": { "copy": "ncp scripts wwwroot/scripts --filter=\".*(\\\\.js$)\"" }, "devDependencies": { "ncp": "2.0.0.0" } }
Since my regex is in a string in a string I have double-escaped it. I have also tried other variations, for example:
--filter=/.*\.js$/g - compilation error --filter=/.*\\.js$/g - no files copied --filter=\".*\\.js$\" - no files copied --filter=\"/.*\\.js$/g\" - no files copied (no filter) - all files copied
I am in no way married to NCP. If something else works better then I will use that.
So: How do I, inside package.json's scripts section copy only files with a speific extension to another folder? I am pretty sure I have overlooked something blindingly obvious...
Scripts are stored in a project's package. json file, which means they're shared amongst everyone using the codebase. They help automate repetitive tasks, and mean having to learn fewer tools. Node npm scripts also ensure that everyone is using the same command with the same flags.
You can add dependencies to a package. json file from the command line or by manually editing the package.
Your package. json holds important information about the project. It contains human-readable metadata about the project (like the project name and description) as well as functional metadata like the package version number and a list of dependencies required by the application.
Warning! The cpx package appears to be abandoned. cpy-cli, copyfiles, and other solutions are listed in comments here or answers, below.
cpx might be a good substitution.
It has a CLI, allows you to use globs instead of regex, can preserve the directory tree, and is relatively up-to-date as I write this....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With