expo-pod-pinner
Version:
The Pod Pinner plugin allows you to modify your Podfile in a managed workflow.
59 lines (58 loc) • 2.88 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updatePodfile = void 0;
const PodPinnerLog_1 = require("./PodPinnerLog");
const FileManager_1 = require("./FileManager");
async function updatePodfile(iosPath, props) {
var _a, _b;
const podfileContent = await FileManager_1.FileManager.readFile(`${iosPath}/Podfile`);
let updatedPodfile = podfileContent;
function addPodSpec(podRegex, spec, podDeclaration, podName) {
const match = podRegex.exec(updatedPodfile);
if (match && match[1]) {
// If the pod is already in the Podfile, check if the version needs updating.
if (match[1] !== spec) {
updatedPodfile = updatedPodfile.replace(podRegex, podDeclaration);
PodPinnerLog_1.PodPinnerLog.log(`Updated version for ${podName} in Podfile.`);
}
else {
PodPinnerLog_1.PodPinnerLog.log(`${podName} already has spec ${spec} in Podfile. No update needed.`);
}
}
else {
// If the pod isn't in the Podfile, add it under the specified target.
const targetRegex = new RegExp(`target\\s+'${props.targetName.replace(/'/g, "\\'")}'\\s+do`, 'g');
const targetMatch = targetRegex.exec(updatedPodfile);
if (targetMatch) {
updatedPodfile = updatedPodfile.replace(targetRegex, `$&\n ${podDeclaration}`);
PodPinnerLog_1.PodPinnerLog.log(`Added ${podName} to ${props.targetName} target in Podfile.`);
}
else {
PodPinnerLog_1.PodPinnerLog.error(`Target "${props.targetName}" not found in Podfile.`);
}
}
}
(_a = props.pods) === null || _a === void 0 ? void 0 : _a.forEach((pod) => {
const podName = Object.keys(pod)[0];
const version = pod[podName];
const podDeclaration = `pod '${podName}', '${version}'`;
const podRegex = new RegExp(`pod\\s+'${podName}'\\s*,\\s*'([^']*)'`, 'g');
addPodSpec(podRegex, version, podDeclaration, podName);
});
(_b = props.specs) === null || _b === void 0 ? void 0 : _b.forEach((pod) => {
const podName = Object.keys(pod)[0];
const spec = pod[podName];
const podDeclaration = `pod '${podName}', ${spec}`;
const podRegex = new RegExp(`pod\\s+'${podName}'\\s*,\\s*'([^']*)'`, 'g');
addPodSpec(podRegex, spec, podDeclaration, podName);
});
// Only write to the Podfile if changes have been made.
if (updatedPodfile !== podfileContent) {
await FileManager_1.FileManager.writeFile(`${iosPath}/Podfile`, updatedPodfile);
PodPinnerLog_1.PodPinnerLog.log("Podfile updated successfully.");
}
else {
PodPinnerLog_1.PodPinnerLog.log("No changes made to the Podfile.");
}
}
exports.updatePodfile = updatePodfile;