Skip to content

Commit 483e172

Browse files
committed
use enquirer, add clone task
also removes generate-gitattributes and generate-gitignore, those can be called separately
1 parent 06d07c6 commit 483e172

File tree

4 files changed

+97
-75
lines changed

4 files changed

+97
-75
lines changed

generator.js

+66-39
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,29 @@
11
'use strict';
22

3+
var fs = require('fs');
34
var path = require('path');
5+
var Enquirer = require('enquirer');
6+
var extend = require('extend-shallow');
47
var utils = require('./utils');
58

69
module.exports = function(app, base) {
710
if (!utils.isValid(app, 'generate-git')) return;
811

912
/**
10-
* Load `base-task-prompts`
13+
* Initialize prompts
1114
*/
1215

13-
var prompts = utils.prompts(app);
16+
var enquirer = new Enquirer();
17+
enquirer.register('confirm', require('prompt-confirm'));
1418

15-
/**
16-
* Generate a `.gitattributes` file. You can override the default template by adding
17-
* a custom template at the following path `~/templates/_gitattributes` (in user home).
18-
* See the [git documentation](https://git-scm.com/docs/gitattributes) for `.gitattributes` files.
19-
*
20-
* ```sh
21-
* $ gen git:gitattributes
22-
* ```
23-
* @name gitattributes
24-
* @api public
25-
*/
26-
27-
app.use(require('generate-gitattributes'));
28-
29-
/**
30-
* Generate a `.gitignore` file. You can override the default template by adding
31-
* a custom template at the following path: `~/templates/_gitignore` (in user home).
32-
*
33-
* ```sh
34-
* $ gen git:gitignore
35-
* ```
36-
* @name gitignore
37-
* @api public
38-
*/
19+
enquirer.question('clone', {
20+
message: 'Which repo would you like to clone (owner/name)?',
21+
});
3922

40-
app.use(require('generate-gitignore'));
23+
enquirer.question('first-commit', {
24+
message: 'Want to do first git commit?',
25+
type: 'confirm'
26+
});
4127

4228
/**
4329
* Initialize a git repository, including `git add` and first commit.
@@ -62,34 +48,75 @@ module.exports = function(app, base) {
6248
* @api public
6349
*/
6450

65-
app.task('first-commit', function(next) {
66-
if (utils.exists(path.resolve(app.cwd, '.git'))) {
67-
app.log.warn('.git exists, skipping');
68-
next();
51+
app.task('first-commit', function(cb) {
52+
if (fs.existsSync(path.resolve(app.cwd, '.git'))) {
53+
app.log.warn(`.git exists, skipping ${this.name} task`);
54+
cb();
6955
return;
7056
}
7157

7258
utils.firstCommit(app.cwd, 'first commit', function(err) {
7359
if (err && !/Command failed/.test(err.message)) {
74-
next(err);
60+
cb(err);
7561
} else {
7662
app.log.success('first commit');
77-
next();
63+
cb();
7864
}
7965
});
8066
});
8167

8268
/**
83-
* Prompt the user to initialize a git repository and create a first commit,
84-
* runs the [first-commit](#first-commit) task if specified by the user.
69+
* Alias for the default task, to provide a semantic task name when using this
70+
* generator as a plugin or sub-generator.
71+
*
72+
* ```sh
73+
* $ gen git:clone
74+
* $ gen git:git-clone # aliased for API usage
75+
* ```
76+
* @name clone
77+
* @api public
78+
*/
79+
80+
app.task('clone', ['prompt-clone']);
81+
app.task('prompt-clone', function(cb) {
82+
var opts = extend({}, app.options);
83+
if (opts.clone) {
84+
opts.repo = opts.clone;
85+
utils.clone(opts, cb);
86+
return;
87+
}
88+
89+
return enquirer.ask('clone')
90+
.then(function(answer) {
91+
if (answer.clone) {
92+
opts.repo = answer.clone;
93+
app.log.info('cloning', opts.repo);
94+
utils.clone(opts, cb);
95+
}
96+
});
97+
98+
});
99+
100+
/**
101+
* Prompts the user to confirm if they'd like to initialize a git repository with
102+
* first [first-commit](#first-commit).
85103
*
86104
* ```sh
87-
* $ gen git:prompt-git
105+
* $ gen updater:prompt-git
88106
* ```
89-
* @name prompt-git
107+
* @name updater:prompt-git
90108
* @api public
91109
*/
92110

93-
app.confirm('git', 'Want to initialize a git repository?');
94-
app.task('prompt-git', {silent: true}, prompts.confirm('git', ['first-commit']));
111+
app.task('prompt-first-commit', function(cb) {
112+
var name = this.name;
113+
return enquirer.ask(name)
114+
.then(function(answer) {
115+
if (answer[name]) {
116+
return app.build(name.replace('prompt-', ''), cb);
117+
}
118+
});
119+
});
120+
95121
};
122+

package.json

+20-16
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
"files": [
1313
"generator.js",
1414
"index.js",
15-
"LICENSE",
16-
"README.md",
1715
"utils.js"
1816
],
1917
"main": "index.js",
@@ -24,21 +22,23 @@
2422
"test": "mocha"
2523
},
2624
"dependencies": {
27-
"base-task-prompts": "^0.1.0",
2825
"camel-case": "^3.0.0",
29-
"fs-exists-sync": "^0.1.0",
30-
"generate-gitattributes": "^0.1.1",
31-
"generate-gitignore": "^0.1.2",
32-
"gitty": "^3.3.6",
33-
"is-valid-app": "^0.2.0",
34-
"lazy-cache": "^2.0.1",
35-
"mkdirp": "^0.5.1"
26+
"enquirer": "^0.4.1",
27+
"extend-shallow": "^2.0.1",
28+
"gh-clone": "^0.5.0",
29+
"gitty": "^3.6.0",
30+
"is-valid-app": "^0.3.0",
31+
"lazy-cache": "^2.0.2",
32+
"mkdirp": "^0.5.1",
33+
"prompt-confirm": "^0.2.1"
3634
},
3735
"devDependencies": {
36+
"base": "^0.13.0",
37+
"base-task": "^0.7.1",
3838
"delete": "^0.3.2",
39-
"generate": "^0.7.1",
40-
"gulp-format-md": "^0.1.9",
41-
"mocha": "^2.5.3"
39+
"generate": "^0.13.1",
40+
"gulp-format-md": "^0.1.12",
41+
"mocha": "^3.3.0"
4242
},
4343
"keywords": [
4444
"app",
@@ -82,10 +82,14 @@
8282
"yeoman",
8383
"yo"
8484
],
85+
"lintDeps": {
86+
"devDependencies": {
87+
"files": [
88+
"f*.js"
89+
]
90+
}
91+
},
8592
"verb": {
86-
"toc": {
87-
"method": "preWrite"
88-
},
8993
"layout": "generator",
9094
"tasks": [
9195
"readme"

test.js

+9-17
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ var del = require('delete');
99
var generator = require('./');
1010
var app;
1111

12-
var cwd = path.resolve.bind(path, __dirname, 'actual');
12+
var actual = path.resolve.bind(path, __dirname, 'actual');
1313

1414
function exists(name, cb) {
15-
var filepath = cwd(name);
15+
var filepath = actual(name);
1616

1717
return function(err) {
1818
if (err) return cb(err);
@@ -27,10 +27,14 @@ function exists(name, cb) {
2727
describe('generate-git', function() {
2828
beforeEach(function() {
2929
app = generate({cli: true, silent: true});
30-
app.cwd = cwd();
31-
app.option('dest', cwd());
32-
app.option('askWhen', 'not-answered');
3330
app.data('author.name', 'Jon Schlinkert');
31+
app.option('prompt', false);
32+
app.option('check-directory', false);
33+
app.option('askWhen', 'not-answered');
34+
app.option('dest', actual());
35+
app.option('overwrite', function(file) {
36+
return /actual/.test(file.path);
37+
});
3438
});
3539

3640
describe('plugin', function() {
@@ -54,8 +58,6 @@ describe('generate-git', function() {
5458
app.use(generator);
5559
assert(app.tasks.hasOwnProperty('default'));
5660
assert(app.tasks.hasOwnProperty('first-commit'));
57-
assert(app.tasks.hasOwnProperty('gitattributes'));
58-
assert(app.tasks.hasOwnProperty('gitignore'));
5961
});
6062

6163
it('should work as a generator', function(cb) {
@@ -68,16 +70,6 @@ describe('generate-git', function() {
6870
app.generate('git:default', exists('.git', cb));
6971
});
7072

71-
it('should run the `gitignore` task', function(cb) {
72-
app.register('git', generator);
73-
app.generate('git:gitignore', exists('.gitignore', cb));
74-
});
75-
76-
it('should run the `gitattributes` task', function(cb) {
77-
app.register('git', generator);
78-
app.generate('git:gitattributes', exists('.gitattributes', cb));
79-
});
80-
8173
it('should run the `first-commit` task', function(cb) {
8274
app.register('git', generator);
8375
app.generate('git:first-commit', exists('.git', cb));

utils.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ require = utils;
99
* Lazily required module dependencies
1010
*/
1111

12-
require('base-task-prompts', 'prompts');
1312
require('camel-case', 'camelcase');
14-
require('fs-exists-sync', 'exists');
15-
require('gitty');
13+
require('gh-clone', 'clone');
1614
require('is-valid-app', 'isValid');
15+
require('gitty');
1716
require('mkdirp');
1817
require = fn;
1918

0 commit comments

Comments
 (0)