-
Notifications
You must be signed in to change notification settings - Fork 380
Expand file tree
/
Copy pathconfig.ts
More file actions
104 lines (93 loc) · 3.11 KB
/
config.ts
File metadata and controls
104 lines (93 loc) · 3.11 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { workspace, WorkspaceFolder } from 'vscode'
import path from 'path'
import fs from 'fs'
import { Result, err, ok } from '@bus/result'
import { traceVerbose, traceInfo } from './common/log'
export interface SqlmeshConfiguration {
projectPath: string
lspEntryPoint: string
}
/**
* Get the SQLMesh configuration from VS Code settings.
*
* @returns The SQLMesh configuration
*/
function getSqlmeshConfiguration(): SqlmeshConfiguration {
const config = workspace.getConfiguration('sqlmesh')
const projectPath = config.get<string>('projectPath', '')
const lspEntryPoint = config.get<string>('lspEntrypoint', '')
return {
projectPath,
lspEntryPoint,
}
}
/**
* Get the SQLMesh LSP entry point from VS Code settings. undefined if not set
* it's expected to be a string in the format "command arg1 arg2 ...".
*/
export function getSqlmeshLspEntryPoint():
| {
entrypoint: string
args: string[]
}
| undefined {
const config = getSqlmeshConfiguration()
if (config.lspEntryPoint === '') {
return undefined
}
// Split the entry point into command and arguments
const parts = config.lspEntryPoint.split(' ')
const entrypoint = parts[0]
const args = parts.slice(1)
if (args.length === 0) {
return { entrypoint, args: [] }
}
return { entrypoint, args }
}
/**
* Validate and resolve the project path from configuration.
* If no project path is configured, use the workspace folder.
* If the project path is configured, it must be a directory that contains a SQLMesh project.
*
* @param workspaceFolder The current workspace folder
* @returns A Result containing the resolved project path or an error
*/
export function resolveProjectPath(
workspaceFolder: WorkspaceFolder,
): Result<string, string> {
const config = getSqlmeshConfiguration()
if (!config.projectPath) {
// If no project path is configured, use the workspace folder
traceVerbose('No project path configured, using workspace folder')
return ok(workspaceFolder.uri.fsPath)
}
let resolvedPath: string
// Check if the path is absolute
if (path.isAbsolute(config.projectPath)) {
resolvedPath = config.projectPath
} else {
// Resolve relative path from workspace root
resolvedPath = path.join(workspaceFolder.uri.fsPath, config.projectPath)
}
// Normalize the path
resolvedPath = path.normalize(resolvedPath)
// Validate that the path exists
if (!fs.existsSync(resolvedPath)) {
return err(`Configured project path does not exist: ${resolvedPath}`)
}
// Validate that it's a directory
const stats = fs.statSync(resolvedPath)
if (!stats.isDirectory()) {
return err(`Configured project path is not a directory: ${resolvedPath}`)
}
// Check if it contains SQLMesh project files (config.yaml, config.yml, or config.py)
const configFiles = ['config.yaml', 'config.yml', 'config.py']
const hasConfigFile = configFiles.some(file =>
fs.existsSync(path.join(resolvedPath, file)),
)
if (!hasConfigFile) {
traceInfo(`Warning: No SQLMesh configuration file found in ${resolvedPath}`)
}
traceVerbose(`Using project path: ${resolvedPath}`)
return ok(resolvedPath)
}