-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.js
246 lines (209 loc) · 4.84 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
'use strict';
var isValid = require('is-valid-app');
module.exports = function(app) {
if (!isValid(app, 'generate-updater')) return;
app.option('layout', false);
/**
* Plugin
*/
app.use(require('generate-project'));
app.use(require('generate-git'));
/**
* Attempt to pre-populate answers
*/
app.on('ask', function(val, key, question, answers) {
if (typeof val !== 'undefined') return;
if (/^author\./.test(key)) {
answers[key] = app.common.get(key);
} else {
answers[key] = app.data(key) || app.base.data(key);
}
});
/**
* Scaffold out an [update][] updater project. Alias for the [updater](#updater)
* task, to allow running the updater with the following command:
*
* ```sh
* $ gen updater
* ```
* @name updater:default
* @api public
*/
app.task('default', ['updater']);
app.task('updater', [
'prompt-data',
'test',
'dotfiles',
'main',
'rootfiles',
'verbmd'
]);
/**
* Scaffold out a minimal [Update][] updater project.
*
* ```sh
* $ gen updater:min
* # or
* $ gen updater:minimal
* ```
* @name updater:minimal
* @api public
*/
app.task('min', ['minimal']);
app.task('minimal', [
'prompt-data',
'gitignore-node',
'main',
'license-mit',
'package',
'readme'
]);
/**
* Scaffold out a project for a [Update][] micro-updater.
*
* ```sh
* $ gen updater:micro
* ```
* @name updater:micro
* @api public
*/
app.task('micro', [
'prompt-data',
'test',
'dotfiles',
'main-micro',
'rootfiles',
'prompt-install',
'prompt-git'
]);
/**
* Write a `updater.js` file to the current working directory.
*
* ```sh
* $ gen updater:file
* ```
* @name updater:file
* @api public
*/
task(app, 'main', ['templates/updatefile.js', 'templates/index.js']);
/**
* Write the `updater.js` and `index.js` files for a micro-updater.
*
* ```sh
* $ gen updater:main-micro
* ```
* @name updater:main-micro
*/
task(app, 'main-micro', ['templates/updater-micro.js', 'templates/index.js']);
/**
* Generate the LICENSE, package.json and README.md files for an updater project.
*
* ```sh
* $ gen updater:rootfiles
* ```
* @name updater:rootfiles
* @api public
*/
app.task('rootfiles', [
'license-mit',
'package',
'readme'
]);
/**
* Adds files tree docs to the `docs` directory.
*
* ```sh
* $ gen updater:docs
* ```
* @name updater:docs
* @api public
*/
app.task('docs', ['trees', 'verbfile', 'verbmd']);
/**
* Adds files tree docs to the `docs` directory.
*
* ```sh
* $ gen updater:docs
* ```
* @name updater:docs
* @api public
*/
task(app, 'trees', ['templates/docs/*.md']);
/**
* Add a `verbfile.js` to the current working directory.
*
* ```sh
* $ gen updater:verbfile
* ```
* @name updater:verbfile
* @api public
*/
task(app, 'verbfile', ['templates/verbfile.js']);
/**
* Add a `.verb.md` readme template to the current working directory.
*
* ```sh
* $ gen updater:verbmd
* ```
* @name updater:verbmd
* @api public
*/
task(app, 'verbmd', ['templates/_verb.md']);
/**
* Create a `test.js` file in the `test` directory, with unit tests for all of the tasks
* in the generated updater.
*
* ```sh
* $ gen updater:test
* ```
* @name updater:test
* @api public
*/
task(app, 'test-basic', ['templates/tests/basic.js']);
task(app, 'test', [
'templates/tests/fixtures/*',
'templates/tests/plugin.js',
'templates/tests/test.js'
]);
/**
* Generate files in the updater's `templates` directory.
*
* ```sh
* $ gen updater:templates
* ```
* @name updater:templates
* @api public
*/
task(app, 'templates', 'templates/templates/*.*');
/**
* Middleware for updating the context
*/
app.preRender(/package\.json$/, function(file, next) {
app.cache.answers = app.cache.answers || {};
var desc = (app.cache.answers.description || '').replace(/[.\s]+$/, '');
app.cache.answers.description = desc + `. Use from the command line when Update's CLI is installed globally, or use as a plugin in your own updater.`
next();
});
/**
* Don't ask the same questions more than once
*/
app.task('prompt-data', ['prompt'], function(cb) {
app.option('askWhen', 'not-answered');
app.data('description', 'My awesome updater.');
cb();
});
};
/**
* Create a task with the given `name` and glob `pattern`
*/
function task(app, name, patterns) {
app.task(name, function() {
return file(app, patterns);
});
}
function file(app, patterns) {
return app.src(patterns, {cwd: __dirname})
.pipe(app.renderFile('*', {layout: null})).on('error', console.log)
.pipe(app.conflicts(app.cwd))
.pipe(app.dest(app.cwd));
}