h5_for_electron.build.linux.arm.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. // CI 环境:直接拿 secret(工作流里必须 env:{GH_TOKEN:${{secrets.GH_TOKEN}}})
  10. const token = process.env.GH_TOKEN;
  11. if (!token) throw new Error('CI 环境缺少 GH_TOKEN,请检查 workflow yaml');
  12. return token;
  13. }
  14. // 2. 本地开发:优先读本地 ~/.gh-token(自己放的),没有再读环境变量,还没有就空
  15. try {
  16. return readFileSync(join(require('os').homedir(), '.gh-token'), 'utf8').trim();
  17. } catch {
  18. return process.env.GH_TOKEN || '';
  19. }
  20. }
  21. const TARO_API_URL = 'http://localhost:6001'; // 远程地址,这里写死,要做成部署后可配置
  22. const TARO_MQTT_URL ='ws://localhost:8083/mqtt';
  23. const rootDir = path.join(__dirname, '..'); // 项目根目录
  24. execSync(`npm run build:h5`, { cwd: rootDir, stdio: 'inherit', env: { ...process.env, TARO_API_URL ,TARO_MQTT_URL} }, (error, stdout, stderr) => {
  25. if (error) {
  26. console.error(`Error executing command: ${error.message}`);
  27. return;
  28. }
  29. if (stderr) {
  30. console.error(`Command stderr: ${stderr}`);
  31. return;
  32. }
  33. console.log(`Command stdout: ${stdout}`);
  34. });
  35. function run(cmd,env={}) {
  36. console.log(`\n>>> ${cmd}`);
  37. try {
  38. execSync(cmd, { stdio: 'inherit', cwd: process.cwd() ,env:env});
  39. } catch (e) {
  40. console.error(`命令失败: ${cmd}`, e.message);
  41. exit(1);
  42. }
  43. }
  44. try {
  45. // run('npm run build:h5');
  46. // run('npm run prebuild:arm:linux');
  47. fs.copyFileSync(
  48. path.join(rootDir, '.build', 'taro.linux-arm64-gnu.node'),
  49. path.join(rootDir, 'node_modules', '@tarojs', 'binding', 'taro.linux-arm64-gnu.node')
  50. );
  51. const GH_TOKEN = getGhToken(); // CI/本地 都能拿到合适值
  52. // run('npm run build:electron:win');
  53. const cmd = '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. }