如何使用 package.json 脚本复制具有特定文件扩展名的文件 [英] How to use package.json scripts to copy files with specific file extension

查看:56
本文介绍了如何使用 package.json 脚本复制具有特定文件扩展名的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 npm 作为构建工具.

I am trying out npm as a build tool.

我遇到的一个绊脚石是我需要将 javascript 文件从一个文件夹复制到另一个文件夹.源文件夹包含打字稿文件、javascript 文件和地图文件,但在目标文件夹中我只对 javascript 文件感兴趣.

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.

我不想为每个文件做一个复制声明,而是想复制所有 .js 文件.此外,我的源文件夹包含也包含 javascript 文件的子文件夹.这些也需要复制,并保持子文件夹结构.

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.

我尝试过将 NCP 与过滤器一起使用,但我无法让过滤器正常工作.我已经测试了过滤器中使用的正则表达式,它似乎工作正常.测试在 Regex Tester 使用正则表达式 .*\.js$ 完成并测试-strings 像 main.ts, main.js main.js.map 等,并且只匹配 .js 字符串.

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.

我的包json包含以下内容(缩写):

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

我绝不与 NCP 结婚.如果其他东西效果更好,那么我会使用它.

I am in no way married to NCP. If something else works better then I will use that.

那么:我如何在 package.json 的脚本部分中仅将具有特定扩展名的文件复制到另一个文件夹?我很确定我忽略了一些非常明显的东西......

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...

推荐答案

警告!cpx 包似乎已被放弃.cpy-cli、copyfile 和其他解决方案列在此处的评论或下面的答案中.

cpx 可能是一个很好的替代品.

cpx might be a good substitution.

它有一个 CLI,允许你使用 globs 而不是正则表达式,可以保留目录树,并且在我写这篇文章时是最新的......

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....

这篇关于如何使用 package.json 脚本复制具有特定文件扩展名的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆