-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathes5.js
98 lines (80 loc) · 2.84 KB
/
es5.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
'use strict';
var path = require('path'),
figlet = require('figlet'),
_ = require('underscore'),
cheerio = require('cheerio'),
async = require('async'),
through2 = require('through2'),
File = require('vinyl');
(function () {
'use strict';
function humans(params, next) {
var options = _.defaults(params || {}, {
header: 'Humans.txt'
}),
configuration = [];
next = next || function callback() {
return true;
};
function add(name, object) {
configuration.push('\n/* ' + name + ' */');
stringify(object, '', false);
}
function stringify(object, prepend, isNotFirst) {
prepend = prepend || '';
if (typeof object === 'string') {
configuration.push(prepend + object);
} else if (Array.isArray(object)) {
_.each(object, function (obj, index) {
return stringify(obj, '', index);
});
} else if (object instanceof Object) {
if (isNotFirst) configuration.push("");
_.each(object, function (value, key) {
stringify(value, key + ': ', true);
});
} else {
return next(new Error('Object type for ' + name + ' is not a string or array.'));
}
}
async.waterfall([function (callback) {
return figlet(options.header, function (error, data) {
callback(error, data);
});
}, function (data, callback) {
configuration.push('' + data);
_.each(Object.keys(options), function (name) {
if (name != 'header') if (options[name]) {
add(name.toUpperCase(), options[name]);
}
});
configuration.push("");
callback(null);
}], function (error) {
return next(error, configuration);
});
}
function stream(params) {
params = params || {};
return through2.obj(function (file, enc, callback) {
var $ = cheerio.load(file.contents.toString());
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
return callback(new Error('Streaming not supported'));
}
if (!params.team) {
params.team = $('meta[name="author"]').attr('content');
}
humans(params, function (error, config) {
return callback(error, new File({
path: path.join(file.cwd, 'humans.txt'),
contents: new Buffer(config.join('\n'))
}));
});
});
}
module.exports = humans;
module.exports.stream = stream;
})();