From 6e44e8e00d2fb1c59c1014c75b4c03238abba8ac Mon Sep 17 00:00:00 2001
From: huminmin <mac@MacBook-Pro.local>
Date: 星期四, 21 五月 2026 16:55:06 +0800
Subject: [PATCH] 显示生产数据

---
 multiple/multiple-build.js |  208 ++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 137 insertions(+), 71 deletions(-)

diff --git a/multiple/multiple-build.js b/multiple/multiple-build.js
index 8e078e1..afcd4d5 100644
--- a/multiple/multiple-build.js
+++ b/multiple/multiple-build.js
@@ -1,86 +1,152 @@
-import fs from 'fs/promises';
-import fsSync from 'fs';
-import path from 'path';
-import { fileURLToPath } from 'url';
+import fs from "fs/promises";
+import fsSync from "fs";
+import path from "path";
+import { fileURLToPath } from "url";
 import { execSync } from "child_process";
 
-// 鑾峰彇 __dirname
 const __filename = fileURLToPath(import.meta.url);
 const __dirname = path.dirname(__filename);
 
-// 璇诲彇 JSON 閰嶇疆
-const data = await fs.readFile(path.join(__dirname, 'config.json'), 'utf-8');
+const data = await fs.readFile(path.join(__dirname, "config.json"), "utf-8");
 const config = JSON.parse(data);
 
-// 椤圭洰璺緞
-const rootPath = path.resolve(__dirname, '..');
-const resourcePath = path.join(rootPath, 'multiple', 'assets');
-const replacePath = path.join(rootPath, 'replace');
+const rootPath = path.resolve(__dirname, "..");
+const resourcePath = path.join(rootPath, "multiple", "assets");
+const replacePath = path.join(rootPath, "replace");
+const envFilePath = path.join(rootPath, ".env.production.local");
 
-// 鑾峰彇鍛戒护琛屽弬鏁�
 const params = parseArgs(process.argv);
-const company = params["company"] ?? "default";
+const company = resolveCompany(params);
 const companyMap = config[company];
 
-const envFilePath = path.join(process.cwd(), '.env.production.local');
-
-try {
-    // 1锔忊儯 鐢熸垚 .env
-    console.log("=======鐢熸垚.env=======");
-    const envContent = Object.entries(companyMap.env)
-        .map(([key, value]) => `${key}='${value}'`)
-        .join('\n') + '\n';
-    await fs.writeFile(envFilePath, envContent, 'utf-8');
-
-    // 2锔忊儯 澶囦唤鍘熷璧勬簮骞舵浛鎹�
-    console.log("=======淇敼璧勬簮=======");
-    for (const [key, value] of Object.entries(companyMap)) {
-        if (key === 'env') continue;
-
-        const originFile = path.join(rootPath, config[key]);
-        const backupFile = path.join(replacePath, config[key]);
-        const replaceFile = path.join(resourcePath, companyMap[key]);
-
-        await fs.mkdir(path.dirname(backupFile), { recursive: true });
-        await fs.copyFile(originFile, backupFile);
-        await fs.copyFile(replaceFile, originFile);
-    }
-
-    console.log("=====寮�濮嬫墦鍖�======");
-    execSync("vite build", { stdio: "inherit" });
-    console.log("=====鎵撳寘瀹屾垚======");
-} finally {
-    console.log("=====鎭㈠璧勬簮======");
-
-    // 鍒犻櫎涓存椂 .env 鏂囦欢
-    if (fsSync.existsSync(envFilePath)) {
-        await fs.unlink(envFilePath);
-        console.log(`馃棏锔� 宸插垹闄� ${envFilePath}`);
-    }
-
-    // 鎭㈠璧勬簮鏂囦欢
-    if (fsSync.existsSync(replacePath)) {
-        for (const [key, value] of Object.entries(companyMap)) {
-            if (key === 'env') continue;
-
-            const originFile = path.join(rootPath, config[key]);
-            const backupFile = path.join(replacePath, config[key]);
-
-            await fs.copyFile(backupFile, originFile);
-        }
-        await fs.rm(replacePath, { recursive: true, force: true });
-        console.log(`馃棏锔� 宸插垹闄� ${replacePath}`);
-    }
+if (!companyMap) {
+  const availableCompanies = Object.entries(config)
+    .filter(([, value]) => value && typeof value === "object" && value.env)
+    .map(([key]) => key)
+    .sort();
+  throw new Error(
+    `鏈煡 company: "${company}"銆傚彲閫夊��: ${availableCompanies.join(", ")}`
+  );
 }
 
-// 绠�鍗曞懡浠よ鍙傛暟瑙f瀽
-function parseArgs(argv) {
-    const params = {};
-    for (const arg of argv.slice(2)) {
-        if (arg.startsWith('--')) {
-            const [key, value] = arg.slice(2).split('=');
-            params[key] = value ?? true;
-        }
+console.log(`褰撳墠 company: ${company}`);
+
+async function copyFileWithOverwrite(src, dest) {
+  await fs.mkdir(path.dirname(dest), { recursive: true });
+  if (fsSync.existsSync(dest)) {
+    try {
+      await fs.chmod(dest, 0o666);
+    } catch {
+      // Ignore chmod failure and continue.
     }
-    return params;
+    await fs.rm(dest, { force: true });
+  }
+  await fs.copyFile(src, dest);
+}
+
+try {
+  console.log("=======鐢熸垚.env=======");
+  const envContent =
+    Object.entries(companyMap.env)
+      .map(([key, value]) => `${key}='${value}'`)
+      .join("\n") + "\n";
+  await fs.writeFile(envFilePath, envContent, "utf-8");
+
+  console.log("=======淇敼璧勬簮=======");
+  for (const [key] of Object.entries(companyMap)) {
+    if (key === "env") continue;
+
+    const originFile = path.join(rootPath, config[key]);
+    const backupFile = path.join(replacePath, config[key]);
+    const replaceFile = path.join(resourcePath, companyMap[key]);
+
+    await copyFileWithOverwrite(originFile, backupFile);
+    await copyFileWithOverwrite(replaceFile, originFile);
+  }
+
+  console.log("=====寮�濮嬫墦鍖�=====");
+  const buildEnv = createBuildEnv(companyMap.env);
+  execSync("vite build", { stdio: "inherit", cwd: rootPath, env: buildEnv });
+  console.log("=====鎵撳寘瀹屾垚======");
+} finally {
+  console.log("=====鎭㈠璧勬簮======");
+
+  if (fsSync.existsSync(envFilePath)) {
+    await fs.unlink(envFilePath);
+    console.log(`馃棏锔� 宸插垹闄� ${envFilePath}`);
+  }
+
+  if (fsSync.existsSync(replacePath)) {
+    for (const [key] of Object.entries(companyMap)) {
+      if (key === "env") continue;
+
+      const originFile = path.join(rootPath, config[key]);
+      const backupFile = path.join(replacePath, config[key]);
+      await copyFileWithOverwrite(backupFile, originFile);
+    }
+    await fs.rm(replacePath, { recursive: true, force: true });
+    console.log(`馃棏锔� 宸插垹闄� ${replacePath}`);
+  }
+}
+
+function parseArgs(argv) {
+  const params = {};
+  for (let index = 2; index < argv.length; index++) {
+    const arg = argv[index];
+    if (!arg.startsWith("--")) continue;
+
+    const normalized = arg.slice(2);
+    const equalIndex = normalized.indexOf("=");
+    if (equalIndex >= 0) {
+      const key = normalized.slice(0, equalIndex);
+      const value = normalized.slice(equalIndex + 1);
+      params[key] = value || true;
+      continue;
+    }
+
+    const nextArg = argv[index + 1];
+    if (nextArg && !nextArg.startsWith("--")) {
+      params[normalized] = nextArg;
+      index += 1;
+      continue;
+    }
+
+    params[normalized] = true;
+  }
+  return params;
+}
+
+function resolveCompany(parsedParams) {
+  const fromArg = parseValue(parsedParams.company);
+  if (fromArg) return fromArg;
+
+  const fromNpmConfig = parseValue(process.env.npm_config_company);
+  if (fromNpmConfig) return fromNpmConfig;
+
+  const fromEnv = parseValue(process.env.COMPANY ?? process.env.company);
+  if (fromEnv) return fromEnv;
+
+  return "default";
+}
+
+function parseValue(value) {
+  if (value == null || value === true) return undefined;
+  if (typeof value !== "string") return undefined;
+  const trimmed = value.trim();
+  if (!trimmed) return undefined;
+  return trimmed.replace(/^["']|["']$/g, "");
+}
+
+function createBuildEnv(companyEnv) {
+  const env = { ...process.env };
+  for (const key of Object.keys(env)) {
+    if (key.startsWith("VITE_")) {
+      delete env[key];
+    }
+  }
+  return {
+    ...env,
+    ...companyEnv,
+    VITE_APP_ENV: "production",
+  };
 }

--
Gitblit v1.9.3