copy-deep-plugin

1.0.1 • Public • Published

copy-deep-plugin

This plugin is used for deep copying of folders or files, and some configurations can be made to control part of the copying process.

Getting Started

First, you'll need to install copy-deep-plugin:

$npm install copy-deep-plugin --save-dev

Then,you'll need to add some configuration,. For example:

const copyDeepPlugin = require('copy-deep-plugin');

module.exports = {
  plugins: [
      new copyDeepPlugin({
          resources: {
              from: path.resolve(__dirname, 'source'),
              to: path.resolve(__dirname, 'target'),
          }
      }),
  ],
};

Note: In all configurations, resources must be configured, otherwise it cannot be copied!

Configuration instructions

First,you'll need an object as a configuration for the plugin. All configuration details are set in this object.

For Example:

module.exports = {
  plugins: [
      new copyDeepPlugin({
          // configuration details
      }),
  ],
};

Then,You can refer to the following table for configuration.

key type default required Description
resources {Array | Object} undefined yes from is the source file, to is the destination folder, the file or folder name remains the same before and after copying。
For single file or folder copy, please use Object type data. For multiple file or folder copy, please use Array type data. The format of the Array element is the same as a single copy.
targetCheck {Obejct} {create:true, clear: false} no create:If the file or folder does not exist, it will be created.
clear:If the target file or folder exists, it will be created after being emptied.
copyOutPutRealTime {Boolean} false no Export file or folder copy details: output one detail for each copy.
copyOutPutCompleted {Boolean} true no After all resources are copied, the resource copy result list is output.
hooks {Object} undefined no here are 6 hook functions provided here, namely ready, beforeStart, completed, beforeStartItem, and completedItem. These hook functions have their own parameters and required return values.
isVuePress {Boolean} false no If you want to use this plugin in VuePress framework, please set this property to true, otherwise, ignore it.

For example (contains all configurations except hooks):

module.exports = {
  plugins: [
      new copyDeepPlugin({
          targetCheck: {
              create: true,
              clear: true,
          },
          copyOutPutRealTime: true, 
          copyOutPutCompleted: true, 
          resources:[{
              from: path.resolve(__dirname, 'A'),
              to: path.resolve(__dirname, 'target/'),
          },{
              from: path.resolve(__dirname, 'A/B'),
              to: path.resolve(__dirname, 'target/B'),
          }],
      }),
  ],
};

Hooks

The plugin provides 6 hook functions for you, and you can use it according to the actual situation.

Every hook function will receive first

Note:

1. Each hook function receives a ctx parameter as the first parameter of the function. This ctx represents the entire configuration item you passed in. You can change the original ctx during execution by returning a new ctx.

**2.What parameters each hook function passes in, what parameters should you return to achieve the effect of changing the parameters. Of course, if there is no change, you can not return. **

ready Hook

This function is executed before the configuration item is checked. At this time, you can assume that nothing has happened.

For Example:

module.exports = {
  plugins: [
      new copyDeepPlugin({
          resources:{
              from: "source",
              to: "targer",
          },
          hooks: {
              ready(ctx){
                  // do anything
                  // you can return a new ctx to replace old ctx.Of course, if you return undefined, it will not do anything.
                  ctx.newKeyValue = 'change';
                  return ctx;
              },
          }
      }),
  ],
};

beforeStart

After executing this function, the copy operation will start immediately. The configuration item verification has been completed at this time.

For Example:

module.exports = {
  plugins: [
      new copyDeepPlugin({
          resources:{
              from: "source",
              to: "targer",
          },
          hooks: {
              beforeStart(ctx){
                  // do anything
                  // you can return a new ctx to replace old ctx.Of course, if you return undefined, it will not do anything.
              },
          }
      }),
  ],
};

completed

This function is executed at the end of all copy operations, at which time you will get the entire configuration and a list of copy results via parameters.

For Example:

module.exports = {
  plugins: [
      new copyDeepPlugin({
          resources:{
              from: "source",
              to: "targer",
          },
          hooks: {
              completed(ctx, copyList){
                  // do anything
                  // You can return a new object with ctx and copyList properties to replace the old value. Of course, you can also return an object with one property or even no properties.
                  ctx.newKeyValue = 'change';
                  copyList.map(item => item.size);
                  return {ctx, copyList};
              },
          }
      }),
  ],
};

beforeStartItem & completedItem

beforeStartItem function is executed before each copy. completedItem function is executed every time the copy is completed. You can get the entire configuration and the original path of the current copy by parameters.

For Example:

module.exports = {
  plugins: [
      new copyDeepPlugin({
          resources:{
              from: "source",
              to: "targer",
          },
          hooks: {
              beforeStartItem (ctx, current){
                  // do anything
                  // You can return a new object with ctx and current properties to replace the old value. Of course, you can also return an object with one property or even no properties.
                  current.from = '';
                  return {current};
              },
              completedItem  (ctx, current){
                  // do anything
                  // You can return a new object with ctx and current properties to replace the old value. Of course, you can also return an object with one property or even no properties.
                  current.from = '';
                  return {ctx, current};
              },
          }
      }),
  ],
};

Precautions

We strongly recommend that you use absolute paths when configuring the resources property.

If you use this plugin in Vuepress, you can use in hook function which you select:

    let copyDeep = require('copyDeepPlugin');
    let path = require('path');
    module.exports = {
        async generated (pagePaths) {
            new copyDeep({
                resources:[{
                    from: path.resolve(__dirname, '../../a.js'),// 必须要写绝对路径
                    to: path.resolve(__dirname, './dist'),// 必须要写绝对路径
                }],
                isVuePress: true
            })
        }
    }

At last

Thank you for using this plugin, and hope to bring convenience to your learning and production. Finally, if you have any comments or suggestions about this plugin, please feel free to contact me.

WeChat: 809742006

Package Sidebar

Install

npm i copy-deep-plugin

Weekly Downloads

6

Version

1.0.1

License

ISC

Unpacked Size

21.1 kB

Total Files

6

Last publish

Collaborators

  • yuhua