forked from Briles/lodash-completions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
126 lines (98 loc) · 3.54 KB
/
build.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
(function () {
'use strict';
const cheerio = require('cheerio');
const commander = require('commander');
const fs = require('fs');
const https = require('https');
const marked = require('marked');
commander
.version('1.0.0')
.usage('Generates completions for lodash')
.option('-t --tag [tag]', 'lodash version to fetch (must be valid git tag)', 'master')
.option('-n --namespace [namespace]', 'namespace to use in place of `_`', 'ld')
.parse(process.argv);
const isFunctionRe = new RegExp('\\(.*\\)');
const hasParamsRe = new RegExp('\\([^)]+\\)');
const replacee = '~#~';
const configurations = [{
triggerPrefix: commander.namespace,
completionPrefix: '_',
}, {
triggerPrefix: 'c' + commander.namespace,
completionPrefix: '',
}, ];
var writeCompletions = function (filename, contents) {
filename = filename.toLowerCase();
var destPath = `${__dirname}/completions/${filename}.sublime-completions`;
fs.writeFile(destPath, JSON.stringify(contents, null, 4), function (err) {
if (err) {
return console.log(err);
}
console.log(`Saved "${destPath}"`);
});
};
var parse = function (html) {
var $ = cheerio.load(html);
$('h2').each(function () {
if ($(this).next().is('h3')) {
var completionsData = {
scope: 'source.js, source.coffee',
completions: [],
};
var group = $(this).text().replace(/(“|” Methods)/gi, '').trim();
$(this).nextUntil('h2', 'h3').each(function () {
var base = $(this).text().replace(/\(.*\)/, `(${replacee})`).trim().slice(1);
var isFunction = isFunctionRe.test(base);
var hasParams = hasParamsRe.test($(this).text());
var parameters = [];
if (hasParams) {
$(this).nextUntil('ol').next().children('li').each(function (i) {
i += 1;
parameters.push('${' + i + ':' + $(this).children('code').first().text() + '}');
});
parameters = parameters.join(', ');
}
var aliases = [base];
$(this).nextUntil('h3', 'h4').each(function () {
if ($(this).text().trim() === 'Aliases' && $(this).next().is('p')) {
aliases.push(...($(this).next().text().split(',').map(function (alias) {
alias = alias.trim();
if (alias.slice(0, 1) !== '_') {
alias = '_' + alias;
}
return (isFunction ? alias + `(${replacee})` : alias).trim().slice(1);
})));
}
});
aliases.forEach(function (alias) {
var trigger = alias.replace(replacee, '');
var contents = alias.replace(replacee, parameters);
console.log(trigger);
configurations.forEach(function (config) {
var completion = {
trigger: `${config.triggerPrefix + trigger}\t _ ${group}`,
contents: `${config.completionPrefix + contents}$0`,
};
completionsData.completions.push(completion);
});
});
});
writeCompletions(group, completionsData);
}
});
};
(function () {
var url = `https://raw.githubusercontent.com/lodash/lodash/${commander.tag}/doc/README.md`;
https.get(url, (res) => {
var body = '';
res.on('data', (d) => {
body += d;
});
res.on('end', function () {
parse(marked(body));
});
}).on('error', (e) => {
console.error(e);
});
}());
}());