forked from Tomotoes/HomePage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.js
171 lines (142 loc) · 4.29 KB
/
deploy.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env node
console.log(new Date())
const timeFlag = 'Deploy.js 运行时间: '
console.time(timeFlag)
const identifier = 'home'
const log = require('../Version/log')
const getVersionProxy = require('../Version/getVersionProxy')
const V = getVersionProxy(identifier)
const generateVersionNumber = require('../Version/generateVersionNumber')
const updateSW = require('../Version/updateSW')
const deleteOldVersion = require('../Version/deleteOldVersion')
const exec = require('../Version/execCommand')
const needUpdateVersion = require('../Version/needUpdateVersion')
const printEmptyLine = () => console.log()
let command
let newVersion
const wf = {
updateHomeVersion() {
log.error('>>> 更新 Home 项目版本号...')
if (V.result) {
newVersion = generateVersionNumber()
V.oldVersion = V.version
V.version = newVersion
log.success(`=> Home 项目版本号已成功更新为 ${newVersion} !`)
} else {
newVersion = V.version
log.info(
`=> Home 项目上次 Deploy 执行失败 , 版本号将不会发生改变 , 依然为 ${newVersion} !`
)
}
printEmptyLine()
},
async buildHomeProject() {
command = ['cd .', 'npm run build'].join(' && ')
log.error('>>> 利用新的版本号构建 Home 项目...')
log.info(`执行命令: ${command}`)
printEmptyLine()
await exec(command)
log.success('=> Home 项目已构建完毕!')
printEmptyLine()
},
async updateSWScriptByHomeNewVersion() {
log.error(
'>>> 利用 Home 项目新的版本号 去更新 SW 脚本中 Home 的缓存文件...'
)
printEmptyLine()
await updateSW(identifier)
log.success('=> SW 脚本更新完毕!')
printEmptyLine()
},
async buildSWScript() {
command = ['cd .', 'gulp pwa'].join(' && ')
log.error('>>> 构建新的 SW 脚本...')
log.info(`执行命令: ${command}`)
printEmptyLine()
await exec(command)
log.success('=> SW 脚本构建完毕!')
printEmptyLine()
},
async configHomeProjectGitRepo() {
command = [
'cd ./Home',
'git add -A',
`git commit -am"update at ${new Date().toDateString()}"`
].join(' && ')
log.error('>>> 配置 Home 项目的 Git 仓库...')
log.info(`执行命令: ${command}`)
printEmptyLine()
await exec(command)
log.success('=> Home 项目的 Git 仓库配置完毕!')
printEmptyLine()
},
async deployHomeProject() {
command = ['cd ./Home', 'git push -f origin master'].join(' && ')
log.error('>>> 上传 Home 项目...')
log.info(`执行命令: ${command}`)
printEmptyLine()
await exec(command)
log.success('=> Home 项目上传完毕!')
printEmptyLine()
},
async deployHomeProjectWithNewVersion() {
command = [
'cd ./Home',
`git tag ${newVersion}`,
`git push -f origin ${newVersion}`
].join(' && ')
log.error(`>>> 上传 Home 项目的 ${newVersion} 版本...`)
log.info(`执行命令: ${command}`)
printEmptyLine()
await exec(command)
log.success(`=> Home 项目的 ${newVersion} 版本上传完毕!`)
printEmptyLine()
},
async deleteOldVersion() {
log.error('>>> 正在删除旧版本的 Home 项目..')
printEmptyLine()
await deleteOldVersion(identifier)
log.success('=> 旧版本的 Home 项目删除完毕!')
printEmptyLine()
},
handleSuccess() {
log.success('全部命令执行完毕!')
V.result = true
},
handleError(error) {
console.error(error)
printEmptyLine()
log.debug('!!! 程序发生异常!')
V.result = false
},
handleFinally() {
console.timeEnd(timeFlag)
}
}
const deploy = () => {
if (process.argv.pop() === '-f' || needUpdateVersion(identifier)) {
Promise.resolve()
.then(wf.updateHomeVersion)
.then(wf.buildHomeProject)
.then(wf.updateSWScriptByHomeNewVersion)
.then(wf.buildSWScript)
.then(wf.configHomeProjectGitRepo)
.then(() => Promise.all([
wf.deployHomeProject(),
wf.deployHomeProjectWithNewVersion(),
wf.deleteOldVersion()
]))
.then(wf.handleSuccess)
.catch(wf.handleError)
.finally(wf.handleFinally)
} else {
Promise.resolve()
.then(wf.buildHomeProject)
.then(wf.configHomeProjectGitRepo)
.then(wf.deployHomeProject)
.then(wf.handleSuccess)
.catch(wf.handleError)
.finally(wf.handleFinally)
}
}
deploy()