forked from DriverPackSolution/DriverPack-Solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
92 lines (83 loc) · 2.67 KB
/
gulpfile.babel.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
import fs from 'fs'
import path from 'path'
import assert from 'assert'
import gulp from 'gulp'
import Promise from 'bluebird'
import git from 'gulp-git'
import gutil from 'gulp-util'
Promise.promisifyAll(fs)
Promise.promisifyAll(git)
const REPO_URL = '[email protected]:DriverPackSolution/update.drp.su.git'
const DEPLOY_DIR = 'update.drp.su'
const REMOTE = 'origin'
const cwd = path.join(__dirname, DEPLOY_DIR)
function copyDist (version) {
return new Promise((resolve, reject) => {
gulp.src('./bin/**/*.{html,hta,png,jpg,gif,ico,ttf,css,js}', {base: './bin'})
.pipe(gulp.dest(path.join('.', DEPLOY_DIR, 'beetle', version)))
.on('end', resolve)
.on('error', reject)
})
}
function commitFiles (commitMessage) {
return new Promise((resolve, reject) => {
gulp.src(cwd)
.pipe(git.add({cwd}))
.pipe(git.commit(commitMessage, {cwd}))
.on('end', resolve)
.on('error', reject)
})
}
function getVersion () {
return fs.readFileAsync('bin/Tools/modules/variables.js')
.then(contents => {
const firstline = contents.toString().split('\n')[0]
return firstline.match(/([\.0-9]+)/)[1]
})
}
const updateProject = {
setActiveVersion (nextVersion) {
const scriptFile = path.join(DEPLOY_DIR, 'v2/index.html')
return fs.readFileAsync(scriptFile)
.then(contents => {
let matches = 0
const patchedContents = contents.toString()
.replace(
/http:\/\/[^\.]+.drp.su\/beetle\/([^\\'"]+)\//g,
(match, currentVersion) => {
gutil.log(`Patching ${scriptFile}: ${currentVersion} => ${nextVersion}`)
matches += 1
return match.replace(currentVersion, nextVersion)
}
)
assert(matches === 1, `setActiveVersion: cannot patch ${scriptFile}, expected 1 URL, but found ${matches}`)
return fs.writeFileAsync(scriptFile, patchedContents)
})
}
}
async function release (branch = 'master') {
let release
const version = await getVersion()
release = {
version,
commitMessage: `Release beetle v${version}`
}
gutil.log(release.commitMessage)
try {
await git.statusAsync({cwd, quiet: true})
} catch (err) {
if (err.code !== 'ENOENT') {
throw err
}
gutil.log(`Cloning ${REPO_URL} to ${DEPLOY_DIR}`)
await git.cloneAsync(REPO_URL, {args: DEPLOY_DIR})
}
await git.checkoutAsync(branch, {cwd})
await git.pullAsync(REMOTE, branch, {cwd, args: '--rebase'})
await copyDist(release.version)
await updateProject.setActiveVersion(version)
await commitFiles(release.commitMessage)
await git.pushAsync(REMOTE, branch, {cwd})
}
gulp.task('release:prod', () => release('master'))
gulp.task('release:test', () => release('dev'))