-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
85 lines (78 loc) · 2.69 KB
/
index.ts
File metadata and controls
85 lines (78 loc) · 2.69 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
#!/usr/bin/env node
import { Command, CommanderError } from 'commander'
import * as path from 'path'
import * as fs from 'fs'
import { grey, red } from 'colors'
import { startServer } from './server'
import { parseZModelSchema } from './zmodel-parser'
import 'dotenv/config'
import { getVersion } from './utils/version-utils'
import { telemetry } from './telemetry'
import { CliError } from './cli-error'
export function createProgram() {
const program = new Command()
program
.name('zenstack-proxy')
.description('CLI tool to run ZenStack proxy server')
.version(getVersion()!)
program
.option('-z, --zenstack <path>', 'Path to ZenStack generated folder')
.option('-p, --port <number>', 'Port number for the server', '8008')
.option('-s, --schema <path>', 'Path to ZModel schema file', 'schema.zmodel')
.option('-d, --datasource-url <url>', 'Datasource URL (overrides schema configuration)')
.option('-l, --log <level...>', 'Query log levels (e.g., query, info, warn, error)')
.action(async (options) => {
// Determine ZModel schema path
const zmodelPath = path.isAbsolute(options.schema)
? options.schema
: path.join(process.cwd(), options.schema)
if (!fs.existsSync(zmodelPath)) {
console.error(`ZModel schema file not found: ${zmodelPath}`)
console.error('Please provide a valid path using the -s option.')
process.exit(1)
}
console.log(grey(`Loading ZModel schema from: ${zmodelPath}`))
// Parse ZModel schema
const zmodelConfig = parseZModelSchema(zmodelPath, options.datasourceUrl)
const zmodelSchemaDir = path.dirname(zmodelPath)
// Start the server
await startServer({
zenstackPath: options.zenstack,
port: parseInt(options.port),
zmodelConfig: zmodelConfig,
zmodelSchemaDir: zmodelSchemaDir,
logLevel: options.log,
})
})
return program
}
export default async function () {
const program = createProgram()
// handle errors explicitly to ensure telemetry
program.exitOverride()
let exitCode = 1
try {
await telemetry.trackCli(async () => {
await program.parseAsync()
})
} catch (e: unknown) {
if (e instanceof CommanderError) {
// ignore
exitCode = e.exitCode
} else if (e instanceof CliError) {
console.error(red(e.message))
} else {
if (e instanceof Error) {
console.error(red(`Unhandled error: ${e.message}`))
} else {
console.error(red(`Unhandled error: ${String(e)}`))
}
}
if (telemetry.isTracking) {
// give telemetry a chance to send events before exit
setTimeout(() => {
process.exit(exitCode)
}, 200)
}
}
}