-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-mirrors.mjs
More file actions
73 lines (66 loc) · 2.48 KB
/
Copy pathsync-mirrors.mjs
File metadata and controls
73 lines (66 loc) · 2.48 KB
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
67
68
69
70
71
72
73
#!/usr/bin/env node
/**
* sync-mirrors.mjs — per-mirror self-referencing install sources.
*
* Before pushing to each remote, rewrites the GitHub org inside the
* install-source URLs of the docs (README*.md / INSTALL*.md) to that remote's
* own organization, so every mirror's documentation points at itself:
* origin -> dsh-external, public -> lhh010, omdsh -> omdsh-dev.
* The working tree keeps lhh010 (the public default) after the run.
*
* Usage: node sync-mirrors.mjs (rewrite, commit, push each mirror)
* node sync-mirrors.mjs --no-push (rewrite + commit only)
*/
import { execFileSync } from 'node:child_process'
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
const REMOTES = [
['origin', 'dsh-external'],
['public', 'lhh010'],
['omdsh', 'omdsh-dev'],
]
const DOCS = ['README.md', 'README.en.md', 'INSTALL.md', 'INSTALL.en.md']
const ORGS = ['dsh-external', 'lhh010', 'omdsh-dev']
const pkg = JSON.parse(readFileSync('package.json', 'utf8'))
const REPO = pkg.name.split('/').pop() ?? pkg.name
const noPush = process.argv.includes('--no-push')
const git = (...args) => execFileSync('git', args, { stdio: ['ignore', 'inherit', 'inherit'] })
const gitOut = (...args) => execFileSync('git', args, { encoding: 'utf8' })
/** Rewrite install-source orgs in every doc to the target org. */
function rewrite(targetOrg) {
let changed = false
for (const doc of DOCS) {
if (!existsSync(doc)) continue
const text = readFileSync(doc, 'utf8')
let next = text
for (const org of ORGS) {
if (org === targetOrg) continue
next = next.replaceAll(`github.com/${org}/${REPO}`, `github.com/${targetOrg}/${REPO}`)
next = next.replaceAll(`github:${org}/${REPO}`, `github:${targetOrg}/${REPO}`)
}
if (next !== text) {
writeFileSync(doc, next)
changed = true
}
}
return changed
}
if (gitOut('status', '--porcelain').trim() !== '') {
git('add', '-A')
git('commit', '-m', 'docs: 保存待同步改动(sync-mirrors)')
}
for (const [remote, org] of REMOTES) {
if (rewrite(org)) {
git('add', '-A')
git('commit', '-m', `docs(sync): 安装源指向本镜像组织 ${org}`)
}
if (!noPush) {
git('fetch', remote)
git('push', '--force-with-lease', remote, 'HEAD:main')
}
}
// Restore the canonical public-default org in the working tree.
if (rewrite('lhh010')) {
git('add', '-A')
git('commit', '-m', 'docs(sync): 规范安装源恢复为 lhh010(公开默认)')
}
console.log('sync-mirrors: done')