pgy-deploy
Version:
使用ssh2与scp2实现代码发布
126 lines (125 loc) • 4.51 kB
JavaScript
const fs = require('fs');
const common = require("../../common");
const chalk = require('chalk');
const blue = chalk.blue;
const success = chalk.green;
const error = chalk.bold.red;
const warning = chalk.keyword('orange');
class ConfigClass {
constructor() {
// 配置文件目录
this.configPath = common.toProjectPath("/src/");
// 配置文件后缀正则配置
this.reg = RegExp(/\s*.conf$/);
// 配置文件后缀
this.confSuffix = ".conf";
// 运行参数的默认配置文件
this.config = "deploy.json";
// 配置文件对应名称(此配置文件用于 配置 运行参数的配置文件 的名称)
this.confFileName = "myDefault";
return this;
}
/**
* 修改默认配置文件名称
* @param {*} name
*/
setConfigName(name) {
fs.writeFile(this.configPath + this.confFileName + this.confSuffix, name, function (err) {
if (err) {
console.log(error("修改配置失败:"));
console.log(error(err));
return;
}
console.log(success("修改配置成功:" + name));
});
return;
}
/**
* 重置单个配置文件
*/
delConf() {
const path = this.configPath + this.confFileName + this.confSuffix;
if (!fs.existsSync(path)) {
return console.log(warning(this.confFileName + "配置不存在或无需重置"));
}
fs.unlink(path, (err) => {
if (err) {
console.log(error("重置" + this.confFileName + "配置失败"));
console.log(error(err));
return;
}
console.log(success("重置" + this.confFileName + "配置成功"));
return;
});
}
/**
* 重置所有配置
* 只会重置修改过的配置
*/
delAll() {
const files = fs.readdirSync(this.configPath);
files.forEach(function (file, index) {
if (this.reg.test(file)) {
fs.unlink(this.configPath + file, function (err) {
if (err) {
console.log(error("重置配置失败"));
console.log(error(err));
return;
}
console.log(success("重置" + file.substr(0, a.length - 5) + "配置成功"));
});
}
});
console.log("重置结束");
return;
}
/**
* 打印全部配置
*/
printConfig() {
// 打印设置的配置文件名称 start
let defConf = this.config;
if (fs.existsSync(this.configPath + this.confFileName + this.confSuffix)) {
const confCont = fs.readFileSync(this.configPath + this.confFileName + this.confSuffix);
if (confCont && confCont != "") {
defConf = confCont + " (自定义)";
}
}
console.log("配置文件名称");
console.log("deploy配置文件名称:" + blue(defConf));
// 打印设置的配置文件名称 end
return;
}
// 获取默认脚本参数
getParam(param4) {
if (!param4) {
throw "请指定配置: [配置]";
}
let defConf = this.config;
if (fs.existsSync(this.configPath + this.confFileName + this.confSuffix)) {
const confCont = fs.readFileSync(this.configPath + this.confFileName + this.confSuffix);
if (confCont && confCont != "") {
defConf = confCont;
}
}
if (!fs.existsSync(defConf)) throw "默认环境配置文件不存在";
try {
const confData = JSON.parse(fs.readFileSync(defConf, 'utf8'));
if (param4 && param4 in confData) {
return confData[param4];
}
console.error(error("默认配置文件解析错误"));
throw "读取配置失败";
} catch (error) {
console.error(error("默认配置文件解析错误"));
throw error;
}
}
getVersio() {
const path = common.toProjectPath("/package.json");
const data = JSON.parse(fs.readFileSync(path, 'utf8'));
console.log(data.version);
return;
}
}
module.exports = ConfigClass;