@lark-project/cli
Version:
飞书项目插件开发工具
97 lines (96 loc) • 3.8 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_extra_1 = __importDefault(require("fs-extra"));
const utils_1 = require("../../utils");
const logger_1 = require("../../../utils/logger");
const DEFAULT_OPTION = { spaces: 2, EOL: '\r\n' };
class ConfigBase {
constructor() {
this.run = async (action, key, value) => {
const method = this[action];
if (!method) {
(0, utils_1.error)(`error config action: ${action}`);
}
return method(key, value);
};
this.get = (key, needPrint = true) => {
const config = fs_extra_1.default.readJsonSync(this.path, { throws: false });
const value = (0, utils_1.get)(config, key);
if (!value) {
needPrint && console.log('Configuration does not exist');
return null;
}
else {
try {
const originValue = ['secret', 'pluginSecret'].includes(key)
? (0, utils_1.decrypt)(value)
: value;
needPrint && console.log(originValue);
return originValue;
}
catch (_a) {
throw new Error(`${key} must be encrypted`);
}
}
};
this.list = async (needPrint = true) => {
if (!needPrint) {
return;
}
const config = await fs_extra_1.default.readJson(this.path, { throws: false });
if (!config || Object.keys(config).length === 0) {
console.log('Not found configuration information.');
return;
}
for (const key in config) {
console.log(`\r\n${key}: ${config[key]}`);
}
};
this.set = async (key, value) => {
if (!value || !this.isValid(key, value)) {
(0, utils_1.error)('Invalid configuration key or value');
}
let storedValue = value;
/** 密钥需加密 */
if (['secret', 'pluginSecret'].includes(key) && typeof value === 'string') {
storedValue = (0, utils_1.encrypt)(value);
}
/** 写入配置 */
await this.writeConfig(this.path, key, storedValue);
logger_1.logger.success(`Set successfully, ${key} set to ${value}`);
};
this.remove = async (key) => {
var _a;
const config = (_a = (await fs_extra_1.default.readJson(this.path, { throws: false }))) !== null && _a !== void 0 ? _a : {};
delete config[key];
await fs_extra_1.default.writeJson(this.path, config, DEFAULT_OPTION);
};
}
get path() {
return '';
}
async writeConfig(path, key, value) {
var _a;
const preConfig = fs_extra_1.default.existsSync(path) ? (_a = (await fs_extra_1.default.readJson(path, { throws: false }))) !== null && _a !== void 0 ? _a : {} : {};
(0, utils_1.set)(preConfig, key, value);
await fs_extra_1.default.outputJson(path, preConfig, DEFAULT_OPTION);
}
isValid(key, value) {
switch (key) {
case 'version':
return (0, utils_1.validatePluginVersion)(value);
case 'pluginID':
return (0, utils_1.validatePluginId)(value);
case 'secret':
case 'domain':
case 'pluginSecret':
return true;
default:
return false;
}
}
}
exports.default = ConfigBase;