h5_for_electron.build.linux.arm.js 3.1 KB

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