all files / configs/plugins/ CopyNpmPLugin.js

20% Statements 6/30
0% Branches 0/18
0% Functions 0/4
20% Lines 6/30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57                                                                                                      
const fs = require('fs');
const fse = require('fs-extra');
const path = require('path');
const glob = require('glob');
class CopyNpmPLugin {
  constructor(options) {
    this.cmlType = options.cmlType;
    this.root = options.root;
  }
  apply(compiler) {
    let self = this;
 
    if (compiler.hooks) {
      compiler.hooks.afterEmit.tap('CopyNpmPLugin', copyNpm);
    } else {
      compiler.plugin('after-emit', copyNpm);
    }
 
    function copyNpm(compilation, callback) {
      let copyNpm = cml.config.get().copyNpm && cml.config.get().copyNpm[self.cmlType];
      if (copyNpm && copyNpm.length > 0) {
        copyNpm.forEach(function(npmName) {
 
          let packageRoot = path.join(cml.projectRoot, 'node_modules', npmName);
          let packageJson = JSON.parse(fs.readFileSync(path.join(packageRoot, 'package.json'), {encoding: 'utf-8'}));
          let cmlConfig = packageJson.cml && packageJson.cml[self.cmlType]; 
 
          let copyArray = [];
          if (cmlConfig.pages && cmlConfig.pages.length > 0) {
            copyArray = copyArray.concat(cmlConfig.pages)
          }
          if (cmlConfig.components && cmlConfig.components.length > 0) {
            copyArray = copyArray.concat(cmlConfig.components)
          }
          copyArray.forEach(copyItem => {
            let globPath = path.join(packageRoot, `${copyItem}.*`);
            let copyFiles = glob.sync(globPath);
            copyFiles.forEach(file => {
              let dest = path.join(self.root, copyItem + path.extname(file));
              fse.copySync(file, dest)
            })
          })
 
        })
      }
 
      return callback()
    }
 
 
  }
}
 
module.exports = CopyNpmPLugin;