all files / configs/component_export/ dependencies.js

55% Statements 11/20
100% Branches 0/0
40% Functions 2/5
57.89% Lines 11/19
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                                                                   
 
const fs = require('fs');
const path = require('path');
 
class Dependencies {
  constructor() {
    this.dependencies = {
      "babel-preset-flow": "",
      "chameleon-css-loader": "",
      "chameleon-mixins": "",
      "interface-loader": ""
    };
    this.initDependency();
  }
 
  initDependency() {
    let modules = Object.keys(this.dependencies);
    let packages = fs.readFileSync(`${path.resolve(cml.root, 'package.json')}`);
    packages = JSON.parse(packages);
    let dependencies = packages.dependencies
    modules.forEach(item => {
      this.dependencies[item] = dependencies[item];
    })
  }
 
  addDependency(name, version) {
    this.dependencies[name] = version;
  }
 
  addDependencyByPath(path) {
    let pathArr = path.split('node_modules');
    let npmName = pathArr[1].split('/').filter(item => item !== '');
    npmName = npmName[0];
    let json = fs.readFileSync(`${pathArr[0]}node_modules/${npmName}/package.json`);
    json = JSON.parse(json);
    this.dependencies[npmName] = json.version;
  }
 
  getDependencies() {
    return JSON.stringify(this.dependencies, null, 2);
  }
}
 
module.exports = new Dependencies();