forked from tekd/gulp-gh-pages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
163 lines (148 loc) · 4.8 KB
/
index.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
'use strict';
var git = require('./lib/git');
var gutil = require('gulp-util');
var Transform = require('readable-stream/transform');
var vinylFs = require('vinyl-fs');
var wrapPromise = require('wrap-promise');
/*
* Public: Push to gh-pages branch for github
*
* options - {Object} that contains all the options of the plugin
* - remoteUrl: The {String} remote url (github repository) of the project,
* - origin: The {String} origin of the git repository (default to `"origin"`),
* - branch: The {String} branch where deploy will by done (default to `"gh-pages"`),
* - cacheDir: {String} where the git repo will be located. (default to a temporary folder)
* - push: {Boolean} to know whether or not the branch should be pushed (default to `true`)
* - message: {String} commit message (default to `"Update [timestamp]"`)
*
* Returns `Stream`.
**/
module.exports = function gulpGhPages(options) {
options = options || {};
var origin = options.origin || 'origin';
var branch = options.branch || 'gh-pages';
var message = options.message || 'Update ' + new Date().toISOString();
var files = [];
var TAG;
if (branch !== 'gh-pages') {
TAG = '[gh-pages (' + branch + ')]';
} else {
TAG = '[gh-pages]';
}
return new Transform({
objectMode: true,
transform: function collectFiles(file, enc, cb) {
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new gutil.PluginError('gulp-gh-pages', 'Stream content is not supported'));
return;
}
files.push(file);
cb(null, file);
},
flush: function publish(cb) {
if (files.length === 0) {
gutil.log(TAG, 'No files in the stream.');
cb();
return;
}
var newBranchCreated = false;
git.prepareRepo(options.remoteUrl, origin, options.cacheDir || '.publish')
.then(function(repo) {
gutil.log(TAG, 'Cloning repo');
if (repo._localBranches.indexOf(branch) > -1) {
gutil.log(TAG, 'Checkout branch `' + branch + '`');
return repo.checkoutBranch(branch);
}
if (repo._remoteBranches.indexOf(origin + '/' + branch) > -1) {
gutil.log(TAG, 'Checkout remote branch `' + branch + '`');
return repo.checkoutBranch(branch);
}
gutil.log(TAG, 'Create branch `' + branch + '` and checkout');
newBranchCreated = true;
return repo.createAndCheckoutBranch(branch);
})
.then(function(repo) {
return wrapPromise(function(resolve, reject) {
if (newBranchCreated) {
resolve(repo);
return;
}
// updating to avoid having local cache not up to date
gutil.log(TAG, 'Updating repository');
repo._repo.git('pull', function(err) {
if (err) {
reject(err);
return;
}
resolve(repo);
});
});
})
.then(function(repo) {
// remove all files
return wrapPromise(function(resolve, reject) {
repo._repo.remove('.', {r: true}, function(err) {
if (err) {
reject(err);
return;
}
resolve(repo.status());
});
});
})
.then(function(repo) {
gutil.log(TAG, 'Copying files to repository');
return wrapPromise(function(resolve, reject) {
var destStream = vinylFs.dest(repo._repo.path)
.on('error', reject)
.on('end', function() {
resolve(repo);
})
.resume();
files.forEach(function(file) {
destStream.write(file);
});
destStream.end();
});
})
.then(function(repo) {
return repo.addFiles('.', {force: options.force || false});
})
.then(function(repo) {
var filesToBeCommitted = Object.keys(repo._staged).length;
if (filesToBeCommitted === 0) {
gutil.log(TAG, 'No files have changed.');
cb();
return;
}
gutil.log(TAG, 'Adding ' + filesToBeCommitted + ' files.');
gutil.log(TAG, 'Committing "' + message + '"');
repo.commit(message).then(function(newRepo) {
if (options.push === undefined || options.push) {
gutil.log(TAG, 'Pushing to remote.');
newRepo._repo.git('push', {
'set-upstream': true
}, [origin, newRepo._currentBranch], function(err) {
if (err) {
cb(err);
return;
}
cb();
});
return;
}
cb();
}, cb);
})
.catch(function(err) {
setImmediate(function() {
cb(new gutil.PluginError('gulp-gh-pages', err));
});
});
}
});
};