@graphenedata/cli 0.0.12 → 0.0.13
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.
- package/package.json +2 -2
- package/cli.ts +0 -156
package/package.json
CHANGED
package/cli.ts
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import {Command} from 'commander'
|
|
4
|
-
import {printDiagnostics, printTable} from './printer.ts'
|
|
5
|
-
import {analyze, getDiagnostics, loadWorkspace, toSql, type Query} from '../lang/core.ts'
|
|
6
|
-
import fs from 'fs-extra'
|
|
7
|
-
import path from 'path'
|
|
8
|
-
import {fileURLToPath} from 'url'
|
|
9
|
-
import dotenv from 'dotenv'
|
|
10
|
-
import {config, loadConfig} from '../lang/config.ts'
|
|
11
|
-
import {runServeInBackground, stopGrapheneIfRunning} from './background.ts'
|
|
12
|
-
import {check} from './check.ts'
|
|
13
|
-
import {getConnection, runQuery} from './connections/index.ts'
|
|
14
|
-
import {loginPkce} from './auth.ts'
|
|
15
|
-
|
|
16
|
-
dotenv.config({quiet: true})
|
|
17
|
-
const program = new Command()
|
|
18
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
19
|
-
// in dev: cli/cli.ts -> cli/package.json. in dist: cli/dist/cli/cli.js -> cli/package.json
|
|
20
|
-
const pkgPath = fs.existsSync(path.join(__dirname, 'package.json')) ? path.join(__dirname, 'package.json') : path.join(__dirname, '../../package.json')
|
|
21
|
-
const pkg = fs.readJsonSync(pkgPath)
|
|
22
|
-
|
|
23
|
-
program.name('graphene').description('Graphene CLI').version(pkg.version, '-v, --version')
|
|
24
|
-
|
|
25
|
-
program.hook('preAction', async () => {
|
|
26
|
-
if (process.env.CLI_DELAY) { // useful if you want to attach a debugger
|
|
27
|
-
await new Promise(r => setTimeout(r, 1000))
|
|
28
|
-
}
|
|
29
|
-
loadConfig(process.cwd())
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
program.command('compile')
|
|
33
|
-
.description('Translate a query to SQL and print it')
|
|
34
|
-
.argument('[input]', 'Path to file, a raw string, or "-" for stdin')
|
|
35
|
-
.action(async (input: string | undefined) => {
|
|
36
|
-
await loadWorkspace(process.cwd(), false)
|
|
37
|
-
let sql = await readInput(input)
|
|
38
|
-
let queries = analyze(sql)
|
|
39
|
-
if (!validQuery(queries)) return
|
|
40
|
-
console.log(toSql(queries[0]))
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
program.command('run')
|
|
44
|
-
.description('Run a query against your database')
|
|
45
|
-
.argument('[input]', 'Path to file, a raw string, or "-" for stdin')
|
|
46
|
-
.action(async (input: string | undefined) => {
|
|
47
|
-
await loadWorkspace(process.cwd(), false)
|
|
48
|
-
let gsql = await readInput(input)
|
|
49
|
-
let queries = analyze(gsql)
|
|
50
|
-
if (!validQuery(queries)) return
|
|
51
|
-
let sql = toSql(queries[0])
|
|
52
|
-
let res = await runQuery(sql)
|
|
53
|
-
printTable(res.rows)
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
program.command('schema')
|
|
57
|
-
.description('Inspect database tables or describe a table')
|
|
58
|
-
.argument('[schema | table]', 'Optional schema or table name to describe')
|
|
59
|
-
.action(async (tableArg: string) => {
|
|
60
|
-
let connection = await getConnection()
|
|
61
|
-
let datasets = await connection.listDatasets()
|
|
62
|
-
|
|
63
|
-
// if there's no arg and more than one dataset, just list the datasets
|
|
64
|
-
if (!tableArg && datasets.length > 1) {
|
|
65
|
-
return console.log(`Datasets available:\n${datasets.join('\n')}`)
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// figure out if you're wanting to list tables in a schema/dataset
|
|
69
|
-
let dsToList: string | null = null
|
|
70
|
-
let parts = tableArg ? tableArg.split('.') : []
|
|
71
|
-
if (datasets.includes(tableArg)) dsToList = tableArg // you gave the name of a dataset
|
|
72
|
-
else if (!tableArg && datasets.length == 1) dsToList = datasets[0] // only one dataset, and no args
|
|
73
|
-
else if (!tableArg && config.namespace) dsToList = config.namespace // default namespace configured
|
|
74
|
-
else if (!tableArg && config.dialect == 'duckdb') dsToList = '<default>'
|
|
75
|
-
else if (tableArg && config.dialect == 'snowflake' && parts.length == 2) dsToList = tableArg
|
|
76
|
-
|
|
77
|
-
if (dsToList) {
|
|
78
|
-
let tables = await connection.listTables(dsToList)
|
|
79
|
-
return console.log(`Tables${dsToList ? ` in ${dsToList}` : ''}:\n${tables.join('\n')}`)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// otherwise, assume you're wanting to see tables
|
|
83
|
-
let cols = await connection.describeTable(tableArg)
|
|
84
|
-
if (!cols.length) return console.log(`Table ${tableArg} not found`)
|
|
85
|
-
console.log(`table ${tableArg} (`)
|
|
86
|
-
cols.forEach(col => console.log(` ${col.name} ${col.dataType}`))
|
|
87
|
-
console.log(')')
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
program.command('serve')
|
|
91
|
-
.description('Run the local server')
|
|
92
|
-
.option('--bg', 'Run the server in the background')
|
|
93
|
-
.action(async (options: {bg?: boolean}) => {
|
|
94
|
-
await stopGrapheneIfRunning()
|
|
95
|
-
if (options.bg) {
|
|
96
|
-
await runServeInBackground()
|
|
97
|
-
process.exit(0)
|
|
98
|
-
} else {
|
|
99
|
-
let mod = await import('./serve2.ts') // load dynamically, so we're not pulling in a bunch of deps we might not need
|
|
100
|
-
await mod.serve2()
|
|
101
|
-
}
|
|
102
|
-
})
|
|
103
|
-
|
|
104
|
-
program.command('stop')
|
|
105
|
-
.description('Stop the local server')
|
|
106
|
-
.action(async () => { await stopGrapheneIfRunning() })
|
|
107
|
-
|
|
108
|
-
program.command('check')
|
|
109
|
-
.description('Check the project for errors, optionally capturing a page screenshot')
|
|
110
|
-
.argument('[mdFile]', 'Markdown file to check (e.g., index.md)')
|
|
111
|
-
.option('-c, --chart <chartTitle>', 'Title of a specific chart to capture')
|
|
112
|
-
.action(async (mdArg: string | undefined, options: {chart?: string}) => {
|
|
113
|
-
let res = await check({mdArg, chart: options.chart})
|
|
114
|
-
process.exit(res ? 0 : 1) // import to call `exit`, bc if we started the server in the background, just returning won't actually exit the process.
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
program.command('login')
|
|
118
|
-
.description('Log in to Graphene Cloud')
|
|
119
|
-
.action(async () => {
|
|
120
|
-
await loginPkce()
|
|
121
|
-
console.log('Successfully logged in')
|
|
122
|
-
process.exit(0)
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
program.parse(process.argv)
|
|
126
|
-
|
|
127
|
-
async function readInput (arg): Promise<string> {
|
|
128
|
-
if (!arg || arg === '-') {
|
|
129
|
-
return await new Promise<string>((resolve) => {
|
|
130
|
-
let data = ''
|
|
131
|
-
process.stdin.setEncoding('utf-8')
|
|
132
|
-
process.stdin.on('data', (chunk) => (data += chunk))
|
|
133
|
-
process.stdin.on('end', () => resolve(data))
|
|
134
|
-
process.stdin.resume()
|
|
135
|
-
})
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
let absolutePath = path.resolve(arg)
|
|
139
|
-
if (fs.existsSync(absolutePath)) {
|
|
140
|
-
return await fs.promises.readFile(absolutePath, 'utf-8')
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return arg
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function validQuery (queries: Query[]): boolean {
|
|
147
|
-
if (getDiagnostics().length) {
|
|
148
|
-
printDiagnostics(getDiagnostics())
|
|
149
|
-
process.exit(1)
|
|
150
|
-
}
|
|
151
|
-
if (queries.length == 0) {
|
|
152
|
-
console.warn('No queries found')
|
|
153
|
-
process.exit(1)
|
|
154
|
-
}
|
|
155
|
-
return true
|
|
156
|
-
}
|