UNPKG

react-native-integrate

Version:

Automate integration of additional code into React Native projects

125 lines (124 loc) 4.42 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.stringsXmlTask = stringsXmlTask; exports.runTask = runTask; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const constants_1 = require("../constants"); const applyContentModification_1 = require("../utils/applyContentModification"); const checkCondition_1 = require("../utils/checkCondition"); const findClosingTagIndex_1 = require("../utils/findClosingTagIndex"); const getErrMessage_1 = require("../utils/getErrMessage"); const getProjectPath_1 = require("../utils/getProjectPath"); const setState_1 = require("../utils/setState"); const variables_1 = require("../variables"); async function stringsXmlTask(args) { let { content } = args; const { task, configPath, packageName } = args; for (const action of task.actions) { action.block = 'resources'; 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 = await (0, applyContentModification_1.applyContentModification)({ action, findOrCreateBlock, configPath, packageName, content, indentation: 0, buildComment: buildXmlComment, }); (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; } } return content; } function buildXmlComment(comment) { return comment.split('\n').map(x => `<!-- ${x} -->`); } function checkBlockStartValue(value) { if (!value) { throw new Error('block could not be found, something wrong?'); } } function findOrCreateBlock(content) { let blockContent = { start: 0, end: content.length, match: content, space: '', justCreated: false, }; const blockDefinition = blockDefinitions['resources']; const { regex } = blockDefinition; const blockStart = regex.exec(content); checkBlockStartValue(blockStart); if (blockStart) { const blockEndIndex = (0, findClosingTagIndex_1.findClosingTagIndex)(content, blockStart.index + blockStart[0].length, findClosingTagIndex_1.TagDefinitions.XML); const blockBody = content.substring(blockStart.index + blockStart[0].length, blockEndIndex); blockContent = { start: blockStart.index + blockStart[0].length, end: blockEndIndex, match: blockBody, justCreated: false, space: ' '.repeat(blockDefinition.indentation), }; } return { blockContent, content, }; } const blockDefinitions = { resources: { regex: /<resources.*?>/s, indentation: 4, }, }; function getStringsXmlPath() { const projectPath = (0, getProjectPath_1.getProjectPath)(); const stringsXmlPath = path_1.default.join(projectPath, constants_1.Constants.ANDROID_MAIN_FILE_PATH, 'res', 'values', constants_1.Constants.STRINGS_XML_FILE_NAME); if (!fs_1.default.existsSync(stringsXmlPath)) throw new Error(`strings.xml file not found at ${stringsXmlPath}`); return stringsXmlPath; } function readStringsXmlContent() { const xmlPath = getStringsXmlPath(); return fs_1.default.readFileSync(xmlPath, 'utf-8'); } function writeStringsXmlContent(content) { const xmlPath = getStringsXmlPath(); return fs_1.default.writeFileSync(xmlPath, content, 'utf-8'); } async function runTask(args) { let content = readStringsXmlContent(); content = await stringsXmlTask({ ...args, content, }); writeStringsXmlContent(content); } exports.summary = 'strings.xml modification';