-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerator.js
122 lines (103 loc) · 2.67 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
'use strict';
var fs = require('fs');
var path = require('path');
var Enquirer = require('enquirer');
var extend = require('extend-shallow');
var utils = require('./utils');
module.exports = function(app, base) {
if (!utils.isValid(app, 'generate-git')) return;
/**
* Initialize prompts
*/
var enquirer = new Enquirer();
enquirer.register('confirm', require('prompt-confirm'));
enquirer.question('clone', {
message: 'Which repo would you like to clone (owner/name)?',
});
enquirer.question('first-commit', {
message: 'Want to do first git commit?',
type: 'confirm'
});
/**
* Initialize a git repository, including `git add` and first commit.
*
* ```sh
* $ gen git
* ```
* @name default
* @api public
*/
app.task('default', {silent: true}, ['first-commit']);
/**
* Alias for the default task, to provide a semantic task name when using this
* generator as a plugin or sub-generator.
*
* ```sh
* $ gen git:first-commit
* ```
* @name first-commit
* @api public
*/
app.task('first-commit', function(cb) {
if (fs.existsSync(path.resolve(app.cwd, '.git'))) {
app.log.warn(`.git exists, skipping ${this.name} task`);
cb();
return;
}
utils.firstCommit(app.cwd, 'first commit', function(err) {
if (err && !/Command failed/.test(err.message)) {
cb(err);
} else {
!app.option('silent') && app.log.success('first commit');
cb();
}
});
});
/**
* Alias for the default task, to provide a semantic task name when using this
* generator as a plugin or sub-generator.
*
* ```sh
* $ gen git:clone
* $ gen git:git-clone # aliased for API usage
* ```
* @name clone
* @api public
*/
app.task('clone', ['prompt-clone']);
app.task('prompt-clone', function(cb) {
var opts = extend({}, app.options);
if (opts.clone) {
opts.repo = opts.clone;
utils.clone(opts, cb);
return;
}
return enquirer.ask('clone')
.then(function(answer) {
if (answer.clone) {
opts.repo = answer.clone;
app.log.info('cloning', opts.repo);
utils.clone(opts, cb);
}
});
});
/**
* Prompts the user to confirm if they'd like to initialize a git repository with
* first [first-commit](#first-commit).
*
* ```sh
* $ gen updater:prompt-git
* ```
* @name updater:prompt-git
* @api public
*/
app.task('prompt-first-commit', function(cb) {
var name = this.name;
return enquirer.ask(name)
.then(function(answer) {
if (answer[name]) {
return app.build(name.replace('prompt-', ''), cb);
}
});
});
};