wip-backup-manager
Version:
This utility enables backup & restore of Kentico Kontent projects
281 lines • 12.7 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var fs = require("fs");
var yargs = require("yargs");
var clean_1 = require("../clean");
var core_1 = require("../core");
var export_1 = require("../export");
var import_1 = require("../import");
var zip_1 = require("../zip");
var kontent_management_1 = require("@kentico/kontent-management");
var argv = yargs.argv;
var backupAsync = function (config) { return __awaiter(void 0, void 0, void 0, function () {
var exportService, zipService, report, response, logFilename;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
exportService = new export_1.ExportService({
apiKey: config.apiKey,
projectId: config.projectId,
onExport: function (item) {
if (config.enableLog) {
console.log("Exported: " + item.title + " | " + item.type);
}
}
});
zipService = new zip_1.ZipService({
filename: config.zipFilename,
enableLog: config.enableLog
});
return [4 /*yield*/, exportService.exportProjectValidationAsync()];
case 1:
report = _a.sent();
return [4 /*yield*/, exportService.exportAllAsync()];
case 2:
response = _a.sent();
return [4 /*yield*/, zipService.createZipAsync(response)];
case 3:
_a.sent();
if (!exportContainsInconsistencies(report)) return [3 /*break*/, 5];
logFilename = getLogFilename(config.zipFilename);
return [4 /*yield*/, core_1.fileHelper.createFileInCurrentFolderAsync(logFilename, JSON.stringify(report))];
case 4:
_a.sent();
console.log("Project contains inconsistencies which may cause future import to not work.");
console.log("See '" + logFilename + "' for more details.");
_a.label = 5;
case 5:
console.log('Completed');
return [2 /*return*/];
}
});
}); };
var getLogFilename = function (filename) {
return core_1.getFilenameWithoutExtension(filename) + "_log.json";
};
var cleanAsync = function (config) { return __awaiter(void 0, void 0, void 0, function () {
var cleanService;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
cleanService = new clean_1.CleanService({
onDelete: function (item) {
if (config.enableLog) {
console.log("Deleted: " + item.title + " | " + item.type);
}
},
projectId: config.projectId,
apiKey: config.apiKey
});
return [4 /*yield*/, cleanService.cleanAllAsync()];
case 1:
_a.sent();
console.log('Completed');
return [2 /*return*/];
}
});
}); };
var restoreAsync = function (config) { return __awaiter(void 0, void 0, void 0, function () {
var zipService, importService, data, logFilename;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
zipService = new zip_1.ZipService({
filename: config.zipFilename,
enableLog: config.enableLog
});
importService = new import_1.ImportService({
onImport: function (item) {
if (config.enableLog) {
console.log("Imported: " + item.title + " | " + item.type);
}
},
fixLanguages: true,
projectId: config.projectId,
apiKey: config.apiKey,
enableLog: config.enableLog,
workflowIdForImportedItems: '00000000-0000-0000-0000-000000000000',
process: {
contentItem: function (item) {
return true;
}
}
});
return [4 /*yield*/, zipService.extractZipAsync()];
case 1:
data = _a.sent();
if (!canImport(data, config)) return [3 /*break*/, 3];
return [4 /*yield*/, importService.importFromSourceAsync(data)];
case 2:
_a.sent();
console.log('Completed');
return [3 /*break*/, 5];
case 3:
logFilename = getLogFilename(config.zipFilename);
return [4 /*yield*/, core_1.fileHelper.createFileInCurrentFolderAsync(logFilename, JSON.stringify(data.validation))];
case 4:
_a.sent();
console.log("Project could not be imported due to data inconsistencies.");
console.log("A log file '" + logFilename + "' with issues was created in current folder.");
console.log("To import data regardless of issues, set 'force' config parameter to true");
_a.label = 5;
case 5: return [2 /*return*/];
}
});
}); };
var validateConfig = function (config) {
if (!config) {
throw Error("Invalid config file");
}
var projectId = config.projectId;
var apiKey = config.apiKey;
var action = config.action;
if (!projectId) {
throw Error('Invalid project id');
}
if (!apiKey) {
throw Error('Invalid api key');
}
if (!action) {
throw Error('Invalid action');
}
};
var process = function () { return __awaiter(void 0, void 0, void 0, function () {
var config;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, getConfig()];
case 1:
config = _a.sent();
validateConfig(config);
if (!(config.action === 'backup')) return [3 /*break*/, 3];
return [4 /*yield*/, backupAsync(config)];
case 2:
_a.sent();
return [3 /*break*/, 8];
case 3:
if (!(config.action === 'clean')) return [3 /*break*/, 5];
return [4 /*yield*/, cleanAsync(config)];
case 4:
_a.sent();
return [3 /*break*/, 8];
case 5:
if (!(config.action === 'restore')) return [3 /*break*/, 7];
return [4 /*yield*/, restoreAsync(config)];
case 6:
_a.sent();
return [3 /*break*/, 8];
case 7: throw Error("Invalid action");
case 8: return [2 /*return*/];
}
});
}); };
var exportContainsInconsistencies = function (projectReport) {
if (projectReport.variant_issues.length > 0 || projectReport.type_issues.length > 0) {
return true;
}
return false;
};
var canImport = function (importData, config) {
if (!importData.metadata.isInconsistentExport) {
return true;
}
if (config.force === true) {
return true;
}
return false;
};
var getConfig = function () { return __awaiter(void 0, void 0, void 0, function () {
var configFilename, configFile, action, apiKey, enableLog, force, projectId, zipFilename, config;
var _a, _b, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
configFilename = argv.config;
if (!configFilename) return [3 /*break*/, 2];
return [4 /*yield*/, fs.promises.readFile("./" + configFilename)];
case 1:
configFile = _d.sent();
return [2 /*return*/, JSON.parse(configFile.toString())];
case 2:
action = argv.action;
apiKey = argv.apiKey;
enableLog = (_a = argv.enableLog) !== null && _a !== void 0 ? _a : true;
force = (_b = argv.force) !== null && _b !== void 0 ? _b : true;
projectId = argv.projectId;
zipFilename = (_c = argv.zipFilename) !== null && _c !== void 0 ? _c : getDefaultBackupFilename();
if (!action) {
throw Error("No action was provided");
}
if (!apiKey) {
throw Error("Api key was not provided");
}
if (!projectId) {
throw Error("Project id was not provided");
}
config = {
action: action,
apiKey: apiKey,
enableLog: enableLog,
force: force,
projectId: projectId,
zipFilename: zipFilename
};
return [2 /*return*/, config];
}
});
}); };
var getDefaultBackupFilename = function () {
var date = new Date();
return "kontent-backup-" + date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear() + "-" + date.getHours() + "-" + date.getMinutes();
};
process().then(function (m) { }).catch(function (err) {
if (err instanceof kontent_management_1.SharedModels.ContentManagementBaseKontentError) {
console.log("Management API error occured:", err.message);
for (var _i = 0, _a = err.validationErrors; _i < _a.length; _i++) {
var validationError = _a[_i];
console.log(validationError.message);
}
}
else {
console.log("There was an error processing your request: ", err);
}
});
//# sourceMappingURL=app.js.map