UNPKG

react-native-integrate

Version:

Automate integration of additional code into React Native projects

97 lines (96 loc) 4.4 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isSystemTask = exports.summary = exports.runTask = void 0; exports.scriptTask = scriptTask; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const constants_1 = require("../constants"); const getPackageConfig_1 = require("../utils/getPackageConfig"); const processScript_1 = require("../utils/processScript"); const checkCondition_1 = require("../utils/checkCondition"); const getErrMessage_1 = require("../utils/getErrMessage"); const setState_1 = require("../utils/setState"); const variables_1 = require("../variables"); async function scriptTask(args) { const { task } = 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 { const ctx = Object.entries(args.taskManager.task).reduce((ctx, [taskName, task]) => { ctx[taskName] = async (actionOneOrList, opts) => { const dynamicTask = { task: taskName, ...opts, actions: Array.isArray(actionOneOrList) ? actionOneOrList : [actionOneOrList], }; await task.runTask({ configPath: args.configPath, packageName: args.packageName, task: dynamicTask, taskManager: args.taskManager, }); }; return ctx; }, { get: function (variable) { return variables_1.variables.get(variable); }, set: function (variable, value) { variables_1.variables.set(variable, value); }, }); let resultValue; if ('script' in action) { resultValue = await (0, processScript_1.processScript)(action.script, variables_1.variables, false, true, ctx); } else { const pluginPath = path_1.default.join(path_1.default.dirname(args.configPath), action.module); if (!fs_1.default.existsSync(pluginPath) && args.packageName !== constants_1.Constants.UPGRADE_CONFIG_FILE_NAME) { const remotePath = (0, getPackageConfig_1.getRemotePath)(args.packageName, constants_1.Constants.REMOTE_REPO) + pluginPath.replace(path_1.default.join(args.configPath, '../'), ''); const localDir = path_1.default.join(pluginPath, '..'); if (!fs_1.default.existsSync(localDir)) fs_1.default.mkdirSync(localDir, { recursive: true }); const success = await (0, getPackageConfig_1.downloadFile)(remotePath, pluginPath); if (!success) throw new Error(`File not found at ${pluginPath}`); } // eslint-disable-next-line @typescript-eslint/no-require-imports const plugin = require(path_1.default.relative(__dirname, path_1.default.join(path_1.default.dirname(args.configPath), action.module))); if ('default' in plugin) { resultValue = await plugin.default(ctx); } else resultValue = await plugin(ctx); } if (action.name && resultValue != null) variables_1.variables.set(action.name, resultValue); } catch (e) { (0, setState_1.setState)(action.name, { state: 'error', reason: (0, getErrMessage_1.getErrMessage)(e), }); throw e; } } } exports.runTask = scriptTask; exports.summary = ''; // noinspection JSUnusedGlobalSymbols exports.isSystemTask = true;