-
Notifications
You must be signed in to change notification settings - Fork 0
/
gh-pages.js
200 lines (180 loc) · 5.95 KB
/
gh-pages.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const path = require('path')
const sh = require('kool-shell/namespaced')('brocessing_ghp')
const fs = require('fs-extra')
const tmp = require('tmp')
const { ncp } = require('ncp')
tmp.setGracefulCleanup()
const steps = 5
const defaultOpts = {
append : undefined,
branch : 'gh-pages',
cache : path.join(process.cwd(), '.gh-pages-cache'),
message : ':package: Update gh-pages',
quiet : false,
cwd : process.cwd(),
force : false,
}
function ghpages (copyPath, opts) {
copyPath = copyPath || process.cwd()
opts = Object.assign({}, defaultOpts, opts || {})
let remote = ''
let cleanupCallback
const api = {
deploy
}
const copyOptions = {
clobber: true,
dereference: false,
preserveTimestamps: true,
filter: path => path !== opts.cache
}
function deploy () {
return new Promise((resolve, reject) => {
isEverythingCommit()
.then(getRemoteGit)
.then(() => {
if (!opts.quiet) console.log()
if (!opts.quiet) sh.step(1, steps, 'Rebuilding cache folder...')
})
.then(removeCacheFolder)
.then(createCacheFolder)
.then(() => {
if (!opts.quiet) sh.step(2, steps, `Init git and ${opts.branch} branch...`)
})
.then(gitInit)
.then(addRemoteOrigin)
.then(checkoutGhPages)
.then(() => {
if (!opts.quiet) sh.step(3, steps, 'Copying dist folder...')
})
.then(copyFiles)
.then(appendFiles)
.then(() => commitAndPush(opts.message))
.then(removeCacheFolder)
.then(() => {
let components = (/github\.com[:/]([0-9a-z_.-]+)\/([0-9a-z_.-]+)$/gi).exec(remote)
if (components) {
const author = components[1]
let url = components[2]
if (url && url.slice(-4) === '.git') url = url.slice(0, -4)
resolve(`https://${author}.github.io/${url}/`)
} else {
resolve(null)
}
})
.catch(reject)
})
}
function getRemoteGit () {
let errorMsg = 'No remote repository ! Deploy failed.'
return new Promise((resolve, reject) => {
sh.silentExec('git', ['config', '--get', 'remote.origin.url'], {cwd: opts.cwd})
.then((res) => {
if (!res || res.stdout === '') return reject(errorMsg)
if (res.stderr && res.stderr !== '') return Promise.reject(res)
remote = res.stdout.trim()
resolve(remote)
})
.catch((e) => {
if (e.stderr === undefined) return reject(e)
if (e.stderr !== '' || e.stdout !== '') return reject(e)
reject('Error with the command git config --get remote.origin.url')
})
})
}
function gitInit () {
return sh.silentExec('git', ['init'], { cwd: opts.cache })
}
function addRemoteOrigin () {
return sh.silentExec('git',
['remote', 'add', 'origin', remote],
{ cwd: opts.cache })
}
function removeCacheFolder () {
return new Promise((resolve, reject) => {
fs.remove(opts.cache, e => {
try { cleanupCallback && cleanupCallback() } catch (e) {}
return e ? reject(e) : resolve()
})
})
}
function createCacheFolder () {
return new Promise((resolve, reject) => {
tmp.dir((err, path, cb) => {
if (err) reject(err)
opts.cache = path
cleanupCallback = cb
fs.mkdirp(opts.cache, e => e ? reject(e) : resolve())
resolve()
})
})
}
function isEverythingCommit () {
if (opts.force) return Promise.resolve()
return new Promise((resolve, reject) => {
sh.silentExec('git', ['status', '--porcelain'], {cwd: opts.cwd})
.then((res) => {
if (res.stderr && res.stderr !== '') return Promise.reject(res)
if (res.stdout.trim() !== '') reject('Uncommitted git changes! Deploy failed.')
else resolve()
})
.catch((e) => {
if (e.stderr === undefined) return reject(e)
if (e.stderr !== '' || e.stdout !== '') return reject(e)
reject('Error with the command git status --porcelain')
})
})
}
function copyFiles () {
return new Promise((resolve, reject) => {
fs.copy(copyPath, opts.cache, copyOptions, (err) => err ? reject(err) : resolve())
})
}
function appendFiles () {
if (!opts.append) return
return Promise.all(
(Array.isArray(opts.append) ? opts.append : [opts.append]).map(dir => new Promise((resolve, reject) => {
ncp(dir, path.join(opts.cache, path.basename(dir)), (err) => err ? reject(err) : resolve())
}))
)
}
function checkoutGhPages () {
return new Promise((resolve, reject) => {
sh.silentExec('git',
['show-ref', '--verify', '--opts.quiet', 'refs/heads/' + opts.branch],
{cwd: opts.cache})
.then(() => {
sh.silentExec('git', ['checkout', opts.branch], {cwd: opts.cache})
.then(resolve, reject)
})
.catch((e) => {
sh.silentExec('git', ['checkout', '-b', opts.branch], {cwd: opts.cache})
.then(resolve, reject)
})
})
}
function commitAndPush (message) {
return new Promise((resolve, reject) => {
if (!opts.quiet) sh.step(4, steps, 'Adding files and commit...')
sh.silentExec('git', ['add', '-A'], {cwd: opts.cache})
.then(() => sh.silentExec('git',
['commit', '-m', message, '--no-verify'],
{cwd: opts.cache}))
.catch(e => {
return (e.stdout && ~e.stdout.indexOf('othing to commit'))
? Promise.reject('Nothing to commit')
: Promise.reject(e)
}) // mute error if there is nothing to commit
.then(() => {
if (!opts.quiet) sh.step(5, steps, 'Pushing files - this may take a moment...')
})
.then(() => sh.silentExec('git',
['push', 'origin', opts.branch, '--force'],
{cwd: opts.cache}))
.then(resolve)
.catch(reject)
})
}
return api
}
module.exports = ghpages