This repository was archived by the owner on Mar 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildControl.js
532 lines (452 loc) · 15.2 KB
/
buildControl.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import BaseSourced from './baseSourced'
import Git from './git'
import Npm from './npm'
import fs from 'fs-extra'
import path from 'path'
import url from 'url'
import semver from 'semver'
const Default = {
branch: 'dist', // The branch to commit to.
versionBump: 'patch', // Will bump the versino if package.json is present https://docs.npmjs.com/cli/version. Pass false to avoid bump.
remote: {
repo: '../', // The remote repo to push to (URL|RemoteName|FileSystemPath). Common examples include:
// - `[email protected]:alienfast/foo.git` - your main project's remote (gh-pages branch)
// - `../` - the local project repository itself
// If token && login are provided, the remote.repo will be formatted to include these
login: undefined,
token: undefined,
branch: undefined// The remote branch to push to. Common usage would be for Heroku's master branch requirement.
},
clean: { // clean the cwd dir before/after a run
before: false,
after: false
},
tag: {
name: undefined, // fn or string. Default will autoresolve from the package.json version if possible. Pass false to avoid tagging.
existsFailure: true // if tag already exists, fail the execution
},
push: true, // Pushes `branch` to remote. If tag is set, pushes the specified tag as well. false will disable
disableRelativeAutoPush: false, // when testing, we may have nothing to push to. By default, if using a remote repo that is relative, will try to push using the config.branch using the sourceGit all the way to the server.
commit: {
auto: true, // Commits built code to `branch`. A new commit is only created if the built code has changed. false will disable
// The commit template to use when committing. (special characters must be escaped)
// The following tokens are replaced:
// - %sourceName%: the package.json name or the project directory name
// - %sourceTag%: the current tag i.e. v1.0.0
// - %sourceBranch%: the current branch
// - %sourceCommit%: the most recent commit
template: `Built %sourceName% %sourceTag% from commit %sourceCommit% on branch %sourceBranch%`,
ensure: true // require the source and build to be fully committed prior to running.
},
fetch: {
shallow: false // Fetches branch from remote with the flag --depth=1. Which makes a shallow clone with a history truncated to the last revision. Might bring some boost on long-history repositories.
},
git: {
config: {} // [git config](http://git-scm.com/docs/git-config) settings for the repository when preparing the repository. e.g. `{'user.name': 'John Doe'}`
},
force: false // Pushes branch to remote with the flag --force. This will NOT checkout the remote branch, and will OVERRIDE remote with the repo commits. Use with caution.
}
const BuildControl = class extends BaseSourced {
constructor(...configs) {
super(Default,
{tag: {name: () => this.autoResolveTagName()}}, // tag package version auto resolver
...configs
)
// modify
// Build remote repo if sensitive information is passed in
if (this.config.remote.login && this.config.remote.token) {
this.log(`Configuring remote with login and token...`)
let remote = url.parse(this.config.remote.repo)
this.config.remote.repo = url.format({
protocol: remote.protocol,
auth: this.config.remote.login + ':' + this.config.remote.token,
host: remote.host,
pathname: remote.pathname
})
// configure sensitive information
this.config.sensitive[`${this.config.remote.login}:${this.config.remote.token}`] = '<credentials>'
this.config.sensitive[this.config.remote.token] = '<token>'
}
this.sourceGit = new Git({
debug: this.config.debug,
cwd: this.config.sourceCwd,
sensitive: this.config.sensitive
})
this.git = new Git({
debug: this.config.debug,
cwd: this.config.cwd,
sensitive: this.config.sensitive
})
this.npm = new Npm({
debug: this.config.debug,
cwd: this.config.cwd,
versionBump: this.config.versionBump,
sourceCwd: this.config.sourceCwd,
sensitive: this.config.sensitive
})
// Ensure/initialize
this.cleanBefore()
this.ensureDir()
if (this.config.remote.repo === '../') {
this.verifySourceBranchIsTracked()
}
// Set up repository
this.ensureGitInit()
this.configureGit()
this.ensureRemote()
}
ensureDir() {
// reestablish a build dir
fs.ensureDirSync(this.config.cwd)
}
cleanBefore() {
// clean dir
if (this.config.clean.before) {
fs.removeSync(this.config.cwd)
}
}
cleanAfter() {
// clean dir
if (this.config.clean.after) {
fs.removeSync(this.config.cwd)
}
}
resolveBranch() {
return (this.config.remote.branch || this.config.branch)
}
/**
* Can run prior to tests etc to ensure versions are ready as well as commits.
*/
prepublishCheck() {
// Check if git version meets requirements
let version = this.git.version()
if (!version || semver.lt(version, '1.8.0')) {
throw(`Current Git version is ${version}. This plugin requires Git >= 1.8.0.`)
}
// If config.commit.ensure is true check that the main project's working directory is clean
if (this.config.commit.ensure) {
this.git.ensureCommitted()
this.sourceGit.ensureCommitted()
}
if (this.config.fetch.shallow && semver.lt(version, '1.9.0')) {
this.notifyError(`Option "fetch.shallow" is supported on Git >= 1.9.0 and your version is ${version}.`)
}
}
prepublishBuildCheck() {
this.prepublishCheck()
// trigger message if tag exists in remote.
this.tagName()
// Check that build directory contains files
if (fs.readdirSync(this.config.cwd).length === 0) {
throw(`Build directory ${this.config.cwd} is empty. Nothing to version.`)
}
// Check that build directory exists
if (!fs.existsSync(this.config.cwd)) {
throw(`Build directory ${this.config.cwd} doesn't exist. Nothing to version.`)
}
}
/**
* Attempt to track a branch from origin. It may fail on times that the branch is already tracking another remote. There is no problem when that happens, nor does it have any affect
*/
verifySourceBranchIsTracked() {
this.sourceGit.track(this.config.branch)
}
/**
* Initialize git repo if one doesn't exist
*/
ensureGitInit() {
let repo = path.join(this.config.cwd, '.git')
if (!fs.existsSync(repo)) {
this.log(`Creating git repository in ${this.config.cwd}.`)
this.git.init()
}
else {
this.debug(`Git repo already exists in ${this.config.cwd}.`)
}
}
/**
* Initialize the git config
*/
configureGit() {
this.debugDump(`this.config.git.config`, this.config.git.config)
for (let key of Object.keys(this.config.git.config)) {
this.git.configure(key, this.config.git.config[key])
}
}
/**
* Create a named remote if one doesn't exist
*/
ensureRemote() {
let remoteUrlRegex = new RegExp('[\/\\:]')
if (remoteUrlRegex.test(this.config.remote.repo)) {
this.config.remote.name = this.git.hash('remote', this.config.remote.repo)
if (!this.git.remote().includes(this.config.remote.name)) {
this.log(`Creating remote ${this.config.remote.name}`)
this.git.remoteAdd(this.config.remote.name, this.config.remote.repo)
}
}
}
/**
* Check if local branch can safely merge upstream (requires fetched refs)
* @returns {boolean}
*/
shouldUpdate() {
// WARNING: With force, we're not even going to attempt to check out, We're just going to push the repo and override EVERYTHING in the remote
if (this.config.force === true) return false
let status = this.git.status()
if (status) {
let ahead = status.includes('ahead')
let behind = status.includes('behind')
if (ahead && behind) {
throw('The remote and local branches have diverged please\n' +
'resolve manually. Deleting the local **built code**\n' +
'.git directory will usually fix things up.')
}
else if (ahead) {
return false
}
else if (behind) {
return true
}
}
}
/**
* Fetch remote refs to a specific branch, equivalent to a pull without checkout
* @param dest
*/
fetch(dest) {
let branch = this.resolveBranch() + (dest ? ':' + this.config.branch : '');
this.log(`Fetching '${this.config.branch}' ${(this.config.fetch.shallow ? 'files' : 'history')} from ${this.config.remote.repo}.`);
this.git.fetch(this.config.remote.name, branch, this.config.fetch.shallow)
}
/**
* Set branch to track remote
*/
ensureLocalBranchTracksRemote() {
let remoteBranch = this.config.remote.branch || this.config.branch;
if (this.git.configBranchRemote(this.config.branch) !== this.config.remote.name) {
this.git.branchRemote(this.config.branch, this.config.remote.name, remoteBranch)
}
}
sourceName() {
if (this._sourceName) {
return this._sourceName
}
else {
if (this.npm.package() != null) {
this._sourceName = this.npm.package().name
}
else {
this._sourceName = this.config.sourceCwd.split('/').pop()
}
return this._sourceName
}
}
sourceCommit() {
if (this._sourceCommit) {
return this._sourceCommit
}
return this._sourceCommit = this.sourceGit.sourceCommit()
}
sourceCommitFull() {
if (this._sourceCommitFull) {
return this._sourceCommitFull
}
return this._sourceCommitFull = this.sourceGit.sourceCommitFull()
}
sourceCommitLink() {
return `[\`%sourceCommit%\`](../../commit/%sourceCommitFull%)`
.replace(/%sourceCommit%/g, this.sourceCommit())
.replace(/%sourceCommitFull%/g, this.sourceCommitFull())
}
sourceBranch() {
if (this._sourceBranch) {
return this._sourceBranch
}
return this._sourceBranch = this.sourceGit.sourceBranch()
}
sourceTag() {
if (this._sourceTag) {
return this._sourceTag
}
''
let name = this.tagName()
if (name) {
this._sourceTag = name
}
else {
this._sourceTag = ''
}
return this._sourceTag
}
sourceTagLink() {
if (this.tagName()) {
return `[\`%sourceTag%\`](../../releases/tag/%sourceTag%)`
.replace(/%sourceTag%/g, this.sourceTag())
}
else {
return ''
}
}
interpolate(template) {
return template
.replace(/%sourceName%/g, this.sourceName())
.replace(/%sourceTag%/g, this.sourceTag())
.replace(/%sourceTagLink%/g, this.sourceTagLink())
.replace(/%sourceCommit%/g, this.sourceCommit())
.replace(/%sourceCommitFull%/g, this.sourceCommitFull())
.replace(/%sourceCommitLink%/g, this.sourceCommitLink())
.replace(/%sourceBranch%/g, this.sourceBranch())
}
/**
* Stage and commit to a branch
*/
commit() {
if (!this.config.commit.auto) {
return
}
let message = this.interpolate(this.config.commit.template)
// If there are no changes, skip commit
if (!this.git.status()) {
this.log('No changes to your branch. Skipping commit.')
return
}
this.log(`Committing changes to '${this.config.branch}'.`)
this.git.add()
this.git.commit(message)
}
/**
* Tag local branch
*/
tag() {
let tagName = this.tagName()
if (!tagName) {
return
}
this.log(`Tagging the local repository with ${tagName}`)
this.git.tag(tagName)
}
/**
* Convenience to resolve from a fn or string. If tag already exists in a remote, it will return false (don't forget to bump your version in the package.json!)
*/
tagName() {
if (this._tagName !== undefined) {
return this._tagName
}
if (this.config.tag.name === false) {
this._tagName = false
}
else if (this.config.tag.name === undefined) {
this._tagName = false
}
else {
let name = null
if (typeof this.config.tag.name === 'function') {
name = this.config.tag.name()
}
else {
name = this.config.tag.name
}
// If the tag exists, skip tagging
if (name && this.tagExists(name)) {
let msg = `The tag "${name}" already exists on ${this.config.remote.name}`
if (this.config.tag.existsFailure) {
this.notifyError(msg)
}
else {
this.log(`WARNING: ${msg}, skipping tagging.`)
}
name = false
}
this._tagName = name
}
return this._tagName
}
/**
* Resolver plugged into options as tag: {name: ()} that can be overridden by a string or other fn
*/
autoResolveTagName() {
if (this.npm.package() && this.npm.package().version) {
return `v${this.npm.package().version}`
}
else {
return false
}
}
tagExists(name) {
if (this.git.tagExists(name, this.config.remote.name)) {
return true
}
else {
return false
}
}
/**
* Push branch to remote
*/
push() {
if (!this.config.push) {
return
}
let branch = this.config.branch
if (this.config.remote.branch) {
branch += `:${this.config.remote.branch}`
}
this.git.push(this.config.remote.name, branch, this.config.force)
if (this.tagName()) {
this.git.pushTag(this.config.remote.name, this.tagName())
}
// if this was pushed to a relative path, go ahead and try and push that up to the origin
if (!this.config.disableRelativeAutoPush && this.config.remote.repo.includes('..')) {
let remote = 'origin'
this.log(`Repo is using relative path, pushing ${branch} from the source directory...`)
this.sourceGit.push(remote, branch)
if (this.tagName()) {
this.sourceGit.pushTag(remote, this.tagName())
}
}
}
localBranchExists() {
return this.git.branchExists(this.config.branch)
}
remoteBranchExists() {
return this.git.branchRemoteExists(this.resolveBranch(), this.config.remote.name)
}
run() {
// Run task
this.log(this.interpolate(`Starting %sourceName% for commit %sourceCommit% on branch %sourceBranch% using directory ${this.config.cwd}...`))
// Prepare
this.prepublishBuildCheck()
// Set up local branch
let localBranchExists = this.localBranchExists()
let remoteBranchExists = this.remoteBranchExists()
if (remoteBranchExists) {
this.fetch()
}
if (remoteBranchExists && localBranchExists) {
// Make sure local is tracking remote
this.ensureLocalBranchTracksRemote()
// Update local branch history if necessary
if (this.shouldUpdate()) {
this.fetch(true)
}
}
else if (remoteBranchExists && !localBranchExists) { //// TEST THIS ONE
// Create local branch that tracks remote
this.git.track(this.config.branch, this.config.remote.name, this.resolveBranch())
}
else if (!remoteBranchExists && !localBranchExists) {
// Create local branch
this.log(`Creating branch '${this.config.branch}'.`)
this.git.checkout(this.config.branch)
}
// Perform actions
this.git.symbolicRefHead(this.config.branch)
this.git.reset()
this.commit()
this.tag()
this.push()
this.cleanAfter()
}
}
export default BuildControl