-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·77 lines (66 loc) · 1.52 KB
/
index.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
#!/usr/bin/env node
import jsonToTs from 'json-to-ts'
import fetch from 'node-fetch'
import meow from 'meow'
import clipboard from 'clipboardy'
import chalk from 'chalk'
import cfonts from 'cfonts'
;(async () => {
const app = meow(
`
Usage
$ mtr --url <urlString>
$ mtr --url <urlString> --copy
$ mtr --url <urlString> --root post
Options
--url, -u Url to be converted to Types
--root, -r Name of Interface default "RootObject"
--copy, -c Copy output to clipboard
Examples
$ mtr -u https://jsonplaceholder.typicode.com/posts --root post
interface Test {
userId: number;
id: number;
title: string;
body: string;
}
`,
{
importMeta: import.meta,
flags: {
url: {
type: 'string',
alias: 'u',
isRequired: true,
},
copy: {
type: 'boolean',
alias: 'c',
},
root: {
type: 'string',
alias: 'r',
},
},
}
)
const { url, copy, root } = app.flags
if (!url) {
console.log(app.help)
return
}
const res = await fetch(url)
const json = await res.json()
const data = await json[0]
const types = !root
? jsonToTs(data)[0]
: jsonToTs(data, {
rootName: root,
})[0]
console.log(chalk.bold.yellowBright('\nHere are the types you requested:\n'))
console.log(chalk.cyanBright(types))
if (copy) {
clipboard.writeSync(types)
console.log(chalk.greenBright.bold('\nCopied to you clipbaord 🚀\n'))
}
})()