UNPKG

react-native-integrate

Version:

Automate integration of additional code into React Native projects

238 lines (236 loc) 8.93 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.notificationViewControllerTask = notificationViewControllerTask; 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 notificationViewControllerTask(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: findOrCreateBlock(task.lang), 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; } const findOrCreateBlock = (lang) => { const _lang = lang || 'objc'; return (content, block) => { let blockContent = { start: 0, end: content.length, match: content, space: '', justCreated: false, }; const blockDefinition = blockDefinitions[_lang][block]; if (!blockDefinition) throw new Error(`Invalid block: ${block}`); const { regex, makeNewMethod } = blockDefinition; let blockStart = regex.exec(content); const justCreated = !blockStart; if (!blockStart) { const newMethod = makeNewMethod(); content = appendNewMethod(content, newMethod, _lang); blockStart = regex.exec(content); } if (!blockStart) { throw new Error('block could not be inserted, something wrong?'); } const blockEndIndex = (0, findClosingTagIndex_1.findClosingTagIndex)(content, blockStart.index + blockStart[0].length); const blockBody = content.substring(blockStart.index + blockStart[0].length, blockEndIndex); blockContent = { start: blockStart.index + blockStart[0].length, end: blockEndIndex, match: blockBody, justCreated, space: _lang === 'swift' ? ' '.repeat(2) : '', }; return { blockContent, content, }; }; }; const blockDefinitions = { objc: { viewDidLoad: { regex: /viewDidLoad.*?\{/s, makeNewMethod: () => { return '- (void)viewDidLoad {}'; }, }, viewWillAppear: { regex: /viewWillAppear.*?\{/s, makeNewMethod: () => { return '- (void)viewWillAppear:(BOOL)animated {}'; }, }, viewDidAppear: { regex: /viewDidAppear.*?\{/s, makeNewMethod: () => { return '- (void)viewDidAppear:(BOOL)animated {}'; }, }, viewWillDisappear: { regex: /viewWillDisappear.*?\{/s, makeNewMethod: () => { return '- (void)viewWillDisappear:(BOOL)animated {}'; }, }, dealloc: { regex: /dealloc.*?\{/s, makeNewMethod: () => { return '- (void)dealloc {}'; }, }, didReceiveNotification: { regex: /didReceiveNotification\b.*?\{/s, makeNewMethod: () => { return '- (void)didReceiveNotification:(UNNotification *)notification {}'; }, }, didReceiveNotificationResponse: { regex: /didReceiveNotificationResponse.*?\{/s, makeNewMethod: () => { return '- (void)didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(UNNotificationContentExtensionResponseOption option))completion {}'; }, }, }, swift: { viewDidLoad: { regex: /viewDidLoad.*?\{/s, makeNewMethod: () => { return `override func viewDidLoad() { super.viewDidLoad() }`; }, }, viewWillAppear: { regex: /viewWillAppear.*?\{/s, makeNewMethod: () => { return `override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) }`; }, }, viewDidAppear: { regex: /viewDidAppear.*?\{/s, makeNewMethod: () => { return `override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) }`; }, }, viewWillDisappear: { regex: /viewWillDisappear.*?\{/s, makeNewMethod: () => { return `override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) }`; }, }, dealloc: { regex: /deinit.*?\{/s, makeNewMethod: () => { return 'deinit {}'; }, }, didReceiveNotification: { regex: /didReceive\s*\(.*?UNNotification.*?\{/s, makeNewMethod: () => { return 'func didReceive(_ notification: UNNotification) {}'; }, }, didReceiveNotificationResponse: { regex: /didReceive\s*\(.*?UNNotificationResponse.*?\{/s, makeNewMethod: () => { return `func didReceive( _ response: UNNotificationResponse, completionHandler: @escaping (UNNotificationContentExtensionResponseOption) -> Void ) {}`; }, }, }, }; function appendNewMethod(content, newMethod, lang) { const notificationContentMatch = lang === 'objc' ? /@implementation NotificationViewController.*?@end/s.exec(content) : /class NotificationViewController:.*\}/s.exec(content); if (!notificationContentMatch) throw new Error('Could not find @implementation NotificationViewController'); const codeToInsert = `${newMethod} `; return (0, stringSplice_1.stringSplice)(content, notificationContentMatch.index + notificationContentMatch[0].length - (lang === 'objc' ? 4 : 1), 0, codeToInsert); } function getNotificationContentPath(target, lang) { const projectPath = (0, getProjectPath_1.getProjectPath)(); const notificationContentPath = path_1.default.join(projectPath, 'ios', target, lang === 'swift' ? constants_1.Constants.NOTIFICATION_VIEW_CONTROLLER_SWIFT_FILE_NAME : constants_1.Constants.NOTIFICATION_VIEW_CONTROLLER_M_FILE_NAME); if (!fs_1.default.existsSync(notificationContentPath)) throw new Error(`NotificationContent file not found at ${notificationContentPath}`); return notificationContentPath; } function readNotificationContentContent(target, lang) { const notificationContentPath = getNotificationContentPath(target, lang); return fs_1.default.readFileSync(notificationContentPath, 'utf-8'); } function writeNotificationContentContent(content, target, lang) { const notificationContentPath = getNotificationContentPath(target, lang); return fs_1.default.writeFileSync(notificationContentPath, content, 'utf-8'); } async function runTask(args) { args.task.target = (0, variables_1.getText)(args.task.target); let content = readNotificationContentContent(args.task.target, args.task.lang); content = await notificationViewControllerTask({ ...args, content, }); writeNotificationContentContent(content, args.task.target, args.task.lang); } exports.summary = 'NotificationViewController modification';