-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
132 lines (98 loc) · 2.82 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 fs = require('fs');
const os = require('os');
const cp = require('child_process');
const path = require('path');
const htmlEntities = require('html-entities');
const markdownToText = require('markdown-to-text').default;
const HELP_URL = 'https://github.com/nodef/extra-npm/wiki/${name}';
const HELP_NAME_SIZE = 26;
const HELP_DESC_SIZE = 64;
function stringLimit(x, n) {
return x.length <= n? x : x.substring(0, n-4)+' ...';
}
function stringAlign(x, n, s) {
var a = '';
for (var i=0; i<x.length; i+=n) {
if (a) a += '\n'+' '.repeat(s);
a += x.substring(i, i+n);
}
return a;
}
function readFile(f) {
var d = fs.readFileSync(f, 'utf8');
return d.replace(/\r?\n/g, '\n');
}
function writeFile(f, d) {
d = d.replace(/\r?\n/g, os.EOL);
fs.writeFileSync(f, d);
}
function readJson(pth) {
var pth = pth||'package.json';
var d = readFile(pth)||'{}';
return JSON.parse(d);
}
function writeJson(pth, v) {
var pth = pth||'package.json';
var d = JSON.stringify(v, null, 2)+'\n';
writeFile(pth, d);
}
function readDescBin() {
var a = new Map();
for (var f of fs.readdirSync('bin')) {
if (/^_/.test(f)) continue;
var d = readFile(`bin/${f}`);
var name = f.replace(/\..*/, '');
var desc = d.match(/^(?:##|\/\/)\s*(.*)$/m)[1];
a.set(name, desc);
}
return a;
}
function readHelp() {
var a = '', ns = HELP_NAME_SIZE, ds = HELP_DESC_SIZE;
for (var [name, desc] of readDescBin())
a += ` ${name.padEnd(ns)} ${stringAlign(desc, ds, 2+ns)}\n`;
var d = readFile('man/help.txt');
return d.replace('${commands}', a);
}
function readIndex(descs) {
var a = '';
var names = [...descs.keys()].sort();
for (var name of names) {
var desc = descs.get(name);
a += `| [${name}] | ${stringLimit(desc, HELP_DESC_SIZE)} |\n`;
}
a += `\n\n`;
for (var name of descs.keys())
a += `[${name}]: ${HELP_URL.replace('${name}', name)}\n`;
return a;
}
function copyMan(dir) {
fs.mkdirSync('man', {recursive: true});
if (!fs.existsSync(dir)) return;
for (var f of fs.readdirSync(dir)) {
if (path.extname(f) !== '.md') continue;
if (f === 'Readme.md') continue;
if (f === 'Home.md') continue;
var g = f.replace(/\.md$/g, '');
if (fs.existsSync(`man/${g}.txt`)) continue;
var d = readFile(`${dir}/${f}`);
d = markdownToText(d);
d = htmlEntities.decode(d);
writeFile(`man/${g}.txt`, d);
}
}
function makeExec() {
cp.execSync(`chmod +x bin/*`);
cp.execSync(`chmod +x index.sh`);
}
function main(f=true) {
var descs = readDescBin();
if (f) copyMan('wiki');
if (f) writeFile('man/help.txt', readHelp());
writeFile('index.log', readIndex(descs));
var p = readJson('package.json');
p.keywords = [...new Set([...p.keywords, ...descs.keys()])];
if (f) writeJson('package.json', p);
if (f) makeExec();
}
main(process.argv[2] !== 'local');