| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- const { execSync } = require('child_process');
- const fs = require('fs');
- const path = require('path');
- function isCI(){
- const isCI = process.env.CI === 'true' || !!process.env.GITHUB_ACTIONS;
- return isCI;
- }
- function getGhToken() {
- // 1. 在 GitHub Actions 里官方会注入 ACTIONS_RUNTIME_TOKEN 或 CI=true
- const isCI = process.env.CI === 'true' || !!process.env.GITHUB_ACTIONS;
- if (isCI) {
- console.log(`you are running in ic and the GH_token is ${process.env.GH_TOKEN}`)
- // CI 环境:直接拿 secret(工作流里必须 env:{GH_TOKEN:${{secrets.GH_TOKEN}}})
- const token = process.env.GH_TOKEN;
- if (!token) throw new Error('CI 环境缺少 GH_TOKEN,请检查 workflow yaml');
- return token;
- }
- // 2. 本地开发:优先读本地 ~/.gh-token(自己放的),没有再读环境变量,还没有就空
- try {
- return readFileSync(join(require('os').homedir(), '.gh-token'), 'utf8').trim();
- } catch {
- return process.env.GH_TOKEN || '';
- }
- }
- const rootDir = path.join(__dirname, '..'); // 项目根目录
- execSync(`npm run build:h5`, { cwd: rootDir, stdio: 'inherit', env: { ...process.env} }, (error, stdout, stderr) => {
- if (error) {
- console.error(`Error executing command: ${error.message}`);
- return;
- }
- if (stderr) {
- console.error(`Command stderr: ${stderr}`);
- return;
- }
- console.log(`Command stdout: ${stdout}`);
- });
- try {
- const GH_TOKEN = getGhToken(); // CI/本地 都能拿到合适值
- const cmd = isCI()
- ? 'npx electron-builder --config electron-builder.json --win --publish always'
- : 'npx electron-builder --config electron-builder.json --win ';
- execSync(cmd, {
- stdio: 'inherit',
- cwd: process.cwd(),
- env: { ...process.env, GH_TOKEN } // 关键:仅追加 token,其他不动
- });
-
- console.log('\n✅ 全部构建完成');
- } catch (err) {
- console.error('Failed to execute :', err.message);
- process.exit(1);
- }
|