-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathutils.ts
More file actions
152 lines (140 loc) · 4.65 KB
/
utils.ts
File metadata and controls
152 lines (140 loc) · 4.65 KB
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
/* eslint-disable no-console */
import fs from 'fs'
import { globSync } from 'glob'
import {
bold,
cyanBright,
gray,
green,
magenta,
magentaBright,
red,
underline,
white,
whiteBright,
yellow,
} from 'colorette'
import L from 'lodash'
import {
asOpenAPIPath,
getRouteMap,
getRouteSnippets,
routemapToOpenAPI,
} from '@hyperstackjs/hypercontroller'
import asTable from 'as-table'
import { getJobCounts } from '@hyperstackjs/hyperworker'
import type { Context } from './types'
export const sleep = (t: number) =>
new Promise((resolve, _reject) => setTimeout(() => resolve(true), t))
export const requireModule = (f: string) => {
let mod = null
try {
mod = require(f)
} catch (e: any) {
console.log(`error dynamically loading '${f}'\n${e.toString()}`)
throw e
}
return mod.default || mod
}
export const load = (patt: string) =>
L.sortBy(globSync(patt), L.identity).map(requireModule)
export const loadMap = (patt: string) =>
L.sortBy(globSync(patt), L.identity).reduce((acc: any, f: string) => {
acc[f] = requireModule(f)
return acc
}, {})
export const exportRoutes = (controllers: any, toFile: string) => {
const routemap = getRouteMap(controllers)
const spec = routemapToOpenAPI(routemap)
fs.writeFileSync(toFile, JSON.stringify(spec.getSpec(), null, 2))
}
const snipmap: Record<string, string> = {
wget: 'shell_wget',
curl: 'shell_curl',
httpie: 'shell_httpie',
}
const normalizeSnippets = (snip: string) => snipmap[snip] || snip
export const printJobCounts = async (workers: any) => {
const counts = await getJobCounts(Object.values(workers))
console.log(
asTable.configure({
print: (v, k) => {
if (k === 'name') {
return bold(yellow(v)).toString()
} else if (k === 'failed') {
return red(v).toString()
} else if (k === 'active') {
return yellow(v).toString()
} else if (k === 'completed') {
return green(v).toString()
} else {
return gray(v).toString()
}
},
})(counts)
)
}
export const printRoutesV2 = (controllers: any, snippets: string[] = []) => {
const routemap = getRouteMap(controllers)
// print the route map
L.forEach(routemap, (mapping: any, ctl: string) => {
console.log(`${green(ctl)} ${gray(mapping.path)}`)
mapping.actions.forEach((action: any) => {
action.routes.forEach((route: any) => {
const verb = route.verb
const path = asOpenAPIPath(route.parsedPath)
console.log(` ${yellow(verb.toUpperCase())}\t${whiteBright(path)}`)
if (snippets && snippets.length > 0) {
const snips = getRouteSnippets(
routemap,
verb,
path,
snippets.map(normalizeSnippets)
)
snips.snippets.forEach((snip: any) => {
console.log(``)
console.log(`${underline(bold(white(snip.title)))}`)
console.log(`${gray(snip.content)}`)
console.log(``)
})
}
})
})
console.log('\n')
})
}
export const printBanner = (context: Context, print: (s: string) => void) => {
const logger = context.logger()
const { bold, green, cyan, underline } = logger.colors
const stat = (s: any) => underline(bold(green(s)))
const setting = (s: any) => bold(cyan(s))
const store = context.store()
const port = store.controllers?.port
const displayOn = (cfg: any) =>
Object.keys(L.omitBy(cfg, (v) => !v))
.map((v) => setting(v))
.join(', ')
const banner = `
${cyanBright(`▄ ▄ ▄ ▄`)}
${cyan(`█─▄ ▄ ▄ ▄─▄ ▄─▄ ▄─▄ ▄── █─ ─▄ ▄── █─▄`)}
${magentaBright(`▀ ▀ ▀─█ █─▀ ▀── ▀ ──▀ ▀─ ▀─▀ ▀── ▀ ▀`)}
${magentaBright(` └─▀${gray(` https://hyperstackjs.io`)}`)}
🚀 App is running on port ${stat(port)}, in ${stat(store.app?.mode)} mode.
🤘 Loaded ${stat(store.controllers?.numControllers)} controllers, ${stat(
store.workers?.numWorkers
)} workers, ${stat(store.workers?.numMailers)} mailers in ${magenta(
parseInt(store.stats?.bootTime as any)
)}ms.
🧰 Environment is environments/${stat(store.app?.environmentFile)}
· logger: ${setting(logger.level)}
· controllers: ${displayOn(context.config().controllers)}
· workers: ${setting(store.workers?.background)}, ${setting(
store.workers?.mode
)}
· models: ${displayOn(context.config().database)}
· mailers: ${displayOn(
L.omit(context.config().mailers, 'delivery')
)} delivery=${setting(context.config().mailers?.delivery || 'none')}
`
print(banner)
}