()
| 8 | } |
| 9 | |
| 10 | async execute() { |
| 11 | try { |
| 12 | const config = this.configManager.getConfig(); |
| 13 | |
| 14 | // 检查必要的配置 |
| 15 | if (!config.deploy || !config.deploy.github) { |
| 16 | throw new Error('🤯 在 pageforge.yaml 中未找到 GitHub 部署配置'); |
| 17 | } |
| 18 | |
| 19 | const { |
| 20 | repository, |
| 21 | branch = 'gh-pages', |
| 22 | cname, |
| 23 | buildDir = 'dist', |
| 24 | version |
| 25 | } = config.deploy.github; |
| 26 | |
| 27 | if (!repository) { |
| 28 | throw new Error('🤯 配置中需要 GitHub 存储库 URL'); |
| 29 | } |
| 30 | |
| 31 | // 确保构建目录存在 |
| 32 | const buildPath = path.resolve(process.cwd(), buildDir); |
| 33 | if (!fs.existsSync(buildPath)) { |
| 34 | throw new Error(`🤯 找不到生成目录 '${buildDir}'。请先运行 'pageforge build'。`); |
| 35 | } |
| 36 | |
| 37 | // 如果指定了版本,检查之前的版本是否存在 |
| 38 | if (version) { |
| 39 | // 克隆已有的 gh-pages 分支 |
| 40 | const tempDir = path.join(process.cwd(), '.temp-gh-pages'); |
| 41 | if (fs.existsSync(tempDir)) { |
| 42 | fs.rmSync(tempDir, { recursive: true }); |
| 43 | } |
| 44 | |
| 45 | try { |
| 46 | this.execCommand('git', ['clone', '--branch', branch, '--single-branch', repository, tempDir]); |
| 47 | |
| 48 | // 如果存在旧版本目录,将其复制到新的构建目录 |
| 49 | const versionsDir = path.join(tempDir); |
| 50 | if (fs.existsSync(versionsDir)) { |
| 51 | // 复制所有其他版本的文件 |
| 52 | fs.readdirSync(versionsDir).forEach(dir => { |
| 53 | if (dir !== version && !dir.startsWith('.')) { |
| 54 | fs.cpSync(path.join(versionsDir, dir), path.join(buildPath, dir), { recursive: true }); |
| 55 | } |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | // 清理临时目录 |
| 60 | fs.rmSync(tempDir, { recursive: true }); |
| 61 | } catch (error) { |
| 62 | console.log('📝 首次部署或获取已有版本失败,继续部署当前版本'); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // 初始化临时 git 仓库 |
| 67 | this.execCommand('git', ['init'], {cwd: buildPath}); |
no test coverage detected