-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.js
165 lines (142 loc) · 3.89 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
'use strict';
var path = require('path');
var utils = require('./utils');
module.exports = function(app, base, env) {
if (!utils.isValid(app, 'generate-package')) return;
app.use(require('generate-defaults'));
/**
* Helpers
*/
app.helper('date', require('helper-date'));
app.helper('escapeQuotes', function(str) {
return str.replace(/\\?"/g, '\\"');
});
/**
* Middleware
*/
app.postRender(/package\.json$/, function(file, next) {
if (!file.basename === 'package.json') {
next();
return;
}
if (app.options.private) {
var pkg = JSON.parse(file.content);
pkg.private = true;
file.contents = new Buffer(JSON.stringify(pkg, null, 2));
}
next();
});
/**
* Generate a [normalized][normalize-pkg] package.json file in the current working directory.
*
* ```sh
* $ gen package
* # or
* $ gen package:new
* ```
* @name package
* @api public
*/
app.task('package', {silent: true}, ['package-new']);
app.task('default', {silent: true}, ['package-new']);
/**
* Create a [normalized][normalize-pkg] package.json file.
*
* ```sh
* $ gen package:new
* ```
* @name package:new
*/
app.task('new', {silent: true}, ['package-new']);
app.task('package-new', {silent: true}, function() {
return app.src('templates/$package.json', {cwd: __dirname})
.pipe(app.renderFile('*')).on('error', console.log)
.pipe(utils.normalize())
.pipe(app.conflicts(app.cwd))
.pipe(app.dest(app.cwd));
});
/**
* Generate a `package.json` without normalizing the result.
*
* ```sh
* $ gen package:raw
* ```
* @name package:raw
* @api public
*/
app.task('raw', {silent: true}, ['package-raw']);
app.task('package-raw', {silent: true}, function() {
return app.src('templates/$package.json', {cwd: __dirname})
.pipe(app.renderFile('*')).on('error', console.log)
.pipe(app.conflicts(app.cwd))
.pipe(app.dest(app.cwd));
});
/**
* Generate a minimal `package.json` file.
*
* ```sh
* $ gen package:min
* ```
* @name package:min
* @api public
*/
app.task('min', ['package-min']);
app.task('package-min', {silent: true}, function() {
return file(app, 'min.json');
});
/**
* Generate a `package.json` or a sub-directory in a project, with only
* `name`, `description` and `private` fields defined.
*
* ```sh
* $ gen package:sub
* ```
* @name package:sub
* @api public
*/
app.task('sub', ['package-sub']);
app.task('package-sub', {silent: true}, function() {
app.question('subname', 'What name would you like to use?', {default: app.pkg.get('name')});
return file(app, 'sub.json');
});
/**
* Generate a fake `package.json` file to use for development. All of the
* fields in this file are pre-populated with fake data.
*
* ```sh
* $ gen package:dev
* ```
* @name package:dev
* @api public
*/
app.task('dev', ['package-dev']);
app.task('package-dev', {silent: true}, function() {
return file(app, 'dev.json');
});
/**
* Prompts the user to choose the template and fields to use for generating a `package.json` file.
*
* ```sh
* $ gen package:choose
* ```
* @name package:choose
* @api public
*/
app.task('choose', ['package-choose']);
app.task('package-choose', {silent: true}, function() {
return app.src('templates/*.json', {cwd: __dirname})
.pipe(utils.choose({key: 'stem'}))
.pipe(app.renderFile('*')).on('error', console.log)
.pipe(utils.normalize())
.pipe(utils.pick())
.pipe(app.conflicts(app.cwd))
.pipe(app.dest(app.cwd));
});
};
function file(app, pattern) {
var src = app.options.srcBase || path.join(__dirname, 'templates');
return app.src(pattern, {cwd: src})
.pipe(app.renderFile('*')).on('error', console.log)
.pipe(app.conflicts(app.cwd))
.pipe(app.dest(app.cwd));
}