forked from webpack/webpack.js.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_package_files.js
executable file
·133 lines (114 loc) · 3.51 KB
/
fetch_package_files.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
#!/usr/bin/env node
// ./fetch_package_files <file> <output> < input
// ./fetch_package_files "README.md" "./output" < input
const fs = require('fs');
const path = require('path');
const async = require('async');
const mkdirp = require('mkdirp');
const request = require('request');
const _ = require('lodash');
if (require.main === module) {
main();
} else {
module.exports = fetchPackageFiles;
}
function main() {
const file = process.argv[2];
const output = process.argv[3];
if(!file) {
return console.error('Missing file!');
}
if(!output) {
return console.error('Missing output!');
}
mkdirp.sync(output);
const stdin = process.openStdin();
var input = '';
stdin.setEncoding('utf8');
stdin.on('data', function(d) {
input += d;
});
stdin.on('end', function() {
fetchPackageFiles({
input: JSON.parse(input),
file: file,
output: path.resolve(process.cwd(), output),
limit: 4
}, function(err, d) {
if (err) {
return console.error(err);
}
const msg = d.length === 0
? 'Fetched 0 files'
: d.length === 1
? 'Fetched 1 file: '
: `Fetched ${d.length} files: `;
console.log(msg + _.map(d, 'full_name'));
});
});
}
function fetchPackageFiles(options, finalCb) {
const file = options.file;
async.mapLimit(
options.input,
options.limit,
function(pkg, cb) {
const branch = 'master';
const url = ['https://raw.githubusercontent.com', pkg.full_name, branch, file].join('/');
request(url, function(err, response, body) {
if (err) {
return cb(err);
}
if (body && file === 'README.md') {
body = body
.replace(/^[^]*?<\/h1>/m, '') // drop everything up to first </h1>
.replace(/https?:\/\/github.com\/(webpack|webpack-contrib)\/([-A-za-z0-9]+-loader\/?)([)"])/g, '/loaders/$2/$3') // modify loader links
.replace(/https?:\/\/github.com\/(webpack|webpack-contrib)\/([-A-za-z0-9]+-plugin\/?)([)"])/g, '/plugins/$2/$3') // modify plugin links
.replace(/<h2[^>]*>/g, '## ') // replace any <h2> with ##
.replace(/<\/h2>/g, '') // drop </h2>
.replace(/<!--[\s\S]*?-->/g, ''); // drop comments
}
var title = pkg.name;
if (title.match(/-plugin$/)) {
title = _.camelCase(title);
title = _.upperFirst(title);
title = title.replace(/I18N/, 'I18n');
}
// TODO: push this type of stuff to a script of its own to keep this standard
let headmatter = yamlHeadmatter({
title: title,
source: url,
edit: [pkg.html_url, 'edit', branch, file].join('/'),
repo: pkg.html_url
});
return async.parallel(
[
fs.writeFile.bind(null,
path.resolve(options.output, pkg.name + path.extname(file)),
headmatter + body
),
fs.writeFile.bind(null,
path.resolve(options.output, pkg.name + '.json'),
JSON.stringify(pkg, null, 2)
)
],
function(err) {
if (err) {
return cb(err);
}
return cb(null, pkg);
}
);
});
},
finalCb
);
}
// TODO: push this type of to a script of its own to keep this generic
function yamlHeadmatter(fields) {
var ret = '---\n';
Object.keys(fields).forEach(function(field) {
ret += field + ': ' + fields[field] + '\n';
});
return ret + '---\n';
}