UNPKG

react-native-integrate

Version:

Automate integration of additional code into React Native projects

133 lines (132 loc) 5.28 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.buildGradleTask = buildGradleTask; exports.findOrCreateBlock = findOrCreateBlock; 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 stringSplice_1 = require("../utils/stringSplice"); const variables_1 = require("../variables"); async function buildGradleTask(args) { let { content } = args; const { task, configPath, packageName } = 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 = await (0, applyContentModification_1.applyContentModification)({ action, findOrCreateBlock, configPath, packageName, content, indentation: 4, }); (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 findOrCreateBlock(content, block) { let blockContent = { start: 0, end: content.length, match: content, space: '', justCreated: false, }; const blockPath = block.split('.'); let contentOffset = 0; for (let i = 0; i < blockPath.length; i++) { const matcherRegex = new RegExp(`^((\\s+)?)${blockPath[i]}\\s+\\{`, 'ms'); let blockStart = matcherRegex.exec(blockContent.match); const justCreated = !blockStart; if (!blockStart) { const blockName = blockPath[i]; // create block in block const space = ' '.repeat(4 * i); const previousSpace = ' '.repeat(Math.max(0, 4 * (i - 1))); const newBlock = `${space}${blockName} {}`; const codeToInsert = ` ${newBlock} ${previousSpace}`; const contentLengthBeforeInsert = content.length; content = (0, stringSplice_1.stringSplice)(content, blockContent.end, 0, codeToInsert); if (codeToInsert.length && contentLengthBeforeInsert < content.length) { blockContent.match += codeToInsert; blockContent.end += codeToInsert.length; blockStart = matcherRegex.exec(blockContent.match); } } if (!blockStart) { throw new Error('block could not be inserted, something wrong?'); } const blockEndIndex = (0, findClosingTagIndex_1.findClosingTagIndex)(content, contentOffset + blockStart.index + blockStart[0].length); const blockBody = content.substring(contentOffset + blockStart.index + blockStart[0].length, blockEndIndex); blockContent = { start: contentOffset + blockStart.index + blockStart[0].length, end: blockEndIndex, match: blockBody, justCreated, space: ' '.repeat(4 * i), }; contentOffset += blockStart.index + blockStart[0].length; } return { blockContent, content, }; } function getBuildGradlePath(location) { const projectPath = (0, getProjectPath_1.getProjectPath)(); const buildGradlePath = path_1.default.join(projectPath, 'android', location == 'app' ? 'app' : '', constants_1.Constants.BUILD_GRADLE_FILE_NAME); if (!fs_1.default.existsSync(buildGradlePath)) throw new Error(`build.gradle file not found at ${buildGradlePath}`); return buildGradlePath; } function readBuildGradleContent(location = 'root') { const buildGradlePath = getBuildGradlePath(location); return fs_1.default.readFileSync(buildGradlePath, 'utf-8'); } function writeBuildGradleContent(content, location = 'root') { const buildGradlePath = getBuildGradlePath(location); return fs_1.default.writeFileSync(buildGradlePath, content, 'utf-8'); } async function runTask(args) { let content = readBuildGradleContent(args.task.location); content = await buildGradleTask({ ...args, content, }); writeBuildGradleContent(content, args.task.location); } exports.summary = 'build.gradle modification';