-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocess_repos.js
107 lines (94 loc) · 3.13 KB
/
process_repos.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const fs = require('fs')
const chp = require('child_process')
const path = require('path')
const moment = require('moment')
const nbp = require('npm-book-publisher')
const cheerio = require('cheerio')
const {
defaultHdrs,
requestRetry,
getGhFile,
listGhFiles,
getGhRepos,
} = require('./gh_util.js')
const request = requestRetry
// 添加 Gitee Cookie
const GT_COOKIE = process.env['GT_COOKIE']
function giteeDeployPages(name, repo) {
var hdrs = Object.assign({}, defaultHdrs)
hdrs['Cookie'] = GT_COOKIE
var html = request('GET', 'https://gitee.com/',
{headers: hdrs}).body.toString()
var $ = cheerio.load(html)
var token = $('meta[name=csrf-token]').attr('content')
if (!token) return false
hdrs['X-CSRF-Token'] = token
var data = 'branch=&build_directory=&force_https=true&auto_update=true'
url = `https://gitee.com/${name}/${repo}/pages`
var r = request('POST', url, {headers: hdrs, body: data})
return r.statusCode == 200
}
function getDesc(md) {
var rm = /^#+ (.+?)$/m.exec(md)
return rm? rm[1]: ''
}
function dockerHubExist(name, repo) {
var url = `https://hub.docker.com/v2/repositories/${name}/${repo}/`
var r = request('GET', url, {headers: defaultHdrs})
return r.statusCode != 404
}
function filterDockerRepoName(name) {
return name.toLowerCase()
.replace(/[^\w\-]/g, '-')
.split('-')
.filter(x => x)
.join('-')
}
const extras = [
['apachecn', 'hands-on-ml-2e-zh'],
['apachecn', 'principles-zh'],
['it-ebooks-0', 'data8-textbook-zh'],
['it-ebooks-0', 'prob140-textbook-zh'],
]
function main() {
var dir = process.argv[2]
var docs = getGhRepos('apachecn')
.map(x => ['apachecn', x])
.concat(extras)
.sort((a, b) => a[1].localeCompare(b[1]))
console.log(`仓库读取完毕`)
for(var [un, repo] of docs) {
console.log(un + '/' + repo)
// 在这里填写过滤条件
var rootFiles = listGhFiles(un, repo)
if(!rootFiles.includes('index.html') ||
!rootFiles.includes('README.md') ||
!rootFiles.includes('SUMMARY.md'))
continue
// -----------------------
var docDir = path.join(dir, repo)
if(!fs.existsSync(docDir)) {
process.chdir(dir)
chp.spawnSync('git',
['clone', `https://gitclone.com/github.com/${un}/${repo}`, '-b', 'master'],
{stdio: 'inherit'})
if(!fs.existsSync(docDir)) continue
process.chdir(repo)
chp.spawnSync('git',
['remote', 'set-url', 'origin', `https://github.com/${un}/${repo}`],
{stdio: 'inherit'})
}
process.chdir(docDir)
chp.spawnSync('git',
['pull', 'origin', 'master'],
{stdio: 'inherit'})
// 在这里填写操作
// -----------------------
chp.spawnSync('git', ['add', '-A'], {stdio: 'inherit'})
chp.spawnSync('git',
['commit', '-am', moment().format('YYYY-MM-DD HH:mm:ss')],
{stdio: 'inherit'})
chp.spawnSync('git', ['push'], {stdio: 'inherit'})
}
}
if(require.main === module) main()