-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
233 lines (186 loc) · 6.08 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
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_GEX = 'https://github.com/tj/git-extras/blob/master/Commands.md#git-${name}';
const HELP_URL_GEC = 'https://github.com/unixorn/git-extra-commands/blob/master/README.md';
const HELP_URL_GEI = 'https://github.com/nodef/extra-git/wiki/${name}';
const RDESC_GEX_ETC = /^\s+\"([\w-]+):\s*(.*?)\"(?:\s*\\)$/gm;
const RDESC_GEX_MAN = /^\s+-\s+\*\*([\w-]+)\(\d\)\*\*\s(.*)$/gm;
const RDESC_GEC = /^\| +`([^`\s=]+)[^`]*`\s+(?:\|[^|]+)?\| *([^\n]*) *\|$/gm;
const RNOT_TEXT = /[^\w\s\'\(\)-\/]|\s*\.?\s*$/g;
const RSHE_BANG = /^(#!\s*\S+\s+\S+)\n*/;
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 fetchSuper(url) {
var cwd = fs.mkdtempSync('temp-');
cp.execSync(`git clone ${url} .`, {cwd});
return cwd;
}
function cleanSuper(dir) {
cp.execSync(`rm -rf "${dir}"`);
}
function readDescRe(re, pth) {
var d = readFile(pth), m, a = new Map();
while ((m=re.exec(d)) != null)
a.set(m[1].replace(/^git-/g, ''), m[2].replace(RNOT_TEXT, '')+'.');
return a;
}
function readDescGex(dir) {
var etc = readDescRe(RDESC_GEX_ETC, `${dir}/etc/git-extras.fish`);
var man = readDescRe(RDESC_GEX_MAN, `${dir}/man/git-extras.md`);
return new Map([...etc, ...man]);
}
function readDesc(dir, name) {
if (name === 'git-extras') return readDescGex(dir);
return readDescRe(RDESC_GEC, `${dir}/README.md`);
}
function readDescBin() {
var a = new Map();
for (var f of fs.readdirSync('bin')) {
var d = readFile(`bin/${f}`);
var name = f.replace(/\..*/, '');
var desc = d.match(/^##\s*(.*)$/m)[1];
a.set(name, desc);
}
return a;
}
function copyBin(dir, desc, msg) {
fs.mkdirSync('bin', {recursive: true});
for (var f of fs.readdirSync(dir)) {
if (f === 'git-extras') continue;
var g = f.replace(/^git-/g, '');
if (fs.existsSync(`bin/${g}.sh`)) continue;
var d = readFile(`${dir}/${f}`);
var c = desc.get(g)||'';
d = d.replace(RSHE_BANG, `$1\n## ${c}\n${msg}\n\n`)
writeFile(`bin/${g}.sh`, d);
}
}
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 === 'git-extras.md') continue;
if (f === 'Readme.md') continue;
if (f === 'Home.md') continue;
var g = f.replace(/^git-|\.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 copyLicense(pth, name) {
cp.execSync(`cp -f "${pth}" "LICENSE-${name}"`);
}
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(gei, gex, gec) {
var a = '';
var names = [...gei.keys(), ...gex.keys(), ...gec.keys()].sort();
for (var name of names) {
var desc = gei.get(name)||gex.get(name)||gec.get(name);
a += `| [${name}] | ${stringLimit(desc, HELP_DESC_SIZE)} |\n`;
}
a += `\n\n`;
for (var name of gec.keys())
a += `[${name}]: ${HELP_URL_GEC.replace('${name}', name)}\n`;
for (var name of gex.keys())
a += `[${name}]: ${HELP_URL_GEX.replace('${name}', name)}\n`;
for (var name of gei.keys())
a += `[${name}]: ${HELP_URL_GEI.replace('${name}', name)}\n`;
return a;
}
function readKeywords(gei, gex, gec) {
var a = '';
var names = [...gei.keys(), ...gex.keys(), ...gec.keys()].sort();
for (var name of names) {
var desc = gei.get(name)||gex.get(name)||gec.get(name);
a += `| [${name}] | ${stringLimit(desc, HELP_DESC_SIZE)} |\n`;
}
a += `\n\n`;
for (var name of gec.keys())
a += `[${name}]: ${HELP_URL_GEC.replace('${name}', name)}\n`;
for (var name of gex.keys())
a += `[${name}]: ${HELP_URL_GEX.replace('${name}', name)}\n`;
for (var name of gei.keys())
a += `[${name}]: ${HELP_URL_GEI.replace('${name}', name)}\n`;
return a;
}
function copy(url, f=true) {
var name = url.replace(/.*\//, '');
var msg = `## Source: ${name}`;
var dir = fetchSuper(url);
var desc = readDesc(dir, name);
if (f) copyBin(`${dir}/bin`, desc, msg);
if (f) copyMan(`${dir}/man`);
if (f) copyLicense(`${dir}/LICENSE`, name);
cleanSuper(dir);
return desc;
}
function replaceSymlinks(dir) {
for (var f of fs.readdirSync(dir, {withFileTypes: true})) {
if (!f.isSymbolicLink()) continue;
var pth = path.join(dir, f.name);
var txt = readFile(pth);
fs.unlinkSync(pth);
writeFile(pth, txt);
}
}
function makeExec() {
cp.execSync(`chmod +x bin/*`);
cp.execSync(`chmod +x index.sh`);
}
function main(f=true) {
var gei = readDescBin();
var gex = copy('https://github.com/tj/git-extras', f);
var gec = copy('https://github.com/unixorn/git-extra-commands', f);
if (f) copyMan('wiki');
if (f) writeFile('man/help.txt', readHelp());
if (f) replaceSymlinks('bin');
writeFile('index.log', readIndex(gei, gex, gec));
var p = readJson('package.json');
p.keywords = [...new Set([...p.keywords, ...gei.keys(), ...gec.keys(), ...gex.keys()])];
writeJson('package.json', p);
makeExec();
}
main(process.argv[2] !== 'local');