-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerator.js
118 lines (103 loc) · 2.74 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
'use strict';
const fs = require('fs');
const path = require('path');
const Enquirer = require('enquirer');
const isValid = require('is-valid-app');
const camelcase = require('camel-case');
const firstCommit = require('gfc');
const clone = require('gh-clone');
module.exports = function(app, base) {
if (!isValid(app, 'generate-git')) return;
/**
* Initialize prompts
*/
const 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;
}
firstCommit(app.cwd, function(err) {
if (err && !/Command failed/.test(err.message)) {
cb(err);
} else {
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) {
const opts = Object.assign({}, app.options);
// if defined on options, clone now
if (opts.clone) {
opts.repo = opts.clone;
clone(opts, cb);
return;
}
// prompt the user for repo to clone
enquirer.ask('clone').then(function(answer) {
if (answer.clone) {
opts.repo = answer.clone;
app.log.info('cloning', opts.repo);
clone(opts, cb);
} else {
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) {
enquirer.ask(name).then(answer => {
if (answer[this.name]) {
app.build(this.name.replace('prompt-', ''), cb);
} else {
cb();
}
});
});
};