/* eslint-disable prefer-const */ import L from "../../utils/logger"; import path from "path"; import execa from "execa"; import replace from "replace-in-file"; import { findCurrentPkgName } from "../../utils/findCurrentPkgName"; import { removeLastSlash } from "../../utils/removeLastSlash"; async function updateBundleId(bundleId: string, options?: any): Promise { if (!options) { options = {}; } const pwd = removeLastSlash(options.pwd || "."); const bundleIdTokens = bundleId.split("."); if (bundleIdTokens.length !== 3) { throw new Error("bundleId should be 3 digits X.Y.Z"); } const currBundleId = findCurrentPkgName(pwd); const currBundleIdTokens = (currBundleId || "").split("."); if (!currBundleId || currBundleIdTokens.length !== 3) { throw new Error("currBundleId should be 3 digits X.Y.Z"); } L.info(`Replacing bundleId from ${currBundleId} -> ${bundleId}`); const name = pwd; const replaceList = [ path.join(name, "ios/RevtelApp/Info.plist"), // we skip the "ios/RevtelApp.xcodeproj/project.pbxproj" for now // since to correctly modify it requires another Ruby package path.join(name, "android/app/build.gradle"), path.join(name, "android/app/src//main/AndroidManifest.xml"), path.join( name, `android/app/src/main/java/${currBundleIdTokens.join( "/" )}/MainApplication.java` ), path.join( name, `android/app/src/main/java/${currBundleIdTokens.join( "/" )}/MainActivity.java` ), ]; try { const result = await replace.replaceInFile({ files: replaceList, from: new RegExp(currBundleId, "g"), to: bundleId, }); console.log(result); L.log("replace bundleId successfully"); } catch (error) { L.error("replace bundleId fail"); throw error; } L.info(`Adjust android project src directory...`); const bundlePath1 = bundleIdTokens[0]; const bundlePath2 = bundleIdTokens.slice(0, 2).join("/"); const bundlePath3 = bundleIdTokens.slice(0, 3).join("/"); if (bundleIdTokens[0] !== currBundleIdTokens[0]) { await execa.command( `mv ${name}/android/app/src/main/java/${currBundleIdTokens[0]} ${name}/android/app/src/main/java/${bundlePath1}` ); } if (bundleIdTokens[1] !== currBundleIdTokens[1]) { await execa.command( `mv ${name}/android/app/src/main/java/${bundlePath1}/${currBundleIdTokens[1]} ${name}/android/app/src/main/java/${bundlePath2}` ); } if (bundleIdTokens[2] !== currBundleIdTokens[2]) { await execa.command( `mv ${name}/android/app/src/main/java/${bundlePath2}/${currBundleIdTokens[2]} ${name}/android/app/src/main/java/${bundlePath3}` ); } } export { updateBundleId };