UNPKG

jmcreater

Version:

udc前端开发构建工具

104 lines (97 loc) 3.24 kB
'use strict'; var fs = require('fs'); var path = require('path'); var chalk = require('chalk'); var Util = { homedir: function () { var env = process.env; var home = env.HOME; var user = env.LOGNAME || env.USER || env.LNAME || env.USERNAME; if (process.platform === 'win32') { return env.USERPROFILE || env.HOMEDRIVE + env.HOMEPATH || home || null; } if (process.platform === 'darwin') { return home || (user ? '/Users/' + user : null); } if (process.platform === 'linux') { return home || (process.getuid() === 0 ? '/root' : (user ? '/home/' + user : null)); } return home || null; }, getNowDate: function () { var nowDate = new Date(); var now = nowDate.getFullYear() + '-' + (nowDate.getMonth() + 1) + '-' + nowDate.getDate() + ' ' + nowDate.getHours() + ':' + nowDate.getMinutes() + ':' + nowDate.getMinutes(); return now; }, //判断文件是否存在 existsSync(filePath){ try { var stats = fs.statSync(filePath); return (stats.isFile() || stats.isDirectory()); } catch (err) { return false; } }, setConfig: function (config) { var jmPath = this.getJmPath(); if (typeof config === 'object') { fs.writeFileSync(path.join(jmPath, 'config.json'), JSON.stringify(config, null, 2)); } }, getPkgInfo: function () { var info = {}; try { info = JSON.parse(String(fs.readFileSync(path.join(this.getRootPath(), 'package.json')))); } catch (e) { console.log('读取package.json出错'); } return info; }, //读取jm缓存目录 getJmPath: function () { var jmPath = path.join(this.homedir(), '.jm'); if (!this.existsSync(jmPath)) { fs.mkdirSync(jmPath); } return jmPath; }, //缓存的模板存放路径 getTmpPath: function () { var tmpPath = path.join(this.getJmPath(), 'tmp'); if (!fs.existsSync(tmpPath)) { fs.mkdirSync(tmpPath); } return tmpPath }, //读取jm npm包安装路径 getRootPath: function () { return path.join(__dirname, "../../") }, //读取基础配置文件 getDefaultConfig(){ var configPath = path.join(this.getJmPath(), 'config.json'); var config = {}; if (this.existsSync(configPath)) { try { config = JSON.parse(String(fs.readFileSync(configPath))); } catch (e) { config = {}; } return config; } else { return null; } }, //读取用户自定义的配置文件 getUserConfig: function () { var configPath = path.join(process.cwd(), 'config.json'); var config = {}; try { config = JSON.parse(fs.readFileSync(configPath)); } catch (e) { console.log(chalk.red(e)) } return config; } } module.exports = Util;