UNPKG

@amplience/dc-cli

Version:
92 lines (91 loc) 3.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SettingsCloneStep = void 0; const clone_hub_step_1 = require("../model/clone-hub-step"); const path_1 = require("path"); const fs_1 = require("fs"); const export_1 = require("../../settings/export"); const import_1 = require("../../settings/import"); const directory_utils_1 = require("../../../common/import/directory-utils"); class SettingsCloneStep { getId() { return clone_hub_step_1.CloneHubStepId.Settings; } getName() { return 'Clone Settings'; } findItem(path, hubId) { const items = (0, fs_1.readdirSync)((0, path_1.join)(path, 'settings')); return items.find(item => { return /^hub\-.*\.json$/.test(item) && item.indexOf(hubId) != -1; }); } async run(state) { try { await (0, directory_utils_1.ensureDirectoryExists)((0, path_1.join)(state.path, 'settings')); await (0, export_1.handler)({ dir: (0, path_1.join)(state.path, 'settings'), logFile: state.logFile, force: true, ...state.from }); } catch (e) { state.logFile.appendLine(`ERROR: Could not export settings. \n${e}`); return false; } try { try { state.logFile.appendLine('Backing up destination settings.'); await (0, export_1.handler)({ dir: (0, path_1.join)(state.path, 'settings'), logFile: state.logFile, force: true, ...state.to }); } catch (e) { state.logFile.appendLine('Failed to back up destination settings. Continuing.'); } const matchingFile = this.findItem(state.path, state.from.hubId); if (matchingFile == null) { state.logFile.appendLine('Error: Could not find exported settings file.'); return false; } await (0, import_1.handler)({ filePath: (0, path_1.join)(state.path, 'settings', matchingFile), mapFile: state.argv.mapFile, force: state.argv.force, logFile: state.logFile, ...state.to }); } catch (e) { state.logFile.appendLine(`ERROR: Could not import settings. \n${e}`); return false; } return true; } async revert(state) { try { const matchingFile = this.findItem(state.path, state.to.hubId); if (matchingFile == null) { state.logFile.appendLine('Error: Could not find exported settings file.'); return false; } await (0, import_1.handler)({ filePath: (0, path_1.join)(state.path, 'settings', matchingFile), mapFile: state.argv.mapFile, force: state.argv.force, logFile: state.logFile, ...state.to }); } catch (e) { state.logFile.appendLine(`ERROR: Could not import old settings. \n${e}`); return false; } return true; } } exports.SettingsCloneStep = SettingsCloneStep;