Alex Trofimov 10 месяцев назад
Родитель
Сommit
72afab9365
3 измененных файлов с 81 добавлено и 53 удалено
  1. 15 11
      app.json
  2. 1 1
      package.json
  3. 65 41
      scripts/rebrand.js

+ 15 - 11
app.json

@@ -11,14 +11,18 @@
     "ios": {
       "bundleIdentifier": "com.unacms.app",
       "infoPlist": {
-          "UIBackgroundModes": ["remote-notification"],
-          "NSAppTransportSecurity": {"NSAllowsArbitraryLoads": true}
+        "UIBackgroundModes": [
+          "remote-notification"
+        ],
+        "NSAppTransportSecurity": {
+          "NSAllowsArbitraryLoads": true
+        }
       },
       "entitlements": {
-          "aps-environment": "development",
-          "com.apple.security.application-groups": [
-              "group.${ios.bundleIdentifier}.onesignal"
-          ]
+        "aps-environment": "development",
+        "com.apple.security.application-groups": [
+          "group.${ios.bundleIdentifier}.onesignal"
+        ]
       },
       "supportsTablet": true
     },
@@ -40,10 +44,10 @@
     },
     "plugins": [
       [
-          "onesignal-expo-plugin",
-          {
-            "mode": "development"
-          }
+        "onesignal-expo-plugin",
+        {
+          "mode": "development"
+        }
       ],
       "expo-router",
       [
@@ -64,4 +68,4 @@
       "reactCompiler": true
     }
   }
-}
+}

+ 1 - 1
package.json

@@ -4,7 +4,7 @@
   "version": "3.0.0",
   "scripts": {
     "start": "expo start",
-    "rebrand": "node ./scripts/reset-project.js",
+    "rebrand": "node ./scripts/rebrand.js",
     "android": "expo run:android",
     "ios": "expo run:ios",
     "lint": "expo lint"

+ 65 - 41
scripts/rebrand.js

@@ -4,57 +4,81 @@
  * This script is used to rebrand the app to a different UNACMS website.
  */
 
-const fs = require("fs");
+const fs = require("fs").promises;
 const path = require("path");
 const readline = require("readline");
 
 const root = process.cwd();
 const settingsPath = path.join(root, "constants", "settings.ts");
+const appJsonPath = path.join(root, "app.json");
 
-const rl = readline.createInterface({
-  input: process.stdin,
-  output: process.stdout,
-});
-
-rl.question(
-  "Your UNA site URL: ",
-  (answer) => {
-    const userInput = answer.trim().replace(/\/$/, "").toLowerCase();
-
-    // Simple URL validation (domain or https://...)
-    const urlPattern = /^(https?:\/\/)?([a-z0-9-]+\.)+[a-z]{2,}$/i;
-    if (!userInput || !urlPattern.test(userInput)) {
-      console.log("❌ Invalid input. Please enter a valid URL.");
-      rl.close();
-      return;
-    }
-
-    const newUrl = userInput.startsWith("http")
-      ? userInput
-      : `https://${userInput}`;
-
-    // Read settings.ts
-    fs.readFile(settingsPath, "utf8", (err, data) => {
-      if (err) {
-        console.error(`❌ Failed to read settings.ts: ${err.message}`);
-        rl.close();
+// Helper to ask questions with async/await
+function askQuestion(query) {
+  const rl = readline.createInterface({
+    input: process.stdin,
+    output: process.stdout,
+  });
+  return new Promise((resolve) => rl.question(query, (ans) => {
+    rl.close();
+    resolve(ans);
+  }));
+}
+
+(async () => {
+  try {
+    // --------- replace UNA URL
+    await (async () => {
+      const answer = await askQuestion("Your UNA site URL: ");
+      const userInput = answer.trim().replace(/\/$/, "").toLowerCase();
+
+      // Simple URL validation (domain or https://...)
+      const urlPattern = /^(https?:\/\/)?([a-z0-9-]+\.)+[a-z]{2,}(\/[a-zA-Z0-9\-._~:/?#[\]@!$&'()*+,;=]*)?$/i;
+      if (!userInput || !urlPattern.test(userInput)) {
+        console.log("❌ Invalid input. Please enter a valid URL.");
         return;
       }
+      
+      const newUrl = userInput.startsWith("http") ? userInput : `https://${userInput}`;
 
-      // Replace the UNA_URL line
-      const updated = data.replace(
+      let settingsData = await fs.readFile(settingsPath, "utf8");
+      settingsData = settingsData.replace(
         /export const UNA_URL\s*=\s*["'`].*?["'`];/,
         `export const UNA_URL = "${newUrl}";`
       );
+      await fs.writeFile(settingsPath, settingsData, "utf8");
+      console.log(`✅ UNA_URL updated to: ${newUrl}`);
+
+    })();
+
+    // --------- replace Bundle ID
+    await (async () => {
+      const answer = await askQuestion("Your app bundle id or package name (for example: com.example.app): ");
+      const userInput = answer.trim().toLowerCase();
+
+      const bundleId = userInput.trim();
+      const bundlePattern = /^([a-zA-Z0-9]+\.)+[a-zA-Z0-9]+$/;
+      if (!bundlePattern.test(bundleId)) {
+        console.log("❌ Invalid bundle ID.");
+        return;
+      }
+
+      const appJsonData = await fs.readFile(appJsonPath, "utf8");
+      const appJson = JSON.parse(appJsonData);
+
+      appJson.expo = appJson.expo || {};
+      appJson.expo.android = appJson.expo.android || {};
+      appJson.expo.ios = appJson.expo.ios || {};
+
+      appJson.expo.android.package = bundleId;
+      appJson.expo.ios.bundleIdentifier = bundleId;
+
+      await fs.writeFile(appJsonPath, JSON.stringify(appJson, null, 2), "utf8");
+      console.log(`✅ Bundle ID updated to: ${bundleId}`);
+
+    })();
 
-      fs.writeFile(settingsPath, updated, "utf8", (err) => {
-        if (err) {
-          console.error(`❌ Failed to write settings.ts: ${err.message}`);
-        } else {
-          console.log(`✅ UNA_URL updated to: ${newUrl}`);
-        }
-        rl.close();
-      });
-    });
-  }
-);
+  } catch (err) {
+    console.error("❌ An error occurred:", err);
+    const userInput = answer.trim().toLowerCase();
+  } 
+})();