-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmirror.mjs
executable file
·73 lines (63 loc) · 1.56 KB
/
mirror.mjs
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 zx
// import "zx/globals";
// /////////////Func/////////////////
function parseArgs() {
const res = argv;
delete res['_'];
if (res.target) {
res.target = path.resolve(res.target);
} else {
res.target = path.resolve('.');
}
if (res.config) {
res.config = path.resolve(res.config);
} else {
res.config = path.resolve('./.github_backup_config.json');
}
return res;
}
async function createGiteeRepoIfNotExist(name, user, token, isPrivate) {
let response = await fetch(
`https://gitee.com/api/v5/repos/${user}/${name}?access_token=${token}`,
{
method: 'GET',
},
);
if (response.ok) {
return await response.json();
}
response = await fetch(
`https://gitee.com/api/v5/user/repos?name=${name}&access_token=${token}&private=${isPrivate}`,
{
method: 'POST',
},
);
return await response.json();
}
// /////////////Main/////////////////
const args = parseArgs();
console.log(args);
if (!args.token) {
throw new Error('Missing token');
}
const config = await fs.readFile(args.config);
for (const name of Object.keys(config.repos)) {
cd(args.target);
const repo = config.repos[name];
if (repo.ignore) {
continue;
}
const repoPath = path.resolve(`./${repo.name}`);
const mRepo = await createGiteeRepoIfNotExist(
name,
config.username,
token,
args.private === 'always' ? true : repo.status.private,
);
if (fs.existsSync(repoPath)) {
cd(repoPath);
if (mRepo.ssh_url) {
await $`git push --mirror ${mRepo.ssh_url}`;
}
}
}