UNPKG

react-native-integrate

Version:

Automate integration of additional code into React Native projects

146 lines (145 loc) 6.02 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.babelConfigTask = babelConfigTask; exports.applyJSObjectModification = applyJSObjectModification; exports.shouldApplyInsertion = shouldApplyInsertion; exports.readBabelConfigContent = readBabelConfigContent; exports.writeBabelConfigContent = writeBabelConfigContent; exports.runTask = runTask; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const picocolors_1 = __importDefault(require("picocolors")); const prettier_1 = __importDefault(require("prettier")); const constants_1 = require("../constants"); const processScript_1 = require("../utils/processScript"); const prompter_1 = require("../prompter"); const applyContentModification_1 = require("../utils/applyContentModification"); const checkCondition_1 = require("../utils/checkCondition"); const getErrMessage_1 = require("../utils/getErrMessage"); const getProjectPath_1 = require("../utils/getProjectPath"); const jsObjectParser_1 = require("../utils/jsObjectParser"); const setState_1 = require("../utils/setState"); const variables_1 = require("../variables"); async function babelConfigTask(args) { let { content } = args; const { task, configPath, packageName } = args; for (const action of task.actions) { 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 { if ('mode' in action && action.mode === 'text') { const textContent = await (0, applyContentModification_1.applyContentModification)({ action, findOrCreateBlock: undefined, configPath, packageName, content: content.stringify(), indentation: 2, }); content.parse(textContent); } else { content = applyJSObjectModification(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; } } return Promise.resolve(content); } function applyJSObjectModification(content, action) { const setAction = (obj, strategy = 'merge_concat') => { if ('mode' in action) return content; content.merge({ [action.root ?? 'module.exports']: 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 || 'merge_concat'; action.set = (0, variables_1.transformTextInObject)(action.set); setAction(action.set, strategy); } else if (action.script) { (0, processScript_1.processScript)(action.script, variables_1.variables, false, false, { merge: setAction, }); } return content; } function shouldApplyInsertion(array, action, value) { if (action.ifNotPresent) { const { ifNotPresent } = action; if (array.some(x => x.includes(ifNotPresent))) { (0, prompter_1.logMessageGray)(`found existing ${(0, prompter_1.summarize)((0, variables_1.getText)(action.ifNotPresent))}, skipped adding: ${(0, prompter_1.summarize)(value)}`); (0, setState_1.setState)(action.name, { state: 'skipped', reason: 'insert.ifNotPresent', }); return false; } } if (array.some(x => x.includes(value))) { (0, prompter_1.logMessageGray)(`code already exists, skipped adding: ${(0, prompter_1.summarize)(value)}`); (0, setState_1.setState)(action.name, { state: 'skipped', reason: 'insert.exists', }); return false; } return true; } function getBabelConfigPath() { const projectPath = (0, getProjectPath_1.getProjectPath)(); const babelConfigPath = path_1.default.join(projectPath, constants_1.Constants.BABEL_CONFIG_FILE_NAME); if (!fs_1.default.existsSync(babelConfigPath)) throw new Error(`babel.config.js file not found at ${babelConfigPath}`); return babelConfigPath; } function readBabelConfigContent() { const babelConfigPath = getBabelConfigPath(); const parser = new jsObjectParser_1.JsObjectParser(); parser.parse(fs_1.default.readFileSync(babelConfigPath, 'utf-8')); return parser; } async function writeBabelConfigContent(parser) { const babelConfigPath = getBabelConfigPath(); let prettierConfig = await prettier_1.default.resolveConfig((0, getProjectPath_1.getProjectPath)()); if (!prettierConfig) prettierConfig = {}; if (!prettierConfig.parser) prettierConfig.parser = 'babel'; const content = await prettier_1.default.format(parser.stringify(), prettierConfig); return fs_1.default.writeFileSync(babelConfigPath, content, 'utf-8'); } async function runTask(args) { const parser = readBabelConfigContent(); await babelConfigTask({ ...args, content: parser, }); await writeBabelConfigContent(parser); } exports.summary = 'babel.config.js modification';