图文并茂手把手教你How to copy files or directory in nodejs npm scripts编写脚本用npm或者node命令复制文件

每天都要开心哦~~~

今天来个双语文档

先来说一下npm 执行的方式

1.首先,进入项目目录,下载依赖,添加到dev依赖

1.First, Go to your application project, Install copyfiles dependency with the below command Note: please add this as devDependencies using –save-dev

npm install --save-dev copyfiles

2.把下面代码放到你的package.json,就像下图那个样子

2.In the package.json file, There is a script tag, add below line of code

"scripts": {
    "copy": "copyfiles source destination"
}

3.最后,我们可以用npm run copy命令进行拷贝文件操作啦~~

3.Finally, You can script by calling npm run copy command in terminal

4.npm copyfiles examples

4.自定义拷贝案例

  • 来看一些案例吧~discuss some examples
  • 拷贝文件到某个目录Copy specific files into directory
  • 假如你的js文件放在src/assets,你怎么只拷贝里面的JS文件到另一个目录呢?
  • Suppose you have a javascript file in src/assets folder. How do you copy only javascript files to the output directory
"copyjavascript": "copyfiles src/assets/*.js destination"
  • 尝试一下下面的命令,感受一下吧~~
npx copyfiles dist test
npx copyfiles dist/spa/assets/*.js out
npx copyfiles dist/spa/* out
npx copyfiles dist/spa/* test
npx copyfiles dist/spa test  
npx copyfiles dist/spa test -a
npx copyfiles -a dist/spa test
npx copyfiles -a dist/spa/assets/* test

image.png

接下来说一下我们使用node命令执行脚本命令的方式

  • 创建scripts\copy.js
  • 编写代码
const copyfiles = require('copyfiles');

const copyEnd = () => {
  console.log('我拷贝完啦~~~');
};
// 这个命令是针对根目录的哦
copyfiles(['dist/spa/assets/*.js', 'out'], { all: true }, copyEnd);


  • 运行代码
node scripts/copy.js

  • 今天就写到这里啦~小伙伴们,( ̄ω ̄( ̄ω ̄〃 ( ̄ω ̄〃)ゝ我们明天再见啦~~
  • 大家要天天()哦

欢迎大家指出文章需要改正之处~
学无止境,合作共赢

在这里插入图片描述

欢迎路过的小哥哥小姐姐们提出更好的意见哇~~

posted @ 2022-07-12 00:34  糖~豆豆  阅读(372)  评论(0编辑  收藏  举报
Live2D