react-native-integrate
Version:
Automate integration of additional code into React Native projects
170 lines (168 loc) • 6.81 kB
JavaScript
;
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.notificationServiceTask = notificationServiceTask;
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 notificationServiceTask(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: {
didReceiveNotificationRequest: {
regex: /didReceiveNotificationRequest:.*?withContentHandler.*?\{/s,
makeNewMethod: () => {
return '- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {}';
},
},
serviceExtensionTimeWillExpire: {
regex: /serviceExtensionTimeWillExpire.*?\{/s,
makeNewMethod: () => {
return '- (void)serviceExtensionTimeWillExpire {}';
},
},
},
swift: {
didReceiveNotificationRequest: {
regex: /didReceive.*?\{/s,
makeNewMethod: () => {
return `override func didReceive(
_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void
) {}`;
},
},
serviceExtensionTimeWillExpire: {
regex: /serviceExtensionTimeWillExpire.*?\{/s,
makeNewMethod: () => {
return 'override func serviceExtensionTimeWillExpire() {}';
},
},
},
};
function appendNewMethod(content, newMethod, lang) {
const notificationServiceMatch = lang === 'objc'
? /@implementation NotificationService.*?@end/s.exec(content)
: /class NotificationService:.*\}/s.exec(content);
if (!notificationServiceMatch)
throw new Error('Could not find @implementation NotificationService');
const codeToInsert = `${newMethod}
`;
return (0, stringSplice_1.stringSplice)(content, notificationServiceMatch.index +
notificationServiceMatch[0].length -
(lang === 'objc' ? 4 : 1), 0, codeToInsert);
}
function getNotificationServicePath(target, lang) {
const projectPath = (0, getProjectPath_1.getProjectPath)();
const notificationServicePath = path_1.default.join(projectPath, 'ios', target, lang === 'swift'
? constants_1.Constants.NOTIFICATION_SERVICE_SWIFT_FILE_NAME
: constants_1.Constants.NOTIFICATION_SERVICE_M_FILE_NAME);
if (!fs_1.default.existsSync(notificationServicePath))
throw new Error(`NotificationService file not found at ${notificationServicePath}`);
return notificationServicePath;
}
function readNotificationServiceContent(target, lang) {
const notificationServicePath = getNotificationServicePath(target, lang);
return fs_1.default.readFileSync(notificationServicePath, 'utf-8');
}
function writeNotificationServiceContent(content, target, lang) {
const notificationServicePath = getNotificationServicePath(target, lang);
return fs_1.default.writeFileSync(notificationServicePath, content, 'utf-8');
}
async function runTask(args) {
args.task.target = (0, variables_1.getText)(args.task.target);
let content = readNotificationServiceContent(args.task.target, args.task.lang);
content = await notificationServiceTask({
...args,
content,
});
writeNotificationServiceContent(content, args.task.target, args.task.lang);
}
exports.summary = 'NotificationService modification';