@wocker/core
Version:
Core of the Wocker
185 lines (184 loc) • 5.38 kB
JavaScript
"use strict";
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AppConfig = void 0;
const types_1 = require("../types");
class AppConfig {
constructor(data) {
this.logLevel = "off";
const { plugins = [], presets = [], projects = [] } = data, rest = __rest(data, ["plugins", "presets", "projects"]);
Object.assign(this, rest);
this.plugins = plugins.map((plugin) => {
if (typeof plugin === "string") {
return {
name: plugin,
env: "latest"
};
}
return plugin;
});
this.presets = presets;
this.projects = projects.map((ref) => {
const { id, name, path, src } = ref;
return {
name: (name || id),
path: (path || src)
};
});
}
addPlugin(name, env = "latest") {
const plugin = this.plugins.find((plugin) => plugin.name === name);
if (plugin) {
plugin.name = name;
plugin.env = env;
return;
}
this.plugins.push({
name,
env
});
}
removePlugin(name) {
if (this.plugins.length === 0) {
return;
}
this.plugins = this.plugins.filter((plugin) => plugin.name !== name);
}
getProject(name) {
if (!this.projects) {
return;
}
return this.projects.find((projectData) => {
return projectData.name === name;
});
}
addProject(name, path) {
let projectRef = this.getProject(name);
if (!projectRef) {
this.projects.push({
name,
path
});
return;
}
projectRef.name = name;
projectRef.path = path;
}
removeProject(name) {
if (this.projects.length === 0) {
return;
}
this.projects = this.projects.filter((projectData) => {
return projectData.name !== name;
});
}
registerPreset(name, source, path) {
if (source === types_1.PRESET_SOURCE_INTERNAL) {
return;
}
let presetData = this.presets.find((preset) => {
if (source === types_1.PRESET_SOURCE_EXTERNAL && preset.path === path) {
return true;
}
return preset.name === name;
});
if (!presetData) {
presetData = {
name,
source
};
this.presets.push(presetData);
}
presetData.source = source;
if (presetData.source === types_1.PRESET_SOURCE_EXTERNAL) {
presetData.path = path;
}
else if (presetData.path) {
delete presetData.path;
}
}
unregisterPreset(name) {
if (this.presets.length === 0) {
return;
}
this.presets = this.presets.filter((preset) => {
return preset.name !== name;
});
}
hasMeta(name) {
if (!this.meta) {
return false;
}
return name in this.meta;
}
getMeta(name, defaultValue) {
if (!this.meta || !(name in this.meta)) {
return defaultValue;
}
return this.meta[name];
}
setMeta(name, value) {
if (!this.meta) {
this.meta = {};
}
this.meta[name] = value;
}
unsetMeta(name) {
if (!this.meta || !(name in this.meta)) {
return;
}
delete this.meta[name];
if (Object.keys(this.meta).length === 0) {
delete this.meta;
}
}
getEnv(name, defaultValue) {
if (!this.env || !(name in this.env)) {
return defaultValue;
}
return this.env[name];
}
setEnv(name, value) {
if (!this.env) {
this.env = {};
}
this.env[name] = value;
}
unsetEnv(name) {
if (!this.env || !(name in this.env)) {
return;
}
delete this.env[name];
if (Object.keys(this.env).length === 0) {
delete this.env;
}
}
toObject() {
return {
debug: this.debug,
pm: this.pm,
logLevel: this.logLevel,
keystore: this.keystore,
plugins: this.plugins.length > 0 ? this.plugins : undefined,
presets: this.presets.length > 0 ? this.presets : undefined,
projects: this.projects.length > 0 ? this.projects : undefined,
env: this.env,
meta: this.meta
};
}
toJsString() {
const json = JSON.stringify(this.toObject(), null, 4);
return `// Wocker config\nexports.config = ${json};`;
}
}
exports.AppConfig = AppConfig;