forked from shinnn/gulp-gh-pages
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
132 lines (110 loc) · 3.86 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
'use strict';
const {promisify} = require('util');
const {finished, Transform} = require('stream');
const fancyLog = require('fancy-log');
const git = require('./lib.js');
const inspectWithKind = require('inspect-with-kind');
const {isVinyl} = require('vinyl');
const PluginError = require('plugin-error');
const vinylFs = require('vinyl-fs');
/*
* 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,
* - 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};
const branch = options.branch || 'gh-pages';
const message = options.message || `Update ${new Date().toISOString()}`;
const files = [];
const TAG = branch === 'gh-pages' ? '[gh-pages]' : `[gh-pages (${branch})]`;
return new Transform({
objectMode: true,
transform(file, enc, cb) {
if (!isVinyl(file)) {
if (file !== null && typeof file === 'object' && typeof file.isNull === 'function') {
cb(new PluginError('gulp-gh-pages', 'gulp-gh-pages doesn\'t support gulp <= v3.x. Update your project to use gulp >= v4.0.0.'));
return;
}
cb(new PluginError('gulp-gh-pages', `Expected a stream created by gulp-gh-pages to receive Vinyl objects https://github.com/gulpjs/vinyl, but got ${
inspectWithKind(file)
}.`));
return;
}
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new PluginError('gulp-gh-pages', 'Stream content is not supported'));
return;
}
files.push(file);
cb(null, file);
},
async flush(cb) {
if (files.length === 0) {
fancyLog(TAG, 'No files in the stream.');
cb();
return;
}
try {
const repo = await git.prepareRepo(options.remoteUrl, options.cacheDir || '.publish');
fancyLog(TAG, 'Cloning repo');
if (repo._localBranches.includes(branch)) {
fancyLog(TAG, `Checkout branch \`${branch}\``);
await repo.checkoutBranch(branch);
}
if (repo._remoteBranches.includes(`origin/${branch}`)) {
fancyLog(TAG, `Checkout remote branch \`${branch}\``);
await repo.checkoutBranch(branch);
fancyLog(TAG, 'Updating repository');
// updating to avoid having local cache not up to date
await promisify(repo._repo.git.bind(repo._repo))('pull');
} else {
fancyLog(TAG, `Create branch \`${branch}\` and checkout`);
await repo.createAndCheckoutBranch(branch);
}
await promisify(repo._repo.remove.bind(repo._repo))('.', {
r: true,
'ignore-unmatch': true
});
fancyLog(TAG, 'Copying files to repository');
const destStream = vinylFs.dest(repo._repo.path);
for (const file of files) {
destStream.write(file);
}
setImmediate(() => destStream.end());
await promisify(finished)(destStream);
await repo.addFiles('.', {force: options.force || false});
const filesToBeCommitted = Object.keys(repo._staged).length;
if (filesToBeCommitted === 0) {
fancyLog(TAG, 'No files have changed.');
cb();
return;
}
fancyLog(TAG, `Adding ${filesToBeCommitted} files.`);
fancyLog(TAG, `Committing "${message}"`);
const nrepo = await repo.commit(message);
if (!(options.push === undefined || options.push)) {
cb();
return;
}
fancyLog(TAG, 'Pushing to remote.');
await promisify(nrepo._repo.git.bind(nrepo._repo))('push', {
'set-upstream': true
}, ['origin', nrepo._currentBranch]);
cb();
} catch (err) {
cb(err);
}
}
});
};