UNPKG

@amplience/dc-cli

Version:
89 lines (88 loc) 3.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ContentMapping = void 0; const fs_1 = require("fs"); const path_1 = require("path"); const util_1 = require("util"); class ContentMapping { constructor() { this.contentItems = new Map(); this.workflowStates = new Map(); this.events = new Map(); this.editions = new Map(); this.slots = new Map(); this.snapshots = new Map(); } getContentItem(id) { return id === undefined ? undefined : this.contentItems.get(id); } registerContentItem(fromId, toId) { this.contentItems.set(fromId, toId); } getWorkflowState(id) { if (id === undefined) { return undefined; } return this.workflowStates.get(id); } registerWorkflowState(fromId, toId) { this.workflowStates.set(fromId, toId); } getEvent(id) { return id === undefined ? undefined : this.events.get(id); } registerEvent(fromId, toId) { this.events.set(fromId, toId); } getEdition(id) { return id === undefined ? undefined : this.editions.get(id); } registerEdition(fromId, toId) { this.editions.set(fromId, toId); } getSlot(id) { return id === undefined ? undefined : this.slots.get(id); } registerSlot(fromId, toId) { this.slots.set(fromId, toId); } getSnapshot(id) { return id === undefined ? undefined : this.snapshots.get(id); } registerSnapshot(fromId, toId) { this.snapshots.set(fromId, toId); } async save(filename) { const obj = { contentItems: Array.from(this.contentItems), workflowStates: Array.from(this.workflowStates), events: Array.from(this.events), editions: Array.from(this.editions), slots: Array.from(this.slots), snapshots: Array.from(this.snapshots) }; const text = JSON.stringify(obj); const dir = (0, path_1.dirname)(filename); if (!(await (0, util_1.promisify)(fs_1.exists)(dir))) { await (0, util_1.promisify)(fs_1.mkdir)(dir); } await (0, util_1.promisify)(fs_1.writeFile)(filename, text, { encoding: 'utf8' }); } async load(filename) { try { const text = await (0, util_1.promisify)(fs_1.readFile)(filename, { encoding: 'utf8' }); const obj = JSON.parse(text); this.contentItems = new Map(obj.contentItems); this.workflowStates = new Map(obj.workflowStates); this.events = obj.events ? new Map(obj.events) : new Map(); this.editions = obj.editions ? new Map(obj.editions) : new Map(); this.slots = obj.slots ? new Map(obj.slots) : new Map(); this.snapshots = obj.snapshots ? new Map(obj.snapshots) : new Map(); return true; } catch (e) { return false; } } } exports.ContentMapping = ContentMapping;