-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathinitFromGit.js
109 lines (91 loc) · 2.72 KB
/
initFromGit.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
108
109
/**
* 初始化代码
* 从 git 到 手机,不经过PC
*/
main()
/**
* 入口
*/
function main() {
toastLog('---------- start ----------')
// step1: 获取git代码
var codeList = httpGet()
// step2: 写入手机端,会强行覆盖文件
var isOk = overWriteFile(codeList)
if (!isOk) {
toastLog('shit happen')
}
toastLog('---------- end ----------')
}
/**
* 获取文件
* @returns {array} 代码内容列表
*/
function httpGet() {
var returnList = []
var masterUrl = 'https://gitee.com/api/v5/repos/lennon7c7/autojs/git/trees/master'
var masterContent = http.get(masterUrl).body.json()
if (!masterContent) {
toastLog(masterUrl + ': !masterContent')
return
}
var rawUrlPrefix = 'https://gitee.com/lennon7c7/autojs/raw/master/'
var ignoreFile = ['apk', '.gitignore', 'README.md', 'action-all.js', 'action-every-20-min.js', 'action-temp.js', 'test-click.js', 'test-exists.js', 'test-swipe.js']
masterContent.tree.forEach((value1) => {
// 有些文件真的需要过滤
if (ignoreFile.indexOf(value1.path) !== -1) {
return
}
if (value1.type === 'tree') {
// 文件夹目录
var treeContent = http.get(value1.url).body.json()
if (!treeContent) {
toastLog(value1.url + ': !treeContent')
return
}
treeContent.tree.forEach((value2) => {
var path = value1.path + '/' + value2.path
var url = rawUrlPrefix + path
var content = http.get(url).body.string()
if (!content) {
toastLog(value2.path + ': !content')
return
}
returnList.push({
'path': path,
'content': content,
})
toastLog('http.get: ' + url)
})
} else {
// 文件
var url = rawUrlPrefix + value1.path
var content = http.get(url).body.string()
if (!content) {
toastLog(value1.path + ': !content')
return
}
returnList.push({
'path': value1.path,
'content': content,
})
toastLog('http.get: ' + url)
}
})
return returnList
}
/**
* 写入手机端,会强行覆盖文件
*/
function overWriteFile(codeList) {
var isOk = false
codeList.forEach((value1) => {
isOk = files.ensureDir(value1.path)
if (!isOk) {
toastLog(value1.path + ': !isOk')
return
}
files.write(value1.path, value1.content)
})
return isOk
}