-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
355 lines (294 loc) · 7.71 KB
/
middleware.js
File metadata and controls
355 lines (294 loc) · 7.71 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
'use strict';
var path = require('path');
var utils = require('./utils');
/**
* Expose `middleware`
*/
var middleware = exports = module.exports;
/**
* Update `related` helper arguments to the latest.
*
* @param {Object} `app` Instance of update
*/
middleware.related = function(app) {
return function(file, next) {
var pkg = app.pkg && app.pkg.data;
if (!pkg) {
next(new Error('middleware.related needs a package.json file'));
return;
}
var str = file.contents.toString();
str = extractTag(str, pkg.json, {
prop: 'verb.related.list',
tag: 'related'
});
file.contents = new Buffer(str);
next();
};
};
/**
* Update `reflinks` helper arguments to the latest.
*
* @param {Object} `app` Instance of `update`
*/
middleware.reflinks = function(app) {
return function(file, next) {
var pkg = app.pkg && app.pkg.data;
if (!pkg) {
next(new Error('middleware.reflinks needs a package.json file'));
return;
}
var str = file.contents.toString();
str = extractTag(str, pkg.json, {
prop: 'verb.reflinks',
tag: 'reflinks'
});
file.contents = new Buffer(str);
next();
};
};
/**
* Update `related` helper arguments to the latest.
*/
middleware.jscomments = function(file, next) {
var re = /\{%= (?:js)?comments\(['"](.*)['"]\) %}/;
var str = file.contents.toString();
file.contents = new Buffer(str.replace(re, '{%= apidocs(\'$1\') %}'));
next();
};
/**
* Update `related` helper arguments to the latest.
*/
middleware.license = function(file, next) {
var re = /\{%= license\(.*\) %}/;
var str = file.contents.toString();
file.contents = new Buffer(str.replace(re, '{%= license %}'));
next();
};
/**
* Make sure "install" has a header
*/
middleware.install = function(file, next) {
var str = file.contents.toString();
var m = /\{%= include\(['"]install[^%]+\) %}/.exec(str);
if (!m) return next();
var prefix = str.slice(0, m.index).replace(/\s+$/, '');
var suffix = str.slice(m.index);
var last = prefix.split('\n').pop();
if (/install/i.test(last)) {
return next();
}
prefix += '\n\n## Install\n\n';
file.contents = new Buffer(prefix + suffix);
next();
};
/**
* Strip lint deps comments from .verb.md
*/
middleware.lintDeps = function(file, next) {
var re = /<!--\s*deps:\s*mocha\s*-->/;
var str = file.contents.toString();
if (re.test(str)) {
file.contents = new Buffer(str.replace(re, ''));
}
next();
};
middleware.toGithubUrl = function(file, next) {
if (file.basename !== 'README.md' && file.basename !== 'verb.md') {
next();
return;
}
var lines = file.contents.toString()
.split('\n')
.map(function(line) {
var re = /(.*?\[.*?\]\()node_modules\/(.*?)\/(.*#L\d+\))/;
var m = re.exec(line);
if (m) {
return '';
// var str = m[1];
// str += 'https:/github.com/jonschlinkert/';
// str += m[2];
// str += '/blob/master/';
// str += m[3];
// line = str;
}
return line;
});
file.contents = new Buffer(lines.join('\n'));
next();
};
/**
* Strip apidocs comments from .verb.md
*/
middleware.htmlComments = function(file, next) {
var re = /<!--\s*add.*-->\n*/g;
var str = file.contents.toString();
if (re.test(str)) {
file.contents = new Buffer(str.replace(re, ''));
}
next();
};
/**
* Add a travis badge if the repo has `.travis.yml` and
* no badge already exists.
*/
middleware.travis = function(app) {
return function(file, next) {
var cwd = app.cwd;
if (!exists(cwd, '.travis.yml') && !exists(cwd, 'test') && !exists(cwd, 'test.js')) {
next();
return;
}
var snippet = '{%= badge("travis") %}';
var str = file.contents.toString();
if (/badge\(['"]travis['"]\)/.test(str)) {
next();
return;
}
if (hasLayout(str)) {
next();
return;
}
var lines = str.split('\n');
lines[0] += ' ' + snippet;
str = lines.join('\n');
file.contents = new Buffer(str);
next();
};
};
/**
* Ensure a single newline at the end of .verb.md
*/
middleware.whitespace = function(file, next) {
file.contents = new Buffer(file.contents.toString().trim() + '\n');
next();
};
/**
* Utils
*/
function exists(cwd, filename) {
return utils.exists(path.resolve(cwd, filename));
}
function hasLayout(str) {
return !/^# /.test(str);
}
/**
* Extract arguments from a tag in the given string.
*/
function extractTag(str, json, options) {
json = json || {};
var opts = utils.extend({}, options);
if (!opts.prop) opts.prop = 'verb.' + opts.tag;
var tokens = matchTags(str, opts.tag) || {};
var token = tokens[opts.tag];
if (token && token.args && token.args.length) {
var keys = token.args[0];
token.rawArgs.shift();
if (Array.isArray(keys)) {
utils.union(json, opts.prop, keys);
var prepend = str.slice(0, token.loc.start);
var append = str.slice(token.loc.end);
var newTag = createTag(token, opts);
return prepend + newTag + append;
}
}
return str;
// if the tag occurs more than once, we'll assume it's
// because the user has defined different values for each
// one
// var matches = str.split('{%= ' + opts.tag);
// if (matches.length > 2) {
// return str;
// }
// var reStr = '{%=[ \\t]*' + opts.tag + '\\(\\[(.*)\\].*\\)[ \\t]*%}';
// var re = new RegExp(reStr);
// var match = re.exec(str);
// if (!match) return str;
// // parse the array elements from the tag
// var params = match[1];
// if (params) {
// var keys = params.split(/[,'" ]+/).filter(Boolean);
// // add the array of items to `verb[prop]` in package.json
// union(json, opts.prop, keys);
// }
// // replace the array with a variable
// return str.replace(re, createTag(opts.tag, opts.prop));
}
function createTag(token, opts) {
var inner = opts.prop;
if (token.rawArgs.length) {
// remove extra parens we added earlier
var args = token.rawArgs.map(function(arg) {
return arg.replace(/^\(|\)$/g, '');
});
inner += ', ' + args.join(', ');
}
return '{%= ' + opts.tag + '(' + inner + ') %}';
}
function matchTags(str, tag) {
var re = /{%=([\s\S]+?)%}/;
var match = re.exec(str);
var matches = {};
var pos = 0;
while (match) {
var inner = match[1].trim();
var idx = str.indexOf(match[0]);
pos += idx;
var len = match[0].length;
str = str.slice(idx + len);
var end = pos + len;
var token = parseTag({
loc: { start: pos, end: end },
match: match,
inner: inner
});
pos = end;
matches[token.name] = token;
match = re.exec(str);
}
return matches;
}
function parseTag(token) {
parseName(token);
parseArgs(token);
return token;
}
function parseName(token) {
var match = /^(\w+)/.exec(token.inner.trim());
token.name = match[0];
token.inner = token.inner.slice(token.name.length);
return token;
}
function parseArgs(token) {
var str = clean(token.inner.replace(/[\n\s]+/g, ' '));
var segs = str.split(/,[ \t]*(?={)/);
var rawArgs = token.inner.split(/,[ \t]*(?={)/).join(')!@!(').split('!@!');
var len = rawArgs.length;
var idx = -1;
token.rawArgs = rawArgs;
token.args = [];
while (++idx < len) {
var arg = rawArgs[idx].trim();
var inside = arg.replace(/^\(|\)$/g, '');
if (!inside || !/[\[\](){}]/.test(inside)) {
continue;
}
var value = tryEval(arg);
if (value) {
token.args.push(value);
}
}
delete token.inner;
return token;
}
function clean(str) {
return str.replace(/^['"\s(\[]+|[\])\s"']+$/g, '');
}
function tryEval(arg) {
try {
return eval('(function() {return ' + arg + ' }())');
} catch (err) {
// console.log('updater-verb: could not eval args: "%s"', arg);
}
return {};
}