-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
135 lines (113 loc) · 3.14 KB
/
index.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
'use strict';
const got = require('got');
const cheerio = require('cheerio');
const table = require('columnify');
const chalk = require('chalk');
const wrap = require('wordwrap')(90);
const open = require('open');
const SEARCH_URL = {
js: 'JavaScript/Reference/Global_Objects',
css: 'CSS'
};
const getBaseUrl = locale => `https://developer.mozilla.org/${locale}/docs/Web`;
const format = (markup, url) => {
const $ = cheerio.load(markup);
const title = $('#wiki-document-head h1').text();
const method = title.split('.').pop();
const methodWithoutParens = method.replace(/\(\)/, '');
const description = $('#wikiArticle > p')
.filter((index, element) => $(element).text().length !== 0)
.first()
.text()
.replace(title, chalk.bold(title))
.replace(method, chalk.bold(method));
const usage = $('#Syntax')
.next('pre')
.text()
.trim()
.split(/\n/)
.map((line, index) => {
return `\t${index} | ${line}\n`;
})
.join('')
.replace(new RegExp(methodWithoutParens, 'gim'), chalk.underline(methodWithoutParens));
const api = [];
console.log(`\n${chalk.bold(title)}`);
console.log(`\n${wrap(description)}\n`);
if (/[a-z]/.test(usage)) {
console.log(`${usage}\n`);
}
$('#wikiArticle dl dt')
.has('code')
.each((index, element) => {
const $element = $(element);
const isNested = $element.parent().parent().is('dd');
const term = $element
.find('code')
.contents()
.not('span')
.text()
.replace(/^/, isNested ? ' ' : '');
const definition = $element
.next('dd')
.contents()
.not('dl')
.text()
.replace(new RegExp(term.trim(), 'gim'), chalk.bold(term.trim()));
api.push({
term: chalk.bold(term),
definition
});
api.push({term: '', definition: ''});
});
console.log(table(api, {
showHeaders: false,
config: {
term: {
minWidth: 15
},
definition: {
maxWidth: 65
}
}
}));
const returnValue = $('#Return_value + p').text();
if (returnValue.length > 0) {
const recased = returnValue[0].toLowerCase() + returnValue.substr(1);
console.log(`\n${wrap(chalk.bold('Returns ') + recased)}\n`);
}
console.log(`${chalk.dim(url)}`);
};
const fetch = (keyword, language, shouldOpen, locale) => {
const baseUrl = getBaseUrl(locale);
const parts = keyword.replace(/prototype\./, '').split('.');
const url = `${baseUrl}/${SEARCH_URL[language]}/${parts[0]}/${parts[1] || ''}`;
const options = {
headers: {
'user-agent': 'https://github.com/rafaelrinaldi/mdn'
}
};
if (shouldOpen) {
return new Promise(resolve => {
resolve(open(url));
});
}
return got(url, options)
.then(response => {
format(response.body, url);
})
.catch(error => {
if (error.statusCode === 404) {
console.error(`"${keyword}" not found for language "${language}"`);
} else {
console.error(error.stack);
}
process.exit(1);
});
};
module.exports = options => fetch(
options.keyword,
options.language,
options.shouldOpen,
options.locale
);