-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerator.js
427 lines (376 loc) · 12.4 KB
/
generator.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
'use strict';
var path = require('path');
var extend = require('extend-shallow');
var isValid = require('is-valid-app');
var gitAddRemote = require('git-add-remote');
var GitHub = require('github-base');
var github;
module.exports = function(app) {
// return if the generator is already registered
if (!isValid(app, 'generate-gh-repo')) return;
// use generate-data to collect project and repo information
app.use(require('generate-data'));
// configure a datastore to hold github authentication details
var store = app.store.create('generate-gh-repo');
// initialize git-add-remote in the current working directory
var addRemote = gitAddRemote(app.cwd);
// uitility to get a property from `app.cache.data`
var getProp = function(prop) {
return function() {
return app.get(`cache.data.${prop}`);
};
};
// questions to ask when prompting the user for information
var prompts = [
'project.name',
'project.description',
'project.owner',
'project.type',
'homepage',
'private'
];
/**
* Setup prompts that may be asked later
*
* @name gh-repo:questions
*/
app.task('questions', {silent: true}, function(cb) {
// questions to gather information about the repository being created
app.question('project.name', 'What is the name of the repository to create?', {
default: getProp('project.name')
});
app.question('project.description', 'What is the description of the repository?', {
default: getProp('project.description')
});
app.question('project.owner', 'Who is the owner of the repository?', {
default: getProp('project.owner')
});
app.questions.list('project.type', {
message: 'Is the repository owner a "user" or an "organization"?',
choices: [
'user',
'organization'
]
});
app.question('homepage', 'What is the repository\'s homepage?', {
default: function() {
var homepage = getProp('homepage')();
var name = getProp('project.name')();
var owner = getProp('project.owner')();
var repo = `https://github.com/${owner}/${name}`;
if (repo === homepage) {
return getProp('author.url')() || `https://github.com/${owner}`;
}
return homepage;
}
});
app.confirm('private', 'Is the repository private?', {
default: function() {
var val = getProp('private')();
if (typeof val === 'boolean') {
return val;
}
return false;
}
});
// questions for gathering GitHub authentication information from the user
app.confirm('useAuth', 'Found saved GitHub authentication. Would you like to use it?');
app.question('github.auth.username', 'GitHub username?', {force: true});
app.question('github.auth.password', 'GitHub password?', {force: true, type: 'password'});
app.question('github.auth.token', 'GitHub OAuth token?', {force: true});
app.questions.list('github.auth.type', {
force: true,
message: 'Would you like to login using an OAuth token or your GitHub username and password?',
choices: [
'token',
'username/password'
]
});
// prompt the user about adding the newly created git remote
app.confirm('remote', 'Would you like to add the git remote origin url?');
// questions for gathering Travis-CI webhook information from the user
app.confirm('configureTravis', 'Would you like to enable Travis-CI for this repository?');
app.confirm('useTravisAuth', 'Found saved Travis-CI authentication. Would you like to use it?');
app.question('travis.username', 'GitHub username (associated with travis-ci)?', {force: true});
app.question('travis.token', 'Travis-CI token?');
cb();
});
app.task('prompt-travis', {silent: true}, function(cb) {
console.log();
var travis = store.get('travis');
if (travis) {
app.ask('useTravisAuth', {force: true}, function(err, answers) {
if (err) return cb(err);
if (answers.useTravisAuth) {
app.data('travis', travis);
} else {
travis = null;
}
if (travis) return cb();
app.ask(['travis.username', 'travis.token'], handleAnswers);
});
return;
}
app.ask(['travis.username', 'travis.token'], handleAnswers);
function handleAnswers(err, answers) {
if (err) return cb(err);
app.data(answers);
store.set('travis', answers.travis);
cb();
}
});
/**
* Prompt the user for information about the repository being created.
*
* ```sh
* $ gen gh-repo:prompt
* ```
* @name gh-repo:prompt
*/
app.task('prompt', {silent: true}, function(cb) {
console.log();
app.ask(prompts, {force: true}, function(err, answers) {
if (err) return cb(err);
app.data(answers);
cb();
});
});
/**
* Prompt the user for GitHub authentication information
*
* ```sh
* $ gen gh-repo:auth
* ```
* @name gh-repo:auth
*/
app.task('github-auth', {silent: true}, function(cb) {
console.log();
var auth = store.get('github.auth');
if (auth) {
app.ask('useAuth', {force: true}, function(err, answers) {
if (err) return cb(err);
if (answers.useAuth) {
if (auth.type !== 'token') {
delete auth.token;
}
app.data('github.auth', auth);
} else {
auth = null;
}
if (auth) return cb();
app.ask('github.auth.type', handleGithubType);
});
return;
}
app.ask('github.auth.type', handleGithubType);
function handleGithubType(err, answers) {
if (err) return cb(err);
var type = answers.github.auth.type;
if (type === 'token') {
app.ask('github.auth.token', handleAnswers);
return;
}
app.ask([
'github.auth.username',
'github.auth.password'
], handleAnswers);
}
function handleAnswers(err, answers) {
if (err) return cb(err);
app.data(answers);
store.set('github.auth', answers.github.auth);
cb();
}
});
/**
* Initialize the github instance using the user's GitHub authentication information.
*
* ```sh
* $ gen gh-repo:init-github
* ```
* @name gh-repo:init-github
*/
app.task('init-github', {silent: true}, ['github-auth'], function(cb) {
var auth = getProp('github.auth')();
if (!auth) {
cb(new Error('Unable to authenticate to GitHub'));
return;
}
github = new GitHub(auth);
cb();
});
/**
* Create a new repository on GitHub. The generator will attempt to use data from the current project's
* package.json and git repository information, while prompting for user input and validation.
*
* ```sh
* $ gen gh-repo:gh-repo
* ```
* @name gh-repo:gh-repo
* @api public
*/
app.task('gh-repo', ['questions', 'init-github', 'prompt'], function(cb) {
console.log();
var opts = {
owner: getProp('project.owner')(),
repo: getProp('project.name')()
};
var data = {
name: getProp('project.name')(),
description: getProp('project.description')(),
homepage: getProp('homepage')(),
private: getProp('private')()
};
github.get('/repos/:owner/:repo', opts, function(err, res) {
if (err) return cb(err);
if (res.id && !res.message) {
console.log(app.log.timestamp);
console.log(app.log.timestamp, `Unable to create new repository "${opts.owner}/${opts.repo}" because it already exists.`);
console.log(app.log.timestamp);
cb(new Error('Repository already exists.'));
return;
}
if (res.message && res.message !== 'Not Found') {
cb(new Error(res.message));
return;
}
var type = getProp('project.type')();
var fullName = '';
var url = '';
if (type === 'user') {
url = '/user/repos';
var username = getProp('github.auth.username')();
fullName = (username && username + '/') || '';
} else {
url = '/orgs/:owner/repos';
fullName = opts.owner + '/';
}
fullName += data.name;
console.log(app.log.timestamp);
console.log(app.log.timestamp, `Creating "${fullName}" with the following settings:`);
console.log(app.log.timestamp, ` private: ${data.private ? 'true' : 'false'}`);
console.log(app.log.timestamp, ` homepage: ${data.homepage}`);
console.log(app.log.timestamp, ` description: ${data.description}`);
console.log(app.log.timestamp);
app.confirm('confirmCreate', 'Are you sure you want to create this repository?');
console.log();
app.ask('confirmCreate', function(err, answers) {
console.log();
if (err) return cb(err);
if (!answers.confirmCreate) {
console.log(app.log.timestamp);
console.log(app.log.timestamp, 'The repository has not been created. Run `$ gen gh-repo` again to start over.');
console.log(app.log.timestamp);
return cb();
}
github.post(url, extend({}, opts, data), function(err, res) {
if (err) return cb(err);
if (res.message) {
cb(new Error(res.message));
return;
}
console.log(app.log.timestamp);
console.log(app.log.timestamp, `"${opts.owner}/${opts.repo}" has been created.`);
console.log(app.log.timestamp);
console.log();
app.ask('remote', function(err, answers) {
if (err) return cb(err);
if (answers.remote && !process.env.GENERATOR_TEST) {
addRemote.sync('origin', app.cache.data.https + '.git');
}
// ask if the user would like to also enable travis-ci
app.ask('configureTravis', function(err, answers) {
console.log();
if (err) return cb(err);
if (!answers.configureTravis) {
return cb();
}
app.build('enable-travis', cb);
});
});
});
});
});
});
/**
* Enable Travis-CI for the repository by creating a new webhook. This task will
* ask for the Travis-CI username (this should be the same as the GitHub username) and a
* Travis-CI token which can be found under the Accounts menu on Travis-CI.
*
* ```sh
* # use it standalone if the repository has already been created on GitHub
* $ gen gh-repo:enable-travis
* # use it when creating the repository on github
* $ gen gh-repo:default,enable-travis
* ```
* @name gh-repo:enable-travis
* @api public
*/
app.task('enable-travis', ['questions', 'prompt-travis'], function(cb) {
if (!github) {
app.build('init-github', function(err) {
if (err) return cb(err);
enableTravis();
});
return;
}
enableTravis();
function enableTravis() {
var opts = {
owner: getProp('project.owner')(),
repo: getProp('project.name')()
};
// check to see if the webhook exists
github.get('/repos/:owner/:repo/hooks', opts, function(err, hooks) {
if (err) return cb(err);
if (hooks && hooks.message) {
return cb(new Error(hooks.message));
}
if (hasHook(hooks, 'travis')) {
console.log();
console.log(`The Travis-CI webhook is already enabled for "${opts.owner}/${opts.repo}"`);
console.log();
return cb();
}
var data = {
name: 'travis',
config: {
user: getProp('travis.username')(),
token: getProp('travis.token')(),
domain: 'notify.travis-ci.org'
},
active: true
};
github.post('/repos/:owner/:repo/hooks', extend({}, opts, data), function(err, res) {
if (err) return cb(err);
if (res && res.message) {
return cb(new Error(res.message));
}
console.log();
console.log(`The Travis-CI webhook has been enabled for "${opts.owner}/${opts.repo}"`);
console.log();
cb();
})
});
}
function hasHook(arr, name) {
if (!arr || !arr.length) return false;
for (var i = 0; i < arr.length; i++) {
if (arr[i].name === name) {
return true;
}
}
return false;
}
});
/**
* Alias for running the [gh-repo](#gh-repo) task with the following command:
*
* ```sh
* $ gen gh-repo
* ```
* @name default
* @api public
*/
app.task('default', ['gh-repo']);
};