-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.ts
executable file
·76 lines (67 loc) · 2.36 KB
/
index.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env ts-node
import meow = require('meow');
import renderMarkdown, { renderString } from './src/termd';
import { getMarkdownFromUrl, githubReadmeUrl, npmPackageUrl } from './src/utils';
const cli = meow(`
Usage
$ termd <filename>
Options
--string, -s Use a string with markdown syntax
--url, -u Render markdown from url in the terminal
--npm, -n Render npm package readme in the terminal
--github, -g Render github repository readme in the terminal
Examples
$ termd readme.md // # Heading 1
${renderString('# Heading 1')}
$ termd -s "# Heading 1"
${renderString('# Heading 1')}
$ termd --url="https://gist.githubusercontent.com/dephraiim/..."
${renderString('# Heading 1')}
$ termd -n termd
...
$ termd --github="dephraiim/termd"
...
`);
try {
if (cli.input[0]) {
console.log(renderMarkdown(cli.input[0]));
}
if (cli.flags.s || cli.flags.string) {
let optionString = (cli.flags.s as string) || (cli.flags.string as string);
console.log(renderString(optionString));
}
if (cli.flags.u || cli.flags.url) {
let url = (cli.flags.u as string) || (cli.flags.url as string);
getMarkdownFromUrl(url)
.then((string) => console.log(renderString(string)))
.catch((e) => {
throw new Error(e);
});
}
if (cli.flags.n || cli.flags.npm) {
let packageName = (cli.flags.n as string) || (cli.flags.npm as string);
let packageUrl = npmPackageUrl(packageName);
getMarkdownFromUrl(packageUrl)
.then((data) => data.readme)
.then((readme) => console.log(renderString(readme)))
.catch((e) => {
throw new Error(e);
});
}
if (cli.flags.g || cli.flags.github) {
let repoName = (cli.flags.github as string) || (cli.flags.g as string);
let repoUrl = githubReadmeUrl(repoName);
getMarkdownFromUrl(repoUrl)
.then((data) => data.content)
.then((data64) => {
let buffer = Buffer.from(data64, 'base64');
return buffer.toString('utf-8');
})
.then((readme) => console.log(renderString(readme)))
.catch((e) => {
throw new Error(e);
});
}
} catch (error) {
console.log(error.message);
}