-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyncRepositories.ts
executable file
·66 lines (53 loc) · 2.09 KB
/
syncRepositories.ts
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
63
64
65
66
import * as fs from 'fs'
import * as path from 'path'
import { execSync } from 'child_process'
const SYNC_DIR = 'sync'
const DOCS_DIR = 'docs'
function padNumber(num: number): string {
return num.toString().padStart(3, '0')
}
function createSymlink(sourcePath: string, index: number, repoName: string, subdir: string, targetSubdir: string = subdir, prefixTargetSubPath: boolean = true): void {
const sourceSubPath = path.join(sourcePath, 'docs', subdir)
const targetName = prefixTargetSubPath ? `${padNumber(index)}-${repoName}` : repoName
const targetDir = path.join(DOCS_DIR, targetSubdir);
const targetSubPath = path.join(targetDir, targetName)
if (fs.existsSync(sourceSubPath)) {
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, { recursive: true })
}
if (fs.existsSync(targetSubPath)) {
fs.unlinkSync(targetSubPath)
}
fs.symlinkSync(path.resolve(sourceSubPath), path.resolve(targetSubPath), 'dir')
}
}
function syncRepositories(): void {
// Create necessary directories
if (!fs.existsSync(SYNC_DIR)) {
fs.mkdirSync(SYNC_DIR, { recursive: true })
}
if (!fs.existsSync(DOCS_DIR)) {
fs.mkdirSync(DOCS_DIR, { recursive: true })
}
// Read repositories.json
const urls: string[] = JSON.parse(
fs.readFileSync('repositories.json', 'utf-8')
)
console.log(urls);
urls.forEach((url, index) => {
const repoName = url.split('/').pop()?.replace('.git', '') || ''
const repoPath = path.join(SYNC_DIR, repoName)
// Clone or update repository
if (!fs.existsSync(repoPath)) {
console.log(`Cloning ${repoName}...`)
execSync(`git clone ${url} ${repoPath}`)
} else {
console.log(`Updating ${repoName}...`)
execSync('git pull', { cwd: repoPath })
}
createSymlink(repoPath, index, repoName, 'api')
createSymlink(repoPath, index, repoName, 'product', 'products')
createSymlink(repoPath, index, repoName, 'static', 'static', false)
})
}
syncRepositories();