Skip to content

Commit 18cf256

Browse files
committed
chore(vscode): clean up format and improve logs
1 parent ff0783a commit 18cf256

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

vscode/extension/src/commands/format.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { traceLog } from '../utilities/common/log'
2-
import { execSync } from 'child_process'
32
import { sqlmeshExec } from '../utilities/sqlmesh/sqlmesh'
43
import { err, isErr, ok, Result } from '@bus/result'
54
import * as vscode from 'vscode'
@@ -10,6 +9,7 @@ import {
109
handleSqlmeshLspDependenciesMissingError,
1110
} from '../utilities/errors'
1211
import { AuthenticationProviderTobikoCloud } from '../auth/auth'
12+
import { execAsync } from '../utilities/exec'
1313

1414
export const format =
1515
(authProvider: AuthenticationProviderTobikoCloud) =>
@@ -37,22 +37,20 @@ export const format =
3737
vscode.window.showInformationMessage('Project formatted successfully')
3838
}
3939

40-
const internalFormat = async (): Promise<Result<number, ErrorType>> => {
41-
try {
42-
const exec = await sqlmeshExec()
43-
if (isErr(exec)) {
44-
return exec
45-
}
46-
execSync(`${exec.value.bin} format`, {
47-
encoding: 'utf-8',
48-
cwd: exec.value.workspacePath,
49-
env: exec.value.env,
50-
})
51-
return ok(0)
52-
} catch (error: any) {
40+
const internalFormat = async (): Promise<Result<undefined, ErrorType>> => {
41+
const exec = await sqlmeshExec()
42+
if (isErr(exec)) {
43+
return exec
44+
}
45+
const result = await execAsync(`${exec.value.bin}`, ['format'], {
46+
cwd: exec.value.workspacePath,
47+
env: exec.value.env,
48+
})
49+
if (result.exitCode !== 0) {
5350
return err({
5451
type: 'generic',
55-
message: `Error executing sqlmesh format: ${error.message}`,
52+
message: `Error executing sqlmesh format: ${result.stderr}`,
5653
})
5754
}
55+
return ok(undefined)
5856
}

vscode/extension/src/utilities/exec.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,26 @@ export interface CancellableExecOptions extends ExecOptions {
1212
signal?: AbortSignal
1313
}
1414

15-
export function execAsync(
15+
export async function execAsync(
16+
command: string,
17+
args: string[],
18+
options: CancellableExecOptions = {},
19+
): Promise<ExecResult> {
20+
traceInfo(`Executing command: ${command} ${args.join(' ')}`)
21+
const result = await execAsyncCore(command, args, options)
22+
traceInfo(
23+
`Command ${command} ${args.join(' ')} exited with code ${result.exitCode} and stdout ${result.stdout} and stderr ${result.stderr}`,
24+
)
25+
return result
26+
}
27+
28+
function execAsyncCore(
1629
command: string,
1730
args: string[],
1831
options: CancellableExecOptions = {},
1932
): Promise<ExecResult> {
2033
return new Promise((resolve, reject) => {
2134
// Pass the signal straight through to `exec`
22-
traceInfo(`Executing command: ${command} ${args.join(' ')}`)
2335
const child = exec(
2436
`${command} ${args.join(' ')}`,
2537
options,

0 commit comments

Comments
 (0)