File size: 1,773 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

const configPath = path.join(__dirname, '../config/config.ts');
const backupPath = path.join(__dirname, '../config/config.ts.backup');

// 备份原配置
fs.copyFileSync(configPath, backupPath);

try {
  // 读取配置文件
  let config = fs.readFileSync(configPath, 'utf8');

  // 修改配置
  config = config.replace(/publicPath: ['"].*['"],/, "publicPath: '/hooks/',");
  config = config.replace(
    /{ rel: 'stylesheet', href: '\/style\.css' }/,
    "{ rel: 'stylesheet', href: '/hooks/style.css' }",
  );
  config = config.replace(/logo: '\/logo\.svg',/, "logo: '/hooks/logo.svg',");

  // 写入修改后的配置
  fs.writeFileSync(configPath, config);

  // 运行构建命令
  execSync('pnpm run build:doc', { stdio: 'inherit' });

  // 进入 dist 目录
  process.chdir(path.join(__dirname, '../dist'));

  // 初始化 git 仓库(如果不存在)
  try {
    execSync('git init', { stdio: 'inherit' });
  } catch (e) {
    // 如果已经初始化过,忽略错误
  }

  // 添加所有文件
  execSync('git add .', { stdio: 'inherit' });

  // 提交更改
  execSync('git commit -m "chore: update gh-pages"', { stdio: 'inherit' });

  // 添加远程仓库(如果不存在)
  try {
    execSync('git remote add origin git@github.com:alibaba/hooks.git', { stdio: 'inherit' });
  } catch (e) {
    // 如果远程仓库已存在,忽略错误
  }

  // 强制推送到 gh-pages 分支
  execSync('git push -f origin HEAD:gh-pages', { stdio: 'inherit' });

  // 返回到项目根目录
  process.chdir(path.join(__dirname, '..'));
} finally {
  // 恢复原配置
  fs.copyFileSync(backupPath, configPath);
  fs.unlinkSync(backupPath);
}