UNPKG

react-native-integrate

Version:

Automate integration of additional code into React Native projects

86 lines (85 loc) 3.71 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.applyObjectModification = applyObjectModification; exports.modifyObject = modifyObject; const lodash_mergewith_1 = __importDefault(require("lodash.mergewith")); const picocolors_1 = __importDefault(require("picocolors")); const processScript_1 = require("./processScript"); const prompter_1 = require("../prompter"); const variables_1 = require("../variables"); const satisfies_1 = require("./satisfies"); function applyObjectModification(content, action) { const setAction = (obj, strategy = 'assign') => { content = modifyObject(content, obj, strategy); Object.entries(obj).forEach(([key, value]) => { const strValue = typeof value === 'string' ? value : JSON.stringify(value); (0, prompter_1.logMessage)(`set ${picocolors_1.default.yellow(key)} with ${picocolors_1.default.yellow(strategy)} strategy: ${(0, prompter_1.summarize)(strValue)}`); }); }; if ('set' in action) { const strategy = action.strategy || 'assign'; action.set = (0, variables_1.transformTextInObject)(action.set); setAction(action.set, strategy); } else { (0, processScript_1.processScript)(action.script, variables_1.variables, false, false, { merge: setAction, }); } return content; } function modifyObject(content, modifier, strategy) { if (strategy == 'assign') { content = Object.assign(content, modifier); } else if (strategy == 'append') { for (const key in modifier) { if (!(key in content)) content[key] = modifier[key]; } } else { /* eslint-disable @typescript-eslint/no-unsafe-return */ const fieldsToDelete = []; const customizer = function (objValue, srcValue, field, objParent) { if (strategy == 'merge_concat') { if (Array.isArray(objValue) && Array.isArray(srcValue)) { return objValue.concat(srcValue); } } else if (strategy == 'merge_distinct') if (Array.isArray(objValue) && Array.isArray(srcValue)) { return objValue.concat(srcValue.filter(objB => // eslint-disable-next-line @typescript-eslint/no-unsafe-argument objValue.every(objA => !(0, satisfies_1.deepEquals)(objA, objB)))); } if (typeof srcValue === 'object' && srcValue.$assign) { delete srcValue.$assign; return srcValue; } if (typeof srcValue === 'object' && srcValue.$append) { delete srcValue.$append; return objValue; } if (typeof srcValue === 'object' && srcValue.$delete) { fieldsToDelete.push([objParent, field]); return true; } if (Array.isArray(objValue) && typeof srcValue === 'object' && srcValue.$index != null) { const index = srcValue.$index; delete srcValue.$index; objValue[index] = (0, lodash_mergewith_1.default)(objValue[index], srcValue, customizer); return objValue; } }; content = (0, lodash_mergewith_1.default)(content, modifier, customizer); fieldsToDelete.forEach(([obj, field]) => delete obj[field]); /* eslint-enable @typescript-eslint/no-unsafe-return */ } return content; }