h5_for_electron.build.linux.arm.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const { execSync } = require('child_process');
  2. const fs = require('fs');
  3. const path = require('path');
  4. function isCI() {
  5. const isCI = process.env.CI === 'true' || !!process.env.GITHUB_ACTIONS;
  6. return isCI;
  7. }
  8. function getGhToken() {
  9. // 1. 在 GitHub Actions 里官方会注入 ACTIONS_RUNTIME_TOKEN 或 CI=true
  10. const isCI = process.env.CI === 'true' || !!process.env.GITHUB_ACTIONS;
  11. if (isCI) {
  12. console.log(`you are running in ic and the GH_token is ${process.env.GH_TOKEN}`)
  13. // CI 环境:直接拿 secret(工作流里必须 env:{GH_TOKEN:${{secrets.GH_TOKEN}}})
  14. const token = process.env.GH_TOKEN;
  15. if (!token) throw new Error('CI 环境缺少 GH_TOKEN,请检查 workflow yaml');
  16. return token;
  17. }
  18. // 2. 本地开发:优先读本地 ~/.gh-token(自己放的),没有再读环境变量,还没有就空
  19. try {
  20. return readFileSync(join(require('os').homedir(), '.gh-token'), 'utf8').trim();
  21. } catch {
  22. return process.env.GH_TOKEN || '';
  23. }
  24. }
  25. const rootDir = path.join(__dirname, '..'); // 项目根目录
  26. try {
  27. console.log(`复制arm平台必须构建文件taro.linux-arm64-gnu.node到目标位置`);
  28. fs.copyFileSync(
  29. path.join(rootDir, '.build', 'taro.linux-arm64-gnu.node'),
  30. path.join(rootDir, 'node_modules', '@tarojs', 'binding', 'taro.linux-arm64-gnu.node')
  31. );
  32. console.log(`复制arm平台必须构建文件taro.linux-arm64-gnu.node到目标位置===完成`);
  33. } catch (err) {
  34. console.error('复制arm平台构建所需文件时失败 :', err.message);
  35. process.exit(1);
  36. }
  37. execSync(`npm run build:h5`, { cwd: rootDir, stdio: 'inherit', env: { ...process.env } }, (error, stdout, stderr) => {
  38. if (error) {
  39. console.error(`Error executing command: ${error.message}`);
  40. return;
  41. }
  42. if (stderr) {
  43. console.error(`Command stderr: ${stderr}`);
  44. return;
  45. }
  46. console.log(`Command stdout: ${stdout}`);
  47. });
  48. try {
  49. const GH_TOKEN = getGhToken(); // CI/本地 都能拿到合适值
  50. //ci 环境才使用 publish
  51. const cmd = isCI()
  52. ? 'npx electron-builder --config electron-builder.json --linux --publish always'
  53. : 'npm run build:electron:linux';
  54. execSync(cmd, {
  55. stdio: 'inherit',
  56. cwd: process.cwd(),
  57. env: { ...process.env, GH_TOKEN } // 关键:仅追加 token,其他不动
  58. });
  59. console.log('\n✅ 全部构建完成');
  60. } catch (err) {
  61. console.error('Failed to execute :', err.message);
  62. process.exit(1);
  63. }