-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
287 lines (244 loc) · 8.7 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const os = require('os');
const {child_process: cp, fs, path} = require('extra-build');
const {git, github, package} = require('extra-build');
const {javascript, jsdoc, markdown} = require('extra-build');
const owner = 'nodef';
const srcts = 'index.ts';
const outjs = 'index.js';
const outmjs = 'index.mjs';
const outdts = 'index.d.ts';
// Is given file a submodule?
function isSubmodule(pth) {
if (/^_|index.ts$/.test(pth)) return false;
if (!/\.ts$/.test(pth)) return false;
return true;
}
// Get filename keywords for main/sub package.
function filenameKeywords(fil) {
if (fil !== srcts) return [path.symbolname(fil)];
return fs.readdirSync('src').filter(isSubmodule).map(path.keywordname);
}
// Get export keywords for main/sub package.
function exportKeywords(fil) {
var txt = fs.readFileTextSync(`src/${fil}`);
var exps = javascript.exportSymbols(txt);
return exps.map(e => path.symbolname(e.name));
}
// Get keywords for main/sub package.
function keywords(fil) {
var m = package.read('.');
var s = new Set([...m.keywords, ...filenameKeywords(fil), ...exportKeywords(fil)]);
return Array.from(s);
}
// Update GitHub details.
function updateGithub() {
var m = package.read('.');
var {name, description} = m;
var homepage = `https://www.npmjs.com/package/${name}`;
var name = name.replace('@', '').replace('/', '--');
var topics = keywords(srcts).map(path.keywordname).filter(k => k.length<=35);
github.updateDetails(owner, name, {description, homepage, topics});
}
// Webify output files.
function webifyMain(sym) {
cp.execLogSync(`browserify "${outjs}" -o "${outjs}.1" -s ${sym}`);
cp.execLogSync(`cp "${outmjs}" "${outmjs}.1"`);
cp.execLogSync(`terser "${outjs}.1" -o "${outjs}" -c -m`);
cp.execLogSync(`terser "${outmjs}.1" -o "${outmjs}" -c -m`);
cp.execLogSync(`rm -f "${outjs}.1"`);
cp.execLogSync(`rm -f "${outmjs}.1"`);
}
// Generate main output files.
function generateMain(fil, sym) {
var bld = fil.replace(/\.ts/, '.js');
var env = sym? ` --environment TYPE:web` : '';
cp.execLogSync(`rollup -c rollup.config.js -i .build/${bld}` + env);
if (sym) webifyMain(sym);
}
// Publish root package to NPM, GitHub.
function publishRoot(sym, ver) {
fs.restoreFileSync('package.json', () => {
var m = package.read();
m.version = ver;
m.keywords = keywords(srcts);
if (sym) { m.name += '.web'; }
fs.restoreFileSync('README.md', () => {
var txt = fs.readFileTextSync('README.md');
if (sym) txt = txt.replace(/\[Files\]\((.*?)\/\)/g, '[Files]($1.web/)');
fs.writeFileTextSync('README.md', txt);
package.write('.', m);
package.publish('.');
try { package.publishGithub('.', owner); }
catch {}
});
});
}
// Deploy root package to NPM, GitHub.
function deployRoot(ver) {
var m = package.read();
var sym = path.symbolname(m.name);
generateMain(srcts, '');
publishRoot('', ver);
generateMain(srcts, sym);
publishRoot(sym, ver);
}
// Deploy root, sub packages to NPM, GitHub.
function deployAll() {
var m = package.read();
var ver = package.nextUnpublishedVersion(m.name, m.version);
cp.execLogSync(`tsc`);
updateGithub();
deployRoot(ver);
}
// Get markdown for JSDoc symbol.
function jsdocSymbolMarkdown(sym, pre, repo) {
var x = jsdoc.parse(sym.jsdoc);
var nam = pre? `${pre}.${sym.name}` : sym.name;
var pkg = pre? `@${repo}/${sym.name}` : repo;
var sig = `${nam}(${x.params.map(p => p.name).join(', ')})`;
var len = Math.max(...x.params.map(p => p.name.length)) + 2;
var par = x.params.map(p => `// ${(p.name+':').padEnd(len, ' ')}${p.description}`).join('\n');
return `${x.description}<br>\n` +
`📦 [NPM](https://www.npmjs.com/package/${pkg}),\n` +
`🌐 [Web](https://www.npmjs.com/package/${pkg}.web),\n` +
`📜 [Files](https://unpkg.com/${pkg}/),\n` +
`📰 [Docs](https://nodef.github.io/${repo}/).\n\n` +
`> Similar: [${nam}].\n\n` +
`<br>\n\n` +
'```javascript\n' +
`${sig};\n` +
`${par}\n` +
'```\n\n' +
'```javascript\n' +
`const ${nam} = require("${repo}");\n\n` +
`${nam}(...);\n` +
`// → OUTPUT\n` +
'```\n\n' +
'<br>\n' +
'<br>\n\n\n' +
`## References\n\n` +
`- [Example](https://www.example.com/)\n\n` +
`[${nam}]: https://github.com/${owner}/${repo}/wiki/${nam}\n`
}
// Process each source file.
function forEachSourceFile(fn) {
for (var f of fs.readdirSync('src')) {
if (f.startsWith('_')) continue;
var txt = fs.readFileTextSync(`src/${f}`);
var exps = javascript.exportSymbols(txt);
var docs = javascript.jsdocSymbols(txt);
var dmap = new Map(docs.map(x => [x.name, x]));
fn(f, exps, dmap);
}
}
// Create empty wiki files for all exported symbols.
function createWikiFiles() {
forEachSourceFile((f, exps) => {
var nam = f.replace(/\..*/, '');
var pre = f === 'index.ts'? '' : nam;
for (var e of exps) {
var out = `wiki/${pre}${e.name}.md`;
if (fs.existsSync(out)) continue;
fs.writeFileTextSync(out, '');
}
});
}
// Generate wiki file text for all exported symbols.
function generateWikiFiles() {
var m = package.read('.');
forEachSourceFile((f, exps, dmap) => {
var nam = f.replace(/\..*/, '');
var pre = f === 'index.ts'? '' : nam;
for (var e of exps) {
var out = `wiki/${pre}${e.name}.md`;
if (!fs.existsSync(out)) continue;
if (fs.readFileTextSync(out).length > 0) continue;
if (!dmap.has(e.name)) continue;
var md = jsdocSymbolMarkdown(dmap.get(e.name), pre, m.name);
fs.writeFileTextSync(out, md);
}
});
}
// Generate wiki for all exported symbols.
function generateWiki() {
createWikiFiles();
generateWikiFiles();
}
// Update index table for README, wiki.
function updateMarkdownIndex(rkind) {
forEachSourceFile((f, exps, dmap) => {
var nam = f.replace(/\..*/, '');
var pre = f === 'index.ts'? '' : nam;
var out = pre? `wiki/${nam}.md` : 'README.md';
if (!fs.existsSync(out)) return;
var txt = fs.readFileTextSync(out);
txt = markdown.replaceTables(txt, (full, rows) => {
if (rows.length < 1 || rows[0].length < 2) return full;
rows = rows.map(r => [r[0].trim(), r[1].trim()]);
if (!/property/i.test(rows[0][0])) return full;
if (!/description/i.test(rows[0][1])) return full;
var rmap = new Map(rows.map((r, i) => [r[0], i]));
for (var e of exps) {
if (!dmap.has(e.name)) continue;
if (!rkind.test(e.kind)) continue;
var key = `[${e.name}]`;
var val = jsdoc.parse(dmap.get(e.name).jsdoc).description.replace(/\n+/g, ' ').trim();
if (!rmap.has(key)) rows.push([key, val]);
else rows[rmap.get(key)][1] = val;
}
var top = '| ' + rows[0].join(' | ') + ' |\n';
var mid = '| ' + rows[0].map(r => ` ---- `).join(' | ') + ' |\n';
var bot = rows.slice(1).map(r => '| ' + r.join(' | ') + ' |\n').join('');
return top + mid + bot;
});
fs.writeFileTextSync(out, txt);
});
}
// Get docs link reference for jsdoc symbol.
function docsLinkReference(sym, pre, repo) {
var d = jsdoc.parse(sym.jsdoc);
var root = `https://${owner}.github.io/${repo}`;
var name = sym.name;
var pred = pre? `${pre}.` : '';
var prem = pre? `modules/${pre}.html` : 'modules.html';
switch (d.kind) {
case 'interface': return `[${name}]: ${root}/interfaces/${pred}${name}.html`;
case 'class': return `[${name}]: ${root}/classes/${pred}${name}.html`;
default: return `[${name}]: ${root}/${prem}#${name}`;
}
}
// Update link references for README, wiki.
function updateMarkdownLinkReferences() {
var m = package.read('.');
forEachSourceFile((f, exps, dmap) => {
var nam = f.replace(/\..*/, '');
var pre = f === 'index.ts'? '' : nam;
var out = pre? `wiki/${nam}.md` : 'README.md';
if (!fs.existsSync(out)) return;
var txt = fs.readFileTextSync(out);
txt = markdown.replaceLinkReferences(txt, (full, name) => {
if (!dmap.has(name)) return full;
return docsLinkReference(dmap.get(name), pre, m.name);
});
var lset = new Set(markdown.links(txt).filter(x => !x.url).map(x => x.ref || x.name));
var rset = new Set(markdown.linkReferences(txt).map(x => x.name));
for (var l of lset) {
if (rset.has(l)) continue;
if (!dmap.has(l)) continue;
txt += docsLinkReference(dmap.get(l), pre, m.name) + '\n';
}
fs.writeFileTextSync(out, txt);
});
}
// Update markdowns README, wiki.
function updateMarkdown() {
updateMarkdownIndex(/const|class|(async\s+)?function\*?/);
updateMarkdownLinkReferences();
}
function main(a) {
if (a[2] === 'deploy') deployAll();
else if (a[2] === 'wiki') generateWiki();
else if (a[2] === 'markdown') updateMarkdown();
else generateMain(srcts, '');
}
main(process.argv);