all files / lib/ utils.js

35.71% Statements 10/28
0% Branches 0/8
0% Functions 0/5
35.71% Lines 10/28
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 path = require('path');
 
const fse = require('fs-extra');
const watch = require('node-watch');
 
const utils = require('chameleon-tool-utils');
// 创建路由文件
// platform   web端和weex端 现在没有用到,如果要区分,cml weex dev命令要执行两遍, web和weex的要生成两个不同的routerOptions文件
// media dev模式开启watch  其他情况不监听变化 否则命名行不结束
utils.createRouterFile = function (platform, media) {
 
  let routerConfigPath = path.join(cml.projectRoot, 'src/router.config.json');
 
  if (utils.isFile(routerConfigPath)) {
    if (media === 'dev') {
      watch(routerConfigPath, { recursive: true }, function(evt, name) {
        cml.log.debug(' createRouterFile routerchange')
        cml.event.emit('routerchange')
      });
    }
  } else {
    cml.log.error('未找到路由配置文件');
  }
 
}
 
var TEMP_ROOT;
 
utils.getTempRoot = function () {
  if (!TEMP_ROOT) {
    var tmp = path.join(cml.projectRoot, 'node_modules/.chameleon')
    if (cml.config.get().serverPath) {
      tmp = cml.config.get().serverPath
    }
    utils.setTempRoot(tmp);
  }
  return TEMP_ROOT;
};
 
utils.getDevServerPath = function () {
  return path.resolve(utils.getTempRoot() + '/www');
}
 
 
utils.setTempRoot = function (tmp) {
  try {
    TEMP_ROOT = tmp;
    fse.ensureDirSync(tmp);
  } catch (e) {
    console.log(e);
  }
};
 
// 生成config.json文件
module.exports = utils;