-
Notifications
You must be signed in to change notification settings - Fork 380
Expand file tree
/
Copy pathformat.ts
More file actions
68 lines (65 loc) · 1.94 KB
/
format.ts
File metadata and controls
68 lines (65 loc) · 1.94 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
import { traceLog } from '../utilities/common/log'
import { sqlmeshExec } from '../utilities/sqlmesh/sqlmesh'
import { err, isErr, ok, Result } from '@bus/result'
import * as vscode from 'vscode'
import { ErrorType, handleError } from '../utilities/errors'
import { AuthenticationProviderTobikoCloud } from '../auth/auth'
import { execAsync } from '../utilities/exec'
import { LSPClient } from '../lsp/lsp'
export const format =
(
authProvider: AuthenticationProviderTobikoCloud,
lsp: LSPClient | undefined,
restartLSP: () => Promise<void>,
) =>
async (): Promise<void> => {
traceLog('Calling format')
const out = await internalFormat(lsp)
if (isErr(out)) {
return handleError(
authProvider,
restartLSP,
out.error,
'Project format failed',
)
}
vscode.window.showInformationMessage('Project formatted successfully')
}
const internalFormat = async (
lsp: LSPClient | undefined,
): Promise<Result<undefined, ErrorType>> => {
try {
// Try LSP method first
if (lsp) {
const response = await lsp.call_custom_method(
'sqlmesh/format_project',
{},
)
if (isErr(response)) {
return response
}
return ok(undefined)
}
} catch (error) {
traceLog(`LSP format failed, falling back to CLI: ${JSON.stringify(error)}`)
}
// Fallback to CLI method if LSP is not available
// TODO This is a solution in order to be backwards compatible in the cases
// where the LSP method is not implemented yet. This should be removed at
// some point in the future.
const exec = await sqlmeshExec()
if (isErr(exec)) {
return exec
}
const result = await execAsync(`${exec.value.bin}`, ['format'], {
cwd: exec.value.workspacePath,
env: exec.value.env,
})
if (result.exitCode !== 0) {
return err({
type: 'generic',
message: `Error executing sqlmesh format: ${result.stderr}`,
})
}
return ok(undefined)
}