UNPKG

@agility/cli

Version:

Agility CLI for working with your content. (Public Beta)

252 lines 14 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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 = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["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 (g && (g = 0, op[0] && (_ = 0)), _) 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 }; } }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.pushModels = pushModels; var state_1 = require("../../core/state"); var model_mapper_1 = require("lib/mappers/model-mapper"); /** * Simple change detection for models */ function pushModels(sourceData, targetData) { return __awaiter(this, void 0, void 0, function () { var models, sourceGuid, targetGuid, logger, referenceMapper, apiClient, successful, failed, skipped, shouldCreateStub, shouldUpdateFields, shouldSkip, stubCreated, _loop_1, _i, models_1, model, _a, shouldCreateStub_1, model, result, modelsToUpdate, _b, modelsToUpdate_1, model, mapping, result, _c, shouldSkip_1, model; var _d, _e; return __generator(this, function (_f) { switch (_f.label) { case 0: models = sourceData || []; sourceGuid = state_1.state.sourceGuid, targetGuid = state_1.state.targetGuid; logger = (0, state_1.getLoggerForGuid)(sourceGuid[0]); if (!models || models.length === 0) { logger.log("INFO", "No models found to process."); return [2 /*return*/, { status: "success", successful: 0, failed: 0, skipped: 0 }]; } referenceMapper = new model_mapper_1.ModelMapper(sourceGuid[0], targetGuid[0]); apiClient = (0, state_1.getApiClient)(); successful = 0; failed = 0; skipped = 0; shouldCreateStub = []; shouldUpdateFields = []; shouldSkip = []; stubCreated = []; _loop_1 = function (model) { var mapping = referenceMapper.getModelMapping(model, "source"); var targetModel = targetData.find(function (targetModel) { return targetModel.referenceName === model.referenceName; }) || null; var modelLastModifiedDate = new Date(model.lastModifiedDate); var targetLastModifiedDate = targetModel ? new Date(targetModel.lastModifiedDate) : null; var mappingLastModifiedDate = mapping ? new Date(mapping.targetLastModifiedDate) : null; var hasSourceChanged = modelLastModifiedDate > targetLastModifiedDate; var hasTargetChanged = targetLastModifiedDate > mappingLastModifiedDate; var sourceFieldCount = ((_d = model === null || model === void 0 ? void 0 : model.fields) === null || _d === void 0 ? void 0 : _d.length) || 0; var targetFieldCount = ((_e = targetModel === null || targetModel === void 0 ? void 0 : targetModel.fields) === null || _e === void 0 ? void 0 : _e.length) || 0; var fieldCountChanged = sourceFieldCount !== targetFieldCount; // TODO: we only care about the field count if the target model has NO fields and the source model has fields if (!mapping && !targetModel) { shouldCreateStub.push(model); } // if the mapping exists, and the source has changed, we need to update the fields if ((mapping && hasSourceChanged) || (mapping && fieldCountChanged)) { shouldUpdateFields.push(model); } // if the mapping exists, and the target has changed, we need to skip the model, not safe to update if (mapping && hasTargetChanged) { shouldSkip.push(model); } // if the mapping exists, and the source and target have not changed, we need to skip the model if (mapping && !hasSourceChanged && !hasTargetChanged && !state_1.state.overwrite) { shouldSkip.push(model); } if (mapping && !hasSourceChanged && !hasTargetChanged && state_1.state.overwrite) { shouldUpdateFields.push(model); } }; for (_i = 0, models_1 = models; _i < models_1.length; _i++) { model = models_1[_i]; _loop_1(model); } _a = 0, shouldCreateStub_1 = shouldCreateStub; _f.label = 1; case 1: if (!(_a < shouldCreateStub_1.length)) return [3 /*break*/, 4]; model = shouldCreateStub_1[_a]; return [4 /*yield*/, createNewModel(model, referenceMapper, apiClient, targetGuid[0], logger)]; case 2: result = _f.sent(); if (result === "created") { stubCreated.push(model); } else { failed++; } _f.label = 3; case 3: _a++; return [3 /*break*/, 1]; case 4: modelsToUpdate = __spreadArray(__spreadArray([], stubCreated, true), shouldUpdateFields, true); _b = 0, modelsToUpdate_1 = modelsToUpdate; _f.label = 5; case 5: if (!(_b < modelsToUpdate_1.length)) return [3 /*break*/, 8]; model = modelsToUpdate_1[_b]; mapping = referenceMapper.getModelMapping(model, "source"); return [4 /*yield*/, updateExistingModel(model, mapping.targetID, referenceMapper, apiClient, targetGuid[0], logger)]; case 6: result = _f.sent(); if (result) { successful++; } else { failed++; } _f.label = 7; case 7: _b++; return [3 /*break*/, 5]; case 8: for (_c = 0, shouldSkip_1 = shouldSkip; _c < shouldSkip_1.length; _c++) { model = shouldSkip_1[_c]; logger.model.skipped(model, "up to date, skipping", targetGuid[0]); skipped++; } return [2 /*return*/, { status: "success", successful: successful, failed: failed, skipped: skipped, }]; } }); }); } /** * If we're creating a model, we need to create a stub, then update the fields * */ var createNewModel = function (model, referenceMapper, apiClient, targetGuid, logger) { return __awaiter(void 0, void 0, void 0, function () { var createPayload, newModel, error_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); createPayload = __assign(__assign({}, model), { id: 0, fields: [] }); return [4 /*yield*/, apiClient.modelMethods.saveModel(createPayload, targetGuid)]; case 1: newModel = _a.sent(); logger.model.created(model, "created", targetGuid); referenceMapper.addMapping(model, newModel); return [2 /*return*/, "created"]; case 2: error_1 = _a.sent(); logger.model.error(model, error_1, targetGuid); return [2 /*return*/, "failed"]; case 3: return [2 /*return*/]; } }); }); }; /** * Update an existing model in the target instance */ function updateExistingModel(sourceModel, targetID, referenceMapper, apiClient, targetGuid, logger) { return __awaiter(this, void 0, void 0, function () { var fields, updatePayload, updatedModel, error_2; return __generator(this, function (_a) { switch (_a.label) { case 0: fields = (sourceModel === null || sourceModel === void 0 ? void 0 : sourceModel.fields) || []; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); updatePayload = __assign(__assign({}, sourceModel), { id: targetID, fields: (fields || sourceModel.fields || []).map(function (field) { var _a; var cleanField = __assign({}, field); delete cleanField.fieldID; // Remove to prevent API issues // Clean up Content field settings if (cleanField.type === "Content" && ((_a = cleanField.settings) === null || _a === void 0 ? void 0 : _a.ContentDefinition)) { var _b = cleanField.settings, ContentDefinition = _b.ContentDefinition, otherSettings = __rest(_b, ["ContentDefinition"]); cleanField.settings = otherSettings; } return cleanField; }) }); return [4 /*yield*/, apiClient.modelMethods.saveModel(updatePayload, targetGuid)]; case 2: updatedModel = _a.sent(); logger.model.updated(sourceModel, "updated", targetGuid); referenceMapper.addMapping(sourceModel, updatedModel); return [2 /*return*/, "updated"]; case 3: error_2 = _a.sent(); logger.model.error(sourceModel, error_2, targetGuid); return [2 /*return*/, "failed"]; case 4: return [2 /*return*/]; } }); }); } //# sourceMappingURL=model-pusher.js.map