-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.ts
172 lines (143 loc) · 4.15 KB
/
gulpfile.ts
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
172
/* eslint-disable import/no-extraneous-dependencies */
import gulp from 'gulp'
import childProcess from 'child_process'
import webpack from 'webpack'
import WebpackDevServer from 'webpack-dev-server'
import log from 'fancy-log'
import del from 'del'
import PluginError from 'plugin-error'
// @ts-ignore
import SentryCli from '@sentry/cli'
import path from 'path'
import { outputPath, publicPath } from './webpack/constants'
const absolutePublicPath = path.join(outputPath, publicPath)
const sentryCLi = new SentryCli()
enum ENV {
DEVELOPMENT = 'DEVELOPMENT',
PRODUCTION = 'PRODUCTION',
}
enum CC98_ENV {
INTRANET = 'intranet',
INTRANET_TEST = 'intranet_test',
PUBLIC = 'public',
}
async function getDevWebpackConfig() {
return (await import('./webpack/webpack.config.dev')).default
}
async function getProdWebpackConfig() {
return (await import('./webpack/webpack.config.prod')).default
}
async function checkGitChanges() {
if (process.env.COMMIT_REF) {
log('[checkGitChanges]', 'process.env.COMMIT_REF detected, abort git checking')
return
}
try {
childProcess.execSync('git diff-index --quiet HEAD --')
} catch (e) {
throw new PluginError(
'webpack:build',
new Error('uncommitted changes detected, commit before build')
)
}
}
async function setEnv(env: ENV, cc98Env: CC98_ENV) {
process.env.NODE_ENV = env
process.env.CC98_ENV = cc98Env
process.env.GIT_HEAD =
process.env.COMMIT_REF ||
childProcess
.execSync('git rev-parse HEAD')
.toString()
.trim()
log(
'[setEnv]',
`using env NODE_ENV=${process.env.NODE_ENV} CC98_ENV=${process.env.CC98_ENV} GIT_HEAD=${process.env.GIT_HEAD}`
)
}
async function build() {
const config = await getProdWebpackConfig()
const compiler = webpack(config)
await new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if (err) {
reject(new PluginError('webpack:build', err))
}
log(
'[webpack:build-dev]',
stats.toString({
colors: true,
})
)
resolve()
})
})
}
async function dev() {
const config = await getDevWebpackConfig()
const compiler = webpack(config)
const option = config.devServer!
WebpackDevServer.addDevServerEntrypoints(config, option)
const server = new WebpackDevServer(compiler, option)
server.listen(option.port!, option.host!, err => {
if (err) {
throw new PluginError('webpack:dev', err)
}
log('[webpack:dev-server]', `dev server listening on port ${option.port}`)
})
}
async function clear() {
await del([outputPath])
}
async function uploadSourceMap() {
// await sentryCLi.releases.new(process.env.GIT_HEAD)
log('[uploadSourceMap]', `version: ${process.env.GIT_HEAD}`)
log('[uploadSourceMap]', 'uploading source map')
await sentryCLi.releases.uploadSourceMaps(process.env.GIT_HEAD, {
include: [outputPath],
})
log('[uploadSourceMap]', 'uploading source map done')
log('[uploadSourceMap]', 'setting commits')
await sentryCLi.execute(['releases', 'set-commits', '--auto', process.env.NODE_ENV])
log('[uploadSourceMap]', 'setting commits done')
// await sentryCLi.releases.finalize(process.env.GIT_HEAD)
}
async function clearAfterBuild() {
await del([`${path.join(absolutePublicPath, 'scripts')}/*.map`, `${outputPath}/README.md`])
}
function getProdTasks(cc98Env: CC98_ENV) {
return gulp.series(
checkGitChanges,
clear,
setInternalEnv,
build,
// uploadSourceMap,
clearAfterBuild
)
async function setInternalEnv() {
await setEnv(ENV.PRODUCTION, cc98Env)
}
}
function getDevTasks(cc98Env: CC98_ENV) {
return gulp.series(setInternalEnv, dev)
async function setInternalEnv() {
await setEnv(ENV.DEVELOPMENT, cc98Env)
}
}
Object.values(CC98_ENV).forEach((cc98Env: CC98_ENV) => {
gulp.task(`dev:${cc98Env}`, getDevTasks(cc98Env))
gulp.task(`build:${cc98Env}`, getProdTasks(cc98Env))
})
gulp.task('dev', getDevTasks(CC98_ENV.INTRANET))
gulp.task('build', getProdTasks(CC98_ENV.INTRANET))
gulp.task(
'build:ci',
gulp.series(
// checkGitChanges,
clear,
() => setEnv(ENV.PRODUCTION, CC98_ENV.PUBLIC),
build,
uploadSourceMap,
clearAfterBuild
)
)