@wocker/core
Version:
Core of the Wocker
201 lines (200 loc) • 6.74 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AppConfigService = void 0;
const path_1 = __importDefault(require("path"));
const decorators_1 = require("../decorators");
const AppConfig_1 = require("../makes/AppConfig");
const Project_1 = require("../makes/Project");
const AppFileSystemService_1 = require("./AppFileSystemService");
const ProcessService_1 = require("./ProcessService");
const env_1 = require("../env");
let AppConfigService = class AppConfigService {
constructor(version, processService, fs) {
this.version = version;
this.processService = processService;
this.fs = fs;
this.mapTypes = {
[Project_1.PROJECT_TYPE_PRESET]: "Preset",
[Project_1.PROJECT_TYPE_IMAGE]: "Image",
[Project_1.PROJECT_TYPE_DOCKERFILE]: "Dockerfile"
};
this._pwd = (process.cwd() || process.env.PWD);
}
get experimentalFeatures() {
return [
"projectComposeType",
"buildKit"
];
}
get debug() {
return this.config.debug || false;
}
set debug(debug) {
this.config.debug = debug;
}
get config() {
if (!this._config) {
let data = {};
if (this.fs.exists("wocker.config.js")) {
try {
const { config } = require(this.fs.path("wocker.config.js"));
data = config;
}
catch (err) {
// TODO: Log somehow
// this.logService.error(err);
if (this.fs.exists("wocker.config.json")) {
let json = this.fs.readJSON("wocker.config.json");
if (typeof json === "string") {
json = JSON.parse(json);
}
data = json;
}
}
}
else if (this.fs.exists("wocker.config.json")) {
data = this.fs.readJSON("wocker.config.json");
}
else if (this.fs.exists("wocker.json")) {
let json = this.fs.readJSON("wocker.json");
if (typeof json === "string") {
json = JSON.parse(json);
}
data = json;
}
else if (this.fs.exists("data.json")) {
data = this.fs.readJSON("data.json");
}
else if (!this.fs.exists()) {
this.fs.mkdir("", {
recursive: true
});
}
this._config = new AppConfig_1.AppConfig(data);
}
return this._config;
}
get projects() {
return this.config.projects;
}
get plugins() {
return this.config.plugins;
}
/**
* @deprecated
*/
isVersionGTE(version) {
const current = this.version.split(".").map(Number);
const compare = version.split(".").map(Number);
for (let i = 0; i < 3; i++) {
if (current[i] > compare[i]) {
return true;
}
else if (current[i] < compare[i]) {
return false;
}
}
return true;
}
/**
* @deprecated
*/
pwd(...parts) {
return this.processService.pwd(path_1.default.join(...parts));
}
/**
* @deprecated
*/
setPWD(pwd) {
this.processService.chdir(pwd);
}
/**
* @deprecated
*/
dataPath(...args) {
return this.fs.path(...args);
}
/**
* @deprecated
*/
getConfig() {
return this.config;
}
addProject(id, name, path) {
this.config.addProject(id, name, path);
}
removeProject(id) {
this.config.removeProject(id);
this.save();
}
registerPreset(name, source, path) {
this.config.registerPreset(name, source, path);
this.save();
}
unregisterPreset(name) {
this.config.unregisterPreset(name);
this.save();
}
addPlugin(name, env) {
this.config.addPlugin(name);
}
removePlugin(name) {
this.config.removePlugin(name);
}
getMeta(name, byDefault) {
return this.config.getMeta(name, byDefault);
}
setMeta(name, value) {
this.config.setMeta(name, value);
}
unsetMeta(name) {
this.config.unsetMeta(name);
}
isExperimentalEnabled(key) {
return this.config.getMeta(`experimental.${key}`) === "enabled";
}
getProjectTypes() {
if (this.isExperimentalEnabled("projectComposeType")) {
return Object.assign(Object.assign({}, this.mapTypes), { [Project_1.PROJECT_TYPE_COMPOSE]: "Docker compose" });
}
return this.mapTypes;
}
save() {
const fs = this.fs;
if (!fs.exists()) {
fs.mkdir("", {
recursive: true
});
}
fs.writeFile("wocker.config.js", this.config.toJsString());
fs.writeFile("wocker.config.json", this.config.toString()); // Backup file
if (fs.exists("data.json")) {
fs.rm("data.json");
}
if (fs.exists("wocker.json")) {
fs.rm("wocker.json");
}
}
};
exports.AppConfigService = AppConfigService;
exports.AppConfigService = AppConfigService = __decorate([
(0, decorators_1.Injectable)("APP_CONFIG"),
__param(0, (0, decorators_1.Inject)(env_1.WOCKER_VERSION_KEY)),
__metadata("design:paramtypes", [String, ProcessService_1.ProcessService,
AppFileSystemService_1.AppFileSystemService])
], AppConfigService);