h5_for_electron.build.linux.arm.js 2.5 KB

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