-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.js
132 lines (112 loc) · 3.39 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
127
128
129
130
131
132
const path = require('path');
const fs = require('extra-fs');
const build = require('extra-build');
const lunr = require('lunr');
const columns = require('@ifct2017/columns');
const owner = 'ifct2017';
const repo = build.readMetadata('.').name.replace(/@.+\//g, '');
const OVERRIDE = new Map([
['tocpha', 'vitamin-e'],
['tocphd', 'vitamin-e'],
['toctra', 'vitamin-e'],
['toctrd', 'vitamin-e'],
['crypxb', 'carotenoid'],
['cartg', 'carotenoid'],
['carta', 'carotenoid'],
['cartb', 'carotenoid'],
]);
function matchIndex(index, txt) {
var a = [], txt = txt.replace(/\W/g, ' ');
var ms = index.search(txt), max = 0;
for (var m of ms)
max = Math.max(max, Object.keys(m.matchData.metadata).length);
for (var m of ms) {
var keys = Object.keys(m.matchData.metadata).length;
if (keys<max) continue;
if (!m.ref.includes('+') && max<m.ref.split('-').length) continue;
a.push(m);
}
return a;
}
function readAssets() {
var a = new Map();
for (var f of fs.readdirSync('assets')) {
var pth = path.join('assets', f);
var code = f.replace('.txt', '');
var tags = code.replace(/\W+/g, ' ');
var desc = fs.readFileTextSync(pth).replace(/[\n\s]+$/, '');
a.set(code, {code, tags, desc});
}
return a;
}
function indexAssets(map) {
return lunr(function() {
this.ref('code');
this.field('tags');
this.pipeline.remove(lunr.stopWordFilter);
for (var r of map.values())
this.add(r);
});
}
function createTable(index) {
var a = new Map();
for (var c of columns.load().values()) {
var cname = c.name.replace(/\W+/g, ' ').toLowerCase();
var tags = `${c.code} ${c.code} ${c.code} ${cname} ${cname} ${c.tags}`;
var ms = matchIndex(index, tags);
// if (c.code==='cartb') console.log(tags, ms);
if (ms.length===0) console.log('Skipped:', c.name, tags, ms);
if (ms.length===0) continue;
var code = OVERRIDE.get(c.code)||ms[0].ref, r = a.get(code);
// console.log(`${c.code} -> ${code}`);
a.set(c.code, code);
}
return a;
}
function writeCorpus(assets, table) {
var a = '';
for (var [k, v] of assets)
a += `const ${k.replace(/\W+/g, '_')} = ${JSON.stringify(v.desc)};\n`;
a += `const CORPUS = new Map([\n`;
for (var [k, v] of table)
a += ` ["${k}", ${v.replace(/\W+/g, '_')}],\n`;
a += `]);\n`;
a += `module.exports = CORPUS;\n`;
fs.writeFileTextSync('corpus.js', a);
}
function generateCorpus() {
var assets = readAssets();
var index = indexAssets(assets);
var table = createTable(index);
writeCorpus(assets, table);
}
// Publish a root package to NPM, GitHub.
function publishRootPackage(ver) {
var _package = build.readDocument('package.json');
var m = build.readMetadata('.');
m.version = ver;
build.writeMetadata('.', m);
build.publish('.');
try { build.publishGithub('.', owner); }
catch {}
build.writeDocument(_package);
}
// Publish root, sub packages to NPM, GitHub.
function publishPackages() {
var m = build.readMetadata('.');
var ver = build.nextUnpublishedVersion(m.name, m.version);
generateCorpus();
publishRootPackage(ver);
}
// Publish docs.
function publishDocs() {
var m = build.readMetadata('.');
build.updateGithubRepoDetails({owner, repo, topics: m.keywords});
}
// Finally.
function main(a) {
if (a[2]==='publish-docs') publishDocs();
else if (a[2]==='publish-packages') publishPackages();
else generateCorpus();
}
main(process.argv);