after-pack.cjs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. const { execFileSync } = require('node:child_process');
  3. const fs = require('node:fs');
  4. const path = require('node:path');
  5. /**
  6. * electron-builder afterPack hook.
  7. *
  8. * - Ensure the bundled luatos-cli has the executable bit.
  9. * - On macOS, ad-hoc sign (or sign with CSC_NAME) the sidecar binary. The app
  10. * bundle itself is handled by electron-builder; if no Developer ID is
  11. * configured, macOS users can still run the app after clearing quarantine.
  12. */
  13. module.exports = async function afterPack(context) {
  14. const productName = context.packager?.appInfo?.productFilename || 'LuatOS Flasher';
  15. const isMac = process.platform === 'darwin';
  16. const appDir = path.join(context.appOutDir, `${productName}.app`);
  17. const binDir = isMac
  18. ? path.join(appDir, 'Contents', 'Resources', 'bin')
  19. : path.join(context.appOutDir, 'resources', 'bin');
  20. const cli = path.join(binDir, isMac ? 'luatos-cli' : 'luatos-cli.exe');
  21. if (!fs.existsSync(cli)) {
  22. console.warn(`[afterPack] 未找到 ${cli}`);
  23. return;
  24. }
  25. if (!isMac) {
  26. console.log(`[afterPack] 已打包烧录内核:${cli}`);
  27. return;
  28. }
  29. try {
  30. fs.chmodSync(cli, 0o755);
  31. } catch (error) {
  32. console.warn(`[afterPack] chmod 失败:${error.message}`);
  33. }
  34. const identity = process.env.CSC_NAME || '-';
  35. try {
  36. execFileSync('codesign', ['--force', '--sign', identity, cli], { stdio: 'inherit' });
  37. console.log(`[afterPack] 已签名 ${cli}(identity=${identity})`);
  38. } catch (error) {
  39. console.warn(`[afterPack] codesign 失败(可忽略,稍后可手动签):${error.message}`);
  40. }
  41. };