|
| 1 | +const fs = require("fs"); |
| 2 | +const path = require("path"); |
| 3 | + |
| 4 | +const rootDir = path.join(__dirname, ".."); |
| 5 | +const pkgPath = path.join(rootDir, "package.json"); |
| 6 | +const pkg = JSON.parse(fs.readFileSync(pkgPath)); |
| 7 | + |
| 8 | +const githubSite = "https://raw.githubusercontent.com"; |
| 9 | +const giteaRepo = "go-gitea/gitea"; |
| 10 | +const githubTagPath = "refs/tags"; |
| 11 | +const tmplPath = "templates"; |
| 12 | +const localTmplPath = path.join(rootDir, "gitea", tmplPath); |
| 13 | +const [major, minor, patch = ""] = pkg.version.split("."); |
| 14 | +console.log("Version:", pkg.version); |
| 15 | +const versionPath = `${githubTagPath}/v${major}.${minor}.${patch}`; |
| 16 | +const githubUrl = `${githubSite}/${giteaRepo}/${versionPath}/${tmplPath}`; |
| 17 | + |
| 18 | +// 递归读取所有子目录中的 .tmpl 文件 |
| 19 | +function readTmplFilesRecursively(dir) { |
| 20 | + const results = []; |
| 21 | + const files = fs.readdirSync(dir); |
| 22 | + |
| 23 | + for (const file of files) { |
| 24 | + const filePath = path.join(dir, file); |
| 25 | + const stat = fs.statSync(filePath); |
| 26 | + |
| 27 | + if (stat.isDirectory()) { |
| 28 | + // 如果是目录,递归读取 |
| 29 | + results.push(...readTmplFilesRecursively(filePath)); |
| 30 | + } else if (file.endsWith(".tmpl")) { |
| 31 | + // 如果是 .tmpl 文件,添加到结果中(相对于模板目录的相对路径) |
| 32 | + const relativePath = path.relative(localTmplPath, filePath); |
| 33 | + results.push(relativePath); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return results; |
| 38 | +} |
| 39 | + |
| 40 | +// 读取所有模板文件 |
| 41 | +const tmpls = readTmplFilesRecursively(localTmplPath); |
| 42 | +(async () => { |
| 43 | + for (const tmpl of tmpls) { |
| 44 | + const tmplUrl = `${githubUrl}/${tmpl}`; |
| 45 | + console.log("TmplUrl:", tmplUrl); |
| 46 | + |
| 47 | + const response = await fetch(tmplUrl); |
| 48 | + if (!response.ok) { |
| 49 | + throw new Error(`HTTP error! status: ${response.status}`); |
| 50 | + } |
| 51 | + let content = await response.text(); |
| 52 | + fs.writeFileSync(path.join(localTmplPath, tmpl), content); |
| 53 | + } |
| 54 | +})(); |
0 commit comments