UNPKG

@wocker/ws

Version:

Docker workspace for web projects

91 lines (90 loc) 3.54 kB
"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); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProjectRepository = void 0; const core_1 = require("@wocker/core"); const keystore_1 = require("../../keystore"); let ProjectRepository = class ProjectRepository { constructor(appConfigService, fs, keystoreService) { this.appConfigService = appConfigService; this.fs = fs; this.keystoreService = keystoreService; } getByName(name) { const ref = this.appConfigService.config.getProject(name), config = this.fs.readJSON(`projects/${name}/config.json`); return this.fromObject({ ...config, path: ref.path }); } fromObject(data) { const _this = this; return new class extends core_1.Project { constructor(data) { super(data); } async getSecret(key, defaultValue) { return _this.keystoreService.get(`p:${this.name}:${key}`, defaultValue); } async setSecret(key, value) { return _this.keystoreService.set(`p:${this.name}:${key}`, value); } save() { _this.save(this); } }(data); } save(project) { if (!project.name) { throw new Error("Project should has a name"); } if (!project.path) { throw new Error("Project should has a path"); } if (!project.id) { project.id = project.name; } if (!this.fs.exists(`projects/${project.id}`)) { this.fs.mkdir(`projects/${project.id}`, { recursive: true }); } const { path, ...rest } = project.toObject(); this.appConfigService.addProject(project.id, project.name, path); this.fs.writeJSON(`projects/${project.id}/config.json`, rest); this.appConfigService.save(); } search(params = {}) { const { name, path } = params, projects = []; for (const ref of this.appConfigService.projects) { if (name && ref.name !== name) { continue; } if (path && ref.path !== path) { continue; } const project = this.getByName(ref.name); projects.push(project); } return projects; } searchOne(params = {}) { const [project] = this.search(params); return project || null; } }; exports.ProjectRepository = ProjectRepository; exports.ProjectRepository = ProjectRepository = __decorate([ (0, core_1.Injectable)(), __metadata("design:paramtypes", [core_1.AppConfigService, core_1.AppFileSystemService, keystore_1.KeystoreService]) ], ProjectRepository);