-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp.ts
58 lines (48 loc) · 1.87 KB
/
help.ts
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
import chalk, { Chalk } from 'chalk';
import fse from 'fs-extra';
const { existsSync, readFileSync } = fse;
import { dirname, join } from 'path';
import terminalLink from 'terminal-link';
import { fileURLToPath } from 'url';
import { job as version } from './version.js';
import { info } from '../logger.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const versionDef = {
name: 'help',
spec: {},
'default': {},
options: (_: { _: unknown[] }): Record<string, unknown> => ({}),
questions: (_: { [_: string]: unknown }): Record<string, unknown>[] => []
};
export const def = versionDef;
export function job(): void {
version();
info('');
showHelp();
}
function showHelp() {
const manualPath = join(__dirname, '../../USAGE');
if (!existsSync(manualPath)) {
throw new Error(`Unable to find the usage file: ${manualPath}`);
}
const content = readFileSync(manualPath, { encoding: 'utf8' });
const usage = content
.replace(/^(.*)$/m, (m: string) => {
return chalk.bold(m);
})
.replace(/(?<!`)`([^`]+)`(?!`)/gm, '%%%yellow:$1%%%')
.replace(/(?<!\*)\*{3}([^*]+)\*{3}(?!\*)/gm, '%%%bold.italic:$1%%%')
.replace(/%%%([A-Za-z.]*):(.*?)%%%/gm, (_match, property: string, value: string, _offset, _source): string => {
// styles provided with first parameter are taken in account as it
const styles: string[] = property.split('.');
const styling = styles.reduce((o: Chalk, i: string) => {
return (o as unknown as {[key: string]: Chalk})[i];
}, chalk);
return styling(value);
})
.replace(/(?<!\[)\[(.*?)\]\((.*?)\)/gm, (_match, title: string, url: string, _offset, _source) => {
const link = terminalLink(title, url);
return chalk.blue(link);
});
info(usage);
}