Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

20.2 KB
Raw Blame
import { execSync } from 'node:child_process';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

import dotenv from 'dotenv';

import { copyExternalRuntimeModulesToSource } from './external-runtime-deps.config.mjs';
import { getModuleFilesConfig } from './module-deps.config.mjs';
import {
  buildFirstPartyNativeAddons,
  copyNativeModulesToSource,
  getAsarUnpackPatterns,
  getNativeModulesFilesConfig,
} from './native-deps.config.mjs';
import {
  COMPUTER_USE_HELPER_BUNDLE_PATH,
  COMPUTER_USE_HELPER_SIGN_IGNORE,
  embedComputerUseHelper,
  resolveComputerUseHelperVersion,
  verifyComputerUseHelper,
} from './scripts/computerUseHelper.mjs';
import { notarizeMacApp } from './scripts/notarizeMacApp.mjs';
import { packBuiltinCore } from './scripts/packBuiltinCore.mjs';
import { toSparkleBuildVersion } from './scripts/sparkleBuildVersion.mjs';
import { resolveSparklePackaging } from './scripts/sparklePackaging.mjs';
import { verifyFontListSignature } from './scripts/verifyFontListSigning.mjs';

dotenv.config();

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const packageJSON = JSON.parse(await fs.readFile(path.join(__dirname, 'package.json'), 'utf8'));

const computerUseHelperVersion = resolveComputerUseHelperVersion(packageJSON);

const channel = process.env.UPDATE_CHANNEL;
const arch = os.arch();
const hasAppleCertificate = Boolean(process.env.CSC_LINK);

/**
 * Distribution identity. A downstream build (private deployment, white-label)
 * sets these through env instead of forking this file; unset keeps upstream's
 * values.
 *
 * `appId` is not cosmetic: the updater and the OS key the installed app's
 * identity off it, so a distribution must pick one before its first release and
 * never change it. The packaged `productName` (which names the per-user data
 * directory) is stamped into package.json by
 * `scripts/electronWorkflow/setDesktopVersion.ts` from the same
 * DESKTOP_PRODUCT_NAME; `brandName` here is the channel-less form for
 * user-facing strings.
 */
const appId = process.env.DESKTOP_APP_ID || 'com.lobehub.lobehub-desktop';
const brandName = process.env.DESKTOP_PRODUCT_NAME || 'LobeHub';
const executableName = process.env.DESKTOP_EXECUTABLE_NAME || 'LobeHub';
// Communication notifications need the restricted
// `com.apple.developer.usernotifications.communication` entitlement, and
// macOS refuses to launch an app carrying it without a provisioning profile
// that authorizes it — so both must be applied together, and only when a
// profile is provided.
const macProvisioningProfile = process.env.MAC_PROVISIONING_PROFILE;
const macTeamId = process.env.APPLE_TEAM_ID;
const macCommunicationEntitlements =
  macProvisioningProfile && macTeamId
    ? path.join(__dirname, 'build', 'entitlements.mac.comm.generated.plist')
    : undefined;

if (macProvisioningProfile && !macTeamId) {
  console.warn(
    '⚠️ MAC_PROVISIONING_PROFILE is set but APPLE_TEAM_ID is missing — building without communication notification entitlements',
  );
}

if (macCommunicationEntitlements) {
  const baseEntitlements = await fs.readFile(
    path.join(__dirname, 'build', 'entitlements.mac.plist'),
    'utf8',
  );
  const communicationKeys = [
    '    <key>com.apple.application-identifier</key>',
    `    <string>${macTeamId}.${appId}</string>`,
    '    <key>com.apple.developer.team-identifier</key>',
    `    <string>${macTeamId}</string>`,
    '    <key>com.apple.developer.usernotifications.communication</key>',
    '    <true/>',
    '  </dict>',
  ].join('\n');
  await fs.writeFile(
    macCommunicationEntitlements,
    baseEntitlements.replace('</dict>', communicationKeys),
  );
  console.info('🔔 Communication notification entitlements + provisioning profile enabled');
}

// 自定义更新服务器 URL (用于 stable 频道)
const updateServerUrl = process.env.UPDATE_SERVER_URL;

console.info(`🚄 Build Version ${packageJSON.version}, Channel: ${channel}`);
console.info(`🏗️ Building for architecture: ${arch}`);

// Channel identity derived solely from UPDATE_CHANNEL env var.
// Supported channels: stable, nightly, canary
const isStable = !channel || channel === 'stable';
const isNightly = channel === 'nightly';
const isCanary = channel === 'canary';

// Strip trailing channel path from URL for re-appending the correct channel
// Handles both base URL (https://cdn.example.com) and legacy URL with channel (https://cdn.example.com/stable)
const stripChannelSuffix = (url) => url.replace(/\/(stable|nightly|canary|beta)\/?$/, '');

// 根据 channel 配置 publish provider
// - 所有渠道 + UPDATE_SERVER_URL: 使用 generic (S3)
// - 无 UPDATE_SERVER_URL: 回退到 GitHub (本地开发)
const getPublishConfig = () => {
  const channelPath = isStable ? 'stable' : isNightly ? 'nightly' : channel || 'stable';

  if (updateServerUrl) {
    const baseUrl = stripChannelSuffix(updateServerUrl);
    const fullUrl = `${baseUrl}/${channelPath}`;
    console.info(`📦 ${channelPath} channel: Using generic provider (${fullUrl})`);
    return [
      {
        provider: 'generic',
        url: fullUrl,
      },
    ];
  }

  // 本地开发无 S3 时回退到 GitHub
  console.info(`📦 ${channelPath} channel: No UPDATE_SERVER_URL, falling back to GitHub provider`);
  return [
    {
      owner: 'lobehub',
      provider: 'github',
      repo: 'lobehub',
    },
  ];
};

// https://www.electron.build/code-signing-mac#how-to-disable-code-signing-during-the-build-process-on-macos
if (!hasAppleCertificate) {
  // Disable auto discovery to keep electron-builder from searching unavailable signing identities
  process.env.CSC_IDENTITY_AUTO_DISCOVERY = 'false';
  console.info('⚠️ Apple certificate link not found, macOS artifacts will be unsigned.');
}

// 根据版本类型确定协议 scheme
const getProtocolScheme = () => {
  // Schemes are registered OS-wide, so a distribution needs its own: two builds
  // claiming `lobehub://` on one machine race for the handler, and the loser's
  // deep links and OAuth handoffs open the other app. Must match the value the
  // main process is built with (see vite.main.config.ts).
  const scheme = process.env.DESKTOP_PROTOCOL_SCHEME;
  if (scheme) {
    // URL parsing lowercases schemes, so the main process can only ever match a
    // lowercase one — fail the build rather than ship an app that drops its own
    // deep links.
    if (!/^[a-z][\d+.a-z-]*$/.test(scheme)) {
      throw new Error(
        `DESKTOP_PROTOCOL_SCHEME "${scheme}" must be a lowercase URL scheme (a-z, then a-z, 0-9, "+", "-", ".")`,
      );
    }
    return scheme;
  }
  if (isCanary) return 'lobehub-canary';
  if (isNightly) return 'lobehub-nightly';
  return 'lobehub';
};

const protocolScheme = getProtocolScheme();

const sparklePublicKey = process.env.SPARKLE_ED_PUBLIC_KEY;
const { useSparkle } = resolveSparklePackaging({
  channel,
  hasAppleCertificate,
  platform: process.platform,
  sparklePublicKey,
  updateServerUrl,
});
const sparklePackageDir = useSparkle
  ? await fs.realpath(path.join(__dirname, 'node_modules/electron-sparkle-updater'))
  : null;
const sparkleFeedUrl =
  useSparkle && updateServerUrl
    ? `${stripChannelSuffix(updateServerUrl).replace(/\/$/, '')}/${isCanary || channel === 'beta' ? 'canary' : 'stable'}/appcast-${arch}.xml`
    : undefined;

// Determine icon file based on version type
const getIconFileName = () => {
  if (isStable || isCanary) return 'Icon';
  // nightly uses pre-release icon
  return 'Icon-nightly';
};

/**
 * @type {import('electron-builder').Configuration}
 * @see https://www.electron.build/configuration
 */
const config = {
  /**
   * BeforePack hook to resolve pnpm symlinks for native modules.
   * This ensures native modules are properly included in the asar archive.
   */
  beforePack: async (context) => {
    buildFirstPartyNativeAddons();

    if (sparklePackageDir) {
      console.info('🔧 Building Sparkle bridge addon...');
      execSync(
        `node "${path.join(sparklePackageDir, 'bin/electron-sparkle-updater.js')}" rebuild --arch ${arch}`,
        { cwd: __dirname, stdio: 'inherit' },
      );
    }

    await copyNativeModulesToSource();
    await copyExternalRuntimeModulesToSource();

    // Keep the AUV daemon version locked to @auv-js/sdk. The CLI package
    // resolves the platform-specific executable without running postinstall,
    // then we stage that real file outside app.asar for child_process.spawn().
    const { binaryPath: resolveAuvBinaryPath } = await import('@auv-js/cli/binary');
    const auvSource = resolveAuvBinaryPath();
    const auvExecutable = process.platform === 'win32' ? 'auv.exe' : 'auv';
    const auvDestination = path.resolve(__dirname, 'resources/bin', auvExecutable);
    await fs.mkdir(path.dirname(auvDestination), { recursive: true });
    await fs.copyFile(auvSource, auvDestination);
    if (process.platform !== 'win32') await fs.chmod(auvDestination, 0o755);

    // agent-browser is no longer bundled in the installer — BinaryManager
    // lazily downloads it on first use into the per-user cache dir. See
    // apps/desktop/src/main/modules/binaries/agentBrowserBinaries.ts.

    console.info('📦 Building CLI for embedding...');
    execSync('npm run build:cli', { stdio: 'inherit', cwd: __dirname });

    // The shell window has no React/i18next runtime. Bundle only its translated strings.
    const updateStrings = {};
    for (const locale of ['en-US', 'zh-CN']) {
      const messages = JSON.parse(
        await fs.readFile(path.join(__dirname, '../../locales', locale, 'electron.json'), 'utf8'),
      );
      updateStrings[locale] = Object.fromEntries(
        Object.entries(messages)
          .filter(([key]) => key.startsWith('startupUpdate.'))
          .map(([key, value]) => [key.slice('startupUpdate.'.length), value]),
      );
    }
    await fs.writeFile(
      path.join(__dirname, 'shell/update/strings.json'),
      JSON.stringify(updateStrings, null, 2) + '\n',
    );

    execSync('node scripts/shellAbi.mjs --write', { stdio: 'inherit', cwd: __dirname });
    const { shellAbi } = JSON.parse(
      await fs.readFile(path.join(__dirname, 'shell/abi.json'), 'utf8'),
    );
    const corePlatform =
      context.electronPlatformName === 'mas' ? 'darwin' : context.electronPlatformName;
    execSync('node scripts/assembleCore.mjs', { stdio: 'inherit', cwd: __dirname });
    execSync(
      `node scripts/buildCoreManifest.mjs --core=core-dist --platform=${corePlatform} --channel=${channel || 'stable'} --version=${packageJSON.version} --seq=${process.env.CORE_SEQ || 0} --shell-abi=${shellAbi}`,
      { stdio: 'inherit', cwd: __dirname },
    );
    await packBuiltinCore(__dirname);
  },
  /**
   * AfterPack hook for copying Liquid Glass Assets.car on macOS 26+.
   *
   * @see https://github.com/electron-userland/electron-builder/issues/9254
   * @see https://github.com/MultiboxLabs/flow-browser/pull/159
   */
  afterPack: async (context) => {
    const isMac = ['darwin', 'mas'].includes(context.electronPlatformName);

    if (!isMac) {
      return;
    }

    const appPath = path.join(context.appOutDir, `${context.packager.appInfo.productFilename}.app`);
    const resourcesPath = path.join(appPath, 'Contents', 'Resources');

    // The Mac App Store sandbox cannot register the helper's LaunchAgent.
    if (context.electronPlatformName === 'darwin') {
      await embedComputerUseHelper({
        appPath,
        arch: arch === 'arm64' ? 'arm64' : 'x64',
        cacheDir: path.join(__dirname, 'node_modules', '.cache', 'lobehub-computer-use'),
        version: computerUseHelperVersion,
      });
    }

    const iconFileName = getIconFileName();
    const assetsCarSource = path.join(__dirname, 'build', `${iconFileName}.Assets.car`);
    const assetsCarDest = path.join(resourcesPath, 'Assets.car');

    try {
      await fs.access(assetsCarSource);
      await fs.copyFile(assetsCarSource, assetsCarDest);
      console.info(`✅ Copied Liquid Glass icon: ${iconFileName}.Assets.car`);
    } catch {
      // Non-critical: Assets.car not found or copy failed
      // App will use fallback .icns icon on all macOS versions
      console.info(`⏭️  Skipping Assets.car (not found or copy failed)`);
    }
  },
  afterSign: async (context) => {
    await verifyFontListSignature(context);

    if (context.electronPlatformName === 'darwin') {
      verifyComputerUseHelper(
        path.join(
          context.appOutDir,
          `${context.packager.appInfo.productFilename}.app`,
          COMPUTER_USE_HELPER_BUNDLE_PATH,
        ),
        { version: computerUseHelperVersion },
      );
      console.info('✅ Computer Use helper kept its notarized signature');

      if (hasAppleCertificate) {
        await notarizeMacApp(
          path.join(context.appOutDir, `${context.packager.appInfo.productFilename}.app`),
        );
      }
    }
  },
  appId,
  appImage: {
    artifactName: '${productName}-${version}.${ext}',
  },

  // Only explicitly selected native binaries should live outside app.asar.
  asar: {
    smartUnpack: false,
  },

  // Native modules must be unpacked from asar to work correctly
  asarUnpack: getAsarUnpackPatterns(),

  ...(useSparkle ? { buildVersion: toSparkleBuildVersion(packageJSON.version) } : {}),

  detectUpdateChannel: true,

  directories: {
    buildResources: 'build',
    output: 'release',
  },

  dmg: {
    artifactName: '${productName}-${version}-${arch}.${ext}',
    background: 'resources/dmg.png',
    format: 'UDBZ',
    contents: [
      { type: 'file', x: 150, y: 240 },
      { type: 'link', path: '/Applications', x: 450, y: 240 },
    ],
    iconSize: 80,
    window: {
      height: 400,
      width: 600,
    },
  },

  electronDownload: {
    mirror: 'https://npmmirror.com/mirrors/electron/',
  },
  // Electron uses underscores for macOS .lproj directories and hyphens for
  // Windows/Linux locale packs. Keep the English variants on every platform.
  electronLanguages: ['en', 'en_GB', 'en_US', 'en-GB', 'en-US'],

  files: [
    'shell/**',
    '!shell/__tests__',
    ...(useSparkle ? ['!shell/rescue/electron-updater.cjs'] : []),
    'package.json',
    // Exclude all node_modules first
    '!node_modules',
    // Then explicitly include native modules using object form (handles pnpm symlinks)
    ...getNativeModulesFilesConfig(),
    // electron-log ships in the core (assembleCore), a shell copy would shadow it via the resolver shim
    ...getModuleFilesConfig(['font-list']),
  ],
  generateUpdatesFilesForAllChannels: true,
  linux: {
    category: 'Utility',
    icon: 'build/icon.png',
    maintainer: 'electronjs.org',
    target: ['AppImage', 'snap', 'deb', 'rpm', 'tar.gz'],
  },
  mac: {
    binaries: [
      'Contents/Resources/app.asar.unpacked/node_modules/font-list/libs/darwin/fontlist',
      'Contents/Resources/bin/auv',
    ],
    // 'maximum' zips with 15-pass deflate: 7.7x slower for 0.2% smaller, up to 20 min on Intel CI. dmg.format keeps UDBZ separately.
    compression: 'normal',
    entitlementsInherit: 'build/entitlements.mac.plist',
    ...(macCommunicationEntitlements
      ? {
          entitlements: macCommunicationEntitlements,
          provisioningProfile: macProvisioningProfile,
        }
      : {}),
    extendInfo: {
      ...(useSparkle
        ? {
            SUDeltaChainHistory: 6,
            SUEnableAutomaticChecks: false,
            SUEnableInstallerLauncherService: false,
            SUFeedURL: sparkleFeedUrl,
            SUPublicEDKey: sparklePublicKey,
          }
        : {}),
      CFBundleIconName: 'AppIcon',
      /**
       * Claiming `public.folder` is what puts the app in Finder's "Open With"
       * for a folder. Without it the only ways in are the shell command and a
       * drop on the Dock icon — which is what a first-time user is least
       * likely to guess.
       */
      CFBundleDocumentTypes: [
        {
          CFBundleTypeName: 'Folder',
          CFBundleTypeRole: 'Viewer',
          LSHandlerRank: 'Alternate',
          LSItemContentTypes: ['public.folder'],
        },
      ],
      CFBundleURLTypes: [
        {
          CFBundleURLName: `${brandName} Protocol`,
          CFBundleURLSchemes: [protocolScheme],
        },
      ],
      NSAppleEventsUsageDescription:
        'Application needs to control System Settings to help you grant Full Disk Access automatically.',
      NSCameraUsageDescription: "Application requests access to the device's camera.",
      NSDocumentsFolderUsageDescription:
        "Application requests access to the user's Documents folder.",
      NSDownloadsFolderUsageDescription:
        "Application requests access to the user's Downloads folder.",
      NSLocalNetworkUsageDescription: `${brandName} connects to self-hosted servers, model providers, and other services on your local network.`,
      NSMicrophoneUsageDescription: "Application requests access to the device's microphone.",
      NSScreenCaptureUsageDescription:
        'Application requests access to record and analyze screen content for AI assistance.',
      NSUserActivityTypes: ['INSendMessageIntent'],
    },
    gatekeeperAssess: false,
    hardenedRuntime: hasAppleCertificate,
    // Ship the notarized helper as built; re-signing drops its stapled ticket.
    signIgnore: [COMPUTER_USE_HELPER_SIGN_IGNORE],
    // Notarized in afterSign so a transient staple failure retries instead of failing the build.
    notarize: false,
    ...(hasAppleCertificate ? {} : { identity: null }),
    target: [
      { arch: [arch === 'arm64' ? 'arm64' : 'x64'], target: 'dmg' },
      { arch: [arch === 'arm64' ? 'arm64' : 'x64'], target: 'zip' },
    ],
  },
  npmRebuild: true,
  nsis: {
    allowToChangeInstallationDirectory: true,
    artifactName: '${productName}-${version}-setup.${ext}',
    createDesktopShortcut: 'always',
    installerHeader: './build/nsis-header.bmp',
    installerSidebar: './build/nsis-sidebar.bmp',
    oneClick: false,
    shortcutName: '${productName}',
    uninstallDisplayName: '${productName}',
    uninstallerSidebar: './build/nsis-sidebar.bmp',
  },
  protocols: [
    {
      name: `${brandName} Protocol`,
      schemes: [protocolScheme],
    },
  ],
  publish: getPublishConfig(),

  // Release notes 配置
  // 可以通过环境变量 RELEASE_NOTES 传入,或从文件读取
  // 这会被写入 latest-mac.yml / latest.yml 中,供 generic provider 使用
  releaseInfo: {
    releaseNotes: process.env.RELEASE_NOTES || undefined,
  },

  ...(sparklePackageDir
    ? {
        extraFiles: [
          {
            from: path.join(sparklePackageDir, 'native/vendor/Sparkle.framework'),
            to: 'Frameworks/Sparkle.framework',
          },
        ],
      }
    : {}),
  extraResources: [
    { from: 'resources/bin', to: 'bin' },
    { from: 'core.asar', to: 'core.asar' },
    { from: 'core.asar.unpacked', to: 'core.asar.unpacked' },
    // The Sparkle bridge addon is loaded by an explicit path outside app.asar; pnpm's
    // symlinked package dir cannot be matched by asarUnpack, so it ships as a resource.
    ...(sparklePackageDir
      ? [
          {
            from: path.join(sparklePackageDir, 'native/build/Release/sparkle_bridge.node'),
            to: 'sparkle/sparkle_bridge.node',
          },
        ]
      : []),
    // Local Sandbox helper binaries. The sandbox spawns these by path, so they
    // must be real files — not entries inside app.asar, and not something the
    // user is expected to install separately.
    //
    // Shipped as a resource rather than by externalizing
    // `@anthropic-ai/sandbox-runtime`: its JavaScript bundles into the main
    // process perfectly well, and making it a production dependency instead
    // drags four transitive packages into electron-builder's node_modules
    // traversal, which pnpm's layout does not satisfy (`@pondwader/socks5-server
    // not found`). Only the binaries need to exist on disk.
    {
      from: 'node_modules/@anthropic-ai/sandbox-runtime/vendor',
      to: 'sandbox-runtime/vendor',
    },
    // Carried alongside the binaries so the staging directory stays keyed on the
    // backend's real version. Without it the version lookup falls back to the
    // binary's size — which still works, but would defeat the per-version
    // isolation in exactly the case it exists for: an installed app being
    // updated.
    {
      from: 'node_modules/@anthropic-ai/sandbox-runtime/package.json',
      to: 'sandbox-runtime/package.json',
    },
  ],

  win: {
    executableName,
  },
};

export default config;