-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
233 lines (212 loc) · 9.89 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
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
/*jslint node: true */
'use strict';
/**package
{
"name": "volo-ghdeploy",
"description": "A volo command for deploying to GitHub Pages",
"keywords": [
"volo"
],
"version": "0.0.6",
"homepage": "http://github.com/volojs/volo-ghdeploy",
"author": "James Burke <[email protected]> (http://github.com/jrburke)",
"licenses": [
{
"type": "BSD",
"url": "https://github.com/volojs/volo-ghdeploy/blob/master/LICENSE"
},
{
"type": "MIT",
"url": "https://github.com/volojs/volo-ghdeploy/blob/master/LICENSE"
}
],
"engines": {
"node": ">=0.6.7"
}
}
**/
var path = require('path'),
fs = require('fs');
/**
* Returns a volo command that is wired up to use the given
* buildDir and pagesDir
*
* @param {String} buildDir the directory that has the build contents that
* will be deployed to github
* @param {Sting} pagesDir the directory to use to hold the actual code
* deployed to github. This command will copy the contents from buildDir
* to the pagesDir, and set up the gh-deploy branch in the pagesDir.
*
* @return {Object} The volo command.
*/
module.exports = function (buildDir, pagesDir) {
//Return an object that conforms to the volo command API.
return {
summary: 'Deploys code in ' + buildDir + ' to GitHub Pages. Uses ' +
pagesDir + ' to house the deployed code and git info for the ' +
'gh-pages branch',
run: function (d, v, namedArgs) {
var spawnOptions = {
useConsole: !namedArgs.quiet
},
q = v.require('q'),
github = v.require('./lib/github'),
githubAuth = v.require('./lib/github/auth'),
authInfo,
repoOwner,
repoName,
hasGhPages;
//Work with q 0.9 or 0.8
(q.fcall || q.call)(function () {
//First check if there is already a repo
if (!v.exists(buildDir)) {
throw new Error(buildDir + ' does not exist. If that ' +
'directory is generated by a build command, run that ' +
'command first, then retry.');
}
//If already have gh-pages dir, go to next step.
if (v.exists(pagesDir)) {
return;
}
//Figure out if already in a github repo.
return githubAuth.fetch({ v: v })
.then(function (info) {
authInfo = info;
//Suggest the current directory name as the repo name.
repoName = authInfo.user + '/' + path.basename(process.cwd());
return v.prompt(authInfo.user +
', name of github repo [' +
repoName + ']:');
})
.then(function (promptRepoName) {
var dfd = q.defer(),
repoParts;
if (promptRepoName) {
repoName = promptRepoName;
}
//Split repo name into owner and repo
repoParts = repoName.split('/');
if (repoParts.length === 1) {
repoOwner = authInfo.user;
} else if (repoParts.length === 2) {
repoOwner = repoParts[0];
repoName = repoParts[1];
} else {
return new Error('Invalid repo name: ' + repoName);
}
//First check to see if it exists.
github('repos/' + repoOwner + '/' + repoName)
.then(function (data) {
var sshUrl = data.ssh_url;
//Repo exists, see if there is a gh-pages repo
//already
github('repos/' + repoOwner + '/' + repoName + '/branches')
.then(function (data) {
if (data && data.length) {
hasGhPages = data.some(function (branch) {
return branch.name === 'gh-pages';
});
}
dfd.resolve(sshUrl);
}, dfd.reject);
}, function (err) {
if (err.response.statusCode === 404) {
if (repoOwner !== authInfo.user) {
return new Error('Please create the ' +
repoOwner + '/' + repoName +
' repo on GitHub first before proceeding');
} else {
github('user/repos', {
method: 'POST',
token: authInfo.token,
content: {
name: repoName
}
}).then(function (data) {
dfd.resolve(data.ssh_url);
}, function (err) {
dfd.reject(err);
});
}
} else {
return new Error(err);
}
});
return dfd.promise;
})
.then(function (sshUrl) {
//Set up .git.
v.mkdir(pagesDir);
if (hasGhPages) {
//Set up the git repo locally. Just commit a file
//to get the repo prepped and sent to GitHub.
return v.sequence([
['git', 'clone', sshUrl, pagesDir]
], spawnOptions).then(function () {
return v.withDir(pagesDir, function () {
return v.sequence([
['git', 'checkout', 'gh-pages']
], spawnOptions);
});
});
} else {
//Set up the gh-pages repo in the built area.
return v.withDir(pagesDir, function () {
//Set up the git repo locally. Just commit a file
//to get the repo prepped and sent to GitHub.
return v.sequence([
['git', 'init'],
['git', 'remote', 'add', 'origin', sshUrl],
//This step mandated by:
//http://help.github.com/pages/#project_pages_manually
['git', 'symbolic-ref', 'HEAD', 'refs/heads/gh-pages'],
[v, 'rm', path.join(pagesDir, '.git', 'index')],
['git', 'clean', '-fdx', pagesDir],
[v, 'write', 'index.html', 'Setting up pages...'],
['git', 'add', 'index.html'],
['git', 'commit', '-m', 'Create branch.'],
['git', 'push', 'origin', 'gh-pages']
], spawnOptions);
});
}
});
})
.then(function () {
var message = namedArgs.m;
if (!message) {
message = 'Deploy';
}
//Clean up www-ghpages first, but keep .git
if (v.exists(pagesDir)) {
fs.readdirSync(pagesDir).forEach(function (name) {
if (name !== '.git') {
v.rm(pagesDir + '/' + name);
}
});
}
//Copy the contents of www-built to www-ghpages
//Copy the directory for output.
v.copyDir(buildDir, pagesDir);
//Trigger update to origin.
return v.withDir(pagesDir, function () {
return v.sequence([
//Add any new files
['git', 'add', '.'],
//Remove any files from git that are not on on disk
['git', 'add', '-u'],
['git', 'commit', '-m', message],
['git', 'push', 'origin', 'gh-pages']
], spawnOptions);
});
})
.then(function () {
if (repoName) {
return 'GitHub Pages is set up. Check http://' +
repoOwner + '.github.com/' + repoName +
'/ in about 10-15 minutes.';
}
})
.then(d.resolve, d.reject);
}
};
};