UNPKG

react-native-integrate

Version:

Automate integration of additional code into React Native projects

89 lines (88 loc) 3.52 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.summary = void 0; exports.plistTask = plistTask; exports.readPListContent = readPListContent; exports.writePListContent = writePListContent; exports.runTask = runTask; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const simple_plist_1 = __importDefault(require("simple-plist")); const constants_1 = require("../constants"); const applyObjectModification_1 = require("../utils/applyObjectModification"); const checkCondition_1 = require("../utils/checkCondition"); const getErrMessage_1 = require("../utils/getErrMessage"); const getIosProjectPath_1 = require("../utils/getIosProjectPath"); const getProjectPath_1 = require("../utils/getProjectPath"); const setState_1 = require("../utils/setState"); const variables_1 = require("../variables"); function plistTask(args) { let { content } = args; const { task } = args; for (const action of task.actions) { variables_1.variables.set('CONTENT', content); if (action.when && !(0, checkCondition_1.checkCondition)(action.when)) { (0, setState_1.setState)(action.name, { state: 'skipped', reason: 'when', }); continue; } (0, setState_1.setState)(action.name, { state: 'progress', }); try { content = (0, applyObjectModification_1.applyObjectModification)(content, action); (0, setState_1.setState)(action.name, { state: 'done', }); } catch (e) { (0, setState_1.setState)(action.name, { state: 'error', reason: (0, getErrMessage_1.getErrMessage)(e), }); throw e; } } content = Object.keys(content) .sort() .reduce((temp_obj, key) => { temp_obj[key] = content[key]; return temp_obj; }, {}); return content; } function getPListPath(target, filename, skipCheck) { if (!target) target = (0, getIosProjectPath_1.getIosProjectName)(); const projectPath = (0, getProjectPath_1.getProjectPath)(); const pListPath = path_1.default.join(projectPath, 'ios', target, filename); if (!skipCheck && !fs_1.default.existsSync(pListPath)) throw new Error(`Plist file not found at ${pListPath}`); return pListPath; } function readPListContent(target, filename = constants_1.Constants.PLIST_FILE_NAME, skipCheck = false) { const plistPath = getPListPath(target, filename, skipCheck); if (skipCheck && !fs_1.default.existsSync(plistPath)) return {}; return simple_plist_1.default.parse(fs_1.default.readFileSync(plistPath, 'utf-8')); } function writePListContent(content, target, filename = constants_1.Constants.PLIST_FILE_NAME) { const plistPath = getPListPath(target, filename, true); return fs_1.default.writeFileSync(plistPath, simple_plist_1.default.stringify(content), 'utf-8'); } function runTask(args) { if (args.task.target) args.task.target = (0, variables_1.getText)(args.task.target); let content = readPListContent(args.task.target); content = plistTask({ ...args, content, }); writePListContent(content, args.task.target); } exports.summary = 'Info.plist modification';