-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathfetch_packages.js
202 lines (177 loc) · 5.46 KB
/
fetch_packages.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
#!/usr/bin/env node
// ./fetch_package_names <suffix> > output
// ./fetch_package_names "-loader" > output.json
const GitHubApi = require('github');
const fs = require('fs');
const urlModule = require('url');
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 = fetchPackageNames;
}
function main() {
const organization = process.argv[2];
const suffix = process.argv[3];
const file = process.argv[4];
const output = process.argv[5];
if (!organization) {
return console.error('Missing organization!');
}
if (!suffix) {
return console.error('Missing suffix!');
}
if (!file) {
return console.error('Missing file!');
}
if (!output) {
return console.error('Missing output!');
}
mkdirp.sync(output);
fetchPackageNames(
{
organization: organization,
suffix: suffix
},
function(err, d) {
if (err) {
return console.error(err);
}
fetchPackageFiles(
{
input: d,
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 fetchPackageNames(options, cb) {
const github = new GitHubApi();
if (process.env.GITHUB_TOKEN) {
github.authenticate({
type: 'token',
token: process.env.GITHUB_TOKEN
});
github.misc.getRateLimit({}, (err, res) => {
if (err) throw err;
console.log(res.data.rate);
});
}
// XXX: weak since this handles only one page
github.repos.getForOrg(
{
org: options.organization,
per_page: 100
},
function(err, d) {
if (err) {
return cb(err);
}
return cb(
null,
d.data.filter(function(o) {
return o.name.endsWith(options.suffix);
})
);
}
);
}
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.toLowerCase() === 'readme.md') {
body = body
// Remove all but the description from lead-in (XXX: Optional `\/?` should be unnecessary)
.replace(/[^]*?<div align="center">([^]*?)<\/div>/, (match, content) => {
let parsed = content.match(/<p>([^]*?)<\/?p>/);
return parsed ? parsed[1] : '';
})
// Replace lone h1 formats
.replace(/<h1.*?>.+?<\/h1>/, '')
.replace(/# .+/, '')
// Resolve anchor hrefs to avoid broken relative references in the docs
// Examples:
// - [click here](LICENSE) --> [click here](https://raw.githubusercontent.com/user/repository/branch/LICENSE)
// - [click here](./LICENSE) --> [click here](https://raw.githubusercontent.com/user/repository/branch/LICENSE)
// - [click here](#LICENSE) --> [click here](#LICENSE)
.replace(
/\[([^[\]]*)\]\(([^)#]+)\)/g,
(match, textContent, href) => `[${textContent}](${urlModule.resolve(url, href)})`
)
// Modify links to keep them within the site
.replace(
/https?:\/\/github.com\/(webpack|webpack-contrib)\/([-A-za-z0-9]+-loader\/?)([)"])/g,
'/loaders/$2/$3'
)
.replace(
/https?:\/\/github.com\/(webpack|webpack-contrib)\/([-A-za-z0-9]+-plugin\/?)([)"])/g,
'/plugins/$2/$3'
)
// Replace any <h2> with `##`
.replace(/<h2[^>]*>/g, '## ')
.replace(/<\/h2>/g, '')
// Drop any comments
.replace(/<!--[\s\S]*?-->/g, '');
}
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';
}