UNPKG

jmcreater

Version:

udc前端开发构建工具

64 lines (54 loc) 1.64 kB
'use strict' var fs = require('fs'); var path = require('path'); var Util = require('../util'); var request = require('request'); var chalk = require('chalk'); class Base { constructor() { } /** * 读取模板缓存 */ readCache(path) { var _cache = { 'templates': [] }; if (!fs.existsSync(path)) { this.writeCache(_cache, path); } _cache = fs.readFileSync(path, 'utf8'); _cache = JSON.parse(_cache); return _cache; } /** * 写入模板缓存 */ writeCache(content, _path) { fs.writeFileSync(_path, JSON.stringify(content), 'utf8'); } //tmp缓存存放路径 sourceRoot(rootPath) { if (typeof rootPath === 'string') { this._sourceRoot = path.resolve(rootPath); } if (!fs.existsSync(this._sourceRoot)) { this.mkdir(this._sourceRoot); } return this._sourceRoot; } getTemplatesInfo(cb) { var setting = Util.getDefaultConfig(); console.log(chalk.green('加载服务器模板信息中')); request.get(setting["tpl-server"] + "/template.json", function (err, res, data) { //获取模板信息 if (!err && res.statusCode === 200) { var templatesInfo = JSON.parse(data); cb(templatesInfo); } else { console.log('警告:未能从服务器同步模板信息,错误码为' + res.statusCode); } }); } } module.exports = Base;