ms/packages/plugin/src/config.ts
MiaoWoo c2a71b9a7a feat: add config support
Signed-off-by: MiaoWoo <admin@yumc.pw>
2020-02-24 16:24:36 +08:00

41 lines
1.0 KiB
TypeScript

import * as yaml from 'js-yaml'
export interface PluginConfigLoader {
load(content: string): any;
dump(variable: any): string;
}
export class YamlPluginConfig implements PluginConfigLoader {
load(content: string) {
return yaml.safeLoad(content);
}
dump(variable: any): string {
return yaml.safeDump(variable);
}
}
export class JsonPluginConfig implements PluginConfigLoader {
load(content: string) {
return JSON.parse(content);
}
dump(variable: any): string {
return JSON.stringify(variable);
}
}
const configLoaderMap = new Map<string, PluginConfigLoader>();
export function getConfigLoader(format: string) {
if (!configLoaderMap.has(format)) { throw new Error(`Unsupport config format ${format} !`) }
return configLoaderMap.get(format);
}
function init() {
configLoaderMap.set("json", new JsonPluginConfig())
let yaml = new YamlPluginConfig()
configLoaderMap.set("yml", yaml)
configLoaderMap.set("yaml", yaml)
}
init()