/* eslint-disable prefer-const */ import L from "../../utils/logger"; import path from "path"; import execa from "execa"; import ora from "ora"; import replace from "replace-in-file"; async function initApp(name: string, bundleId: string): Promise { const bundleIdTokens = bundleId.split("."); if (bundleIdTokens.length !== 3) { throw new Error("bundleId should be 3 digits X.Y.Z"); } L.info(`initialize a new react-native project: ${name}`); const cmd = `git clone https://github.com/revtel/react-native-starter ${name}`; L.info(`running following command:`); L.log(cmd); const spinner = ora("processing..."); try { spinner.start(); await execa.command(cmd); } finally { spinner.stop(); } L.info(`Replacing bundleId...`); 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/com/revteltech/revtelapp/MainApplication.java" ), path.join( name, "android/app/src//main/java/com/revteltech/revtelapp/MainActivity.java" ), ]; try { const result = await replace.replaceInFile({ files: replaceList, from: /com.revteltech.revtelapp/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] !== "com") { await execa.command( `mv ${name}/android/app/src/main/java/com ${name}/android/app/src/main/java/${bundlePath1}` ); } if (bundleIdTokens[1] !== "revteltech") { await execa.command( `mv ${name}/android/app/src/main/java/${bundlePath1}/revteltech ${name}/android/app/src/main/java/${bundlePath2}` ); } if (bundleIdTokens[2] !== "revtelapp") { await execa.command( `mv ${name}/android/app/src/main/java/${bundlePath2}/revtelapp ${name}/android/app/src/main/java/${bundlePath3}` ); } L.info(`Remove .git dir from template repo...`); await execa.command(`rm -rf ${name}/.git`); L.success(`project created`); L.warn( `REMINDER: you will still need to perform "git init" and "npm install" as well as "pod install" yourself` ); } export { initApp };