galbe 0.13.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,26 +1,20 @@
1
- import { $ } from 'bun'
2
- import { devNull } from 'os'
3
1
  import { Script, createContext } from 'vm'
4
2
  import { Command, Option } from 'commander'
5
3
  import { resolve, extname } from 'path'
6
- import { rm } from 'fs/promises'
7
4
  import { transformSync } from '@swc/core'
8
- import { CWD, fmtList, instanciateRoutes, silentExec, abbreviateVar } from '../../util'
9
- import { $T, Galbe, GalbeCLICommand, Method, Route, STResponse } from '../../../src'
5
+ import { CWD, fmtList, instanciateRoutes, silentExec } from '../../util'
6
+ import { $T, Galbe, Method, Route, STResponse } from '../../../src'
10
7
  import { walkRoutes } from '../../../src/util'
11
- import { schemaToTypeStr, Optional, STSchema } from '../../../src/schema'
8
+ import { schemaToTypeStr, STSchema } from '../../../src/schema'
12
9
 
13
- const clientTargets = ['ts', 'js', 'cli']
10
+ const clientTargets = ['ts', 'js']
14
11
 
15
12
  export default (cmd: Command) => {
16
13
  cmd
17
14
  .description('generate a \x1b[1;30m\x1b[36mGalbe\x1b[0m client')
18
15
  .argument('<index>', 'index file')
19
16
  .addOption(
20
- new Option('-o, --out <file>', 'output file').default(
21
- null,
22
- fmtList(['dist/client.ts', 'dist/client.js', 'dist/cli'])
23
- )
17
+ new Option('-o, --out <file>', 'output file').default(null, fmtList(['dist/client.ts', 'dist/client.js']))
24
18
  )
25
19
  .addOption(
26
20
  new Option('-t, --target <target>', `build target ${fmtList(clientTargets)}`).argParser(v => {
@@ -32,7 +26,7 @@ export default (cmd: Command) => {
32
26
  .action(async (index, props) => {
33
27
  let { target, out } = props
34
28
  if (!target) target = clientTargets.includes(extname(index)?.slice(1)) ? extname(index)?.slice(1) : 'ts'
35
- if (!out) out = { ts: 'dist/client.ts', js: 'dist/client.js', cli: 'dist/cli' }[target]
29
+ if (!out) out = { ts: 'dist/client.ts', js: 'dist/client.js' }[target]
36
30
  let pckg: any = {}
37
31
  try {
38
32
  pckg = await Bun.file(resolve(CWD, 'package.json')).json()
@@ -66,7 +60,6 @@ export default (cmd: Command) => {
66
60
  head: [],
67
61
  }
68
62
  const types: Record<string, STResponse> = {}
69
- let commands: GalbeCLICommand[] = []
70
63
  const metaRoutes = g.meta?.reduce(
71
64
  (routes, c) => ({ ...routes, ...c.routes }),
72
65
  {} as Record<string, Record<string, Record<string, any>>>
@@ -130,109 +123,39 @@ export default (cmd: Command) => {
130
123
  types[s.id] = schemaToTypeStr(s)
131
124
  })
132
125
  routes[r.method.toLocaleLowerCase()].push(route)
133
- if (target === 'cli' && meta?.operationId)
134
- commands.push({
135
- name: meta.operationId,
136
- tags: meta?.tags ? (Array.isArray(meta.tags) ? meta.tags : [meta.tags]) : [],
137
- description: route.summary || route.description,
138
- route,
139
- arguments:
140
- Object.entries((route?.params || {}) as Record<string, { type: string; description?: string }>)?.map(
141
- ([k, p]) => {
142
- return {
143
- name: k,
144
- type: p.type === 'boolean' ? '' : `<${p.type}>`,
145
- description: p?.description || '',
146
- }
147
- }
148
- ) || [],
149
- options:
150
- Object.entries((r.schema?.query || {}) as Record<string, STSchema>)?.map(([k, o]) => {
151
- let type = schemaToTypeStr({ ...o, [Optional]: false })
152
- return {
153
- name: k,
154
- short: abbreviateVar(k),
155
- type: type === 'boolean' ? '' : `<${type}>`,
156
- description: o?.description || '',
157
- default: o.default,
158
- }
159
- }) || [],
160
- })
161
126
  })
162
127
 
163
- if (target === 'js' || target === 'ts') {
164
- const file = await Bun.file(resolve(import.meta.dir, '..', '..', 'res', 'client.template.ts')).text()
165
- let filled = file.replaceAll(/\/\*\%([\s\S]*?)\%\*\//g, (_match, p) => {
166
- let idt = p.match(/^\n*([ \t]*)/, p)?.[1] || ''
167
- const script = new Script(p)
168
- const sandbox = {
169
- console,
170
- version: pckg?.version || '0.1.0',
171
- routes,
172
- types,
173
- }
174
- createContext(sandbox)
175
- let res = script.runInNewContext(sandbox)
176
- if (typeof res === 'string') res = res.split('\n')
177
- if (Array.isArray(res)) return res.map((s, i) => (i === 0 ? s : `${idt}${s}`)).join('\n')
178
- return res ?? ''
179
- })
180
-
181
- if (target === 'js') {
182
- filled = transformSync(filled, {
183
- jsc: {
184
- parser: {
185
- syntax: 'typescript',
186
- },
187
- preserveAllComments: true,
188
- target: 'esnext',
189
- },
190
- }).code
128
+ const file = await Bun.file(resolve(import.meta.dir, '..', '..', 'res', 'client.template.ts')).text()
129
+ let filled = file.replaceAll(/\/\*\%([\s\S]*?)\%\*\//g, (_match, p) => {
130
+ let idt = p.match(/^\n*([ \t]*)/, p)?.[1] || ''
131
+ const script = new Script(p)
132
+ const sandbox = {
133
+ console,
134
+ version: pckg?.version || '0.1.0',
135
+ routes,
136
+ types,
191
137
  }
138
+ createContext(sandbox)
139
+ let res = script.runInNewContext(sandbox)
140
+ if (typeof res === 'string') res = res.split('\n')
141
+ if (Array.isArray(res)) return res.map((s, i) => (i === 0 ? s : `${idt}${s}`)).join('\n')
142
+ return res ?? ''
143
+ })
192
144
 
193
- await Bun.write(out, filled)
194
- } else if (target === 'cli') {
195
- const file = await Bun.file(resolve(import.meta.dir, '..', '..', 'res', 'cli.template.js')).text()
196
-
197
- // Plugin CLI hook
198
- if (commands) for (let p of g.plugins) if (p.cli) await p.cli(commands)
199
-
200
- const tags = commands.reduce(
201
- (p, c) => {
202
- if (c.tags.length)
203
- for (const t of c.tags) {
204
- if (!(t in p)) p[t] = []
205
- p[t].push(c)
206
- }
207
- else p[''].push(c)
208
- return p
145
+ if (target === 'js') {
146
+ filled = transformSync(filled, {
147
+ jsc: {
148
+ parser: {
149
+ syntax: 'typescript',
150
+ },
151
+ preserveAllComments: true,
152
+ target: 'esnext',
209
153
  },
210
- { '': [] as GalbeCLICommand[] }
211
- )
212
-
213
- let filled = file.replaceAll(/\/\*\%([\s\S]*?)\%\*\//g, (_match, p) => {
214
- let idt = p.match(/^\n*([ \t]*)/, p)?.[1] || ''
215
- const script = new Script(p)
216
- const sandbox = {
217
- console,
218
- name: pckg?.name || 'Galbe app CLI',
219
- description: pckg?.description || '',
220
- version: pckg?.version || '0.1.0',
221
- tags,
222
- }
223
- createContext(sandbox)
224
- let res = script.runInNewContext(sandbox)
225
- if (typeof res === 'string') res = res.split('\n')
226
- if (Array.isArray(res)) return res.map((s, i) => (i === 0 ? s : `${idt}${s}`)).join('\n')
227
- return res ?? ''
228
- })
229
- const buildId = crypto.randomUUID()
230
- const buildPath = resolve(CWD, '.galbe', 'client', `${buildId}.js`)
231
- await Bun.write(buildPath, filled)
232
- await $`bun build --compile ${buildPath} --outfile ${resolve(CWD, out)} > ${devNull} && printf "\u200B"`
233
- await rm(resolve(CWD, '.galbe'), { recursive: true })
154
+ }).code
234
155
  }
235
156
 
157
+ await Bun.write(out, filled)
158
+
236
159
  Bun.write(Bun.stdout, ' : \x1b[1;30m\x1b[32mdone\x1b[0m\n')
237
160
  process.exit(0)
238
161
  })