-
Notifications
You must be signed in to change notification settings - Fork 380
Expand file tree
/
Copy pathutils.ts
More file actions
161 lines (148 loc) · 4.79 KB
/
utils.ts
File metadata and controls
161 lines (148 loc) · 4.79 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import path from 'path'
import fs from 'fs-extra'
import os from 'os'
import { _electron as electron, Page } from '@playwright/test'
import { exec } from 'child_process'
import { promisify } from 'util'
// Absolute path to the VS Code executable you downloaded in step 1.
export const VS_CODE_EXE = fs.readJsonSync(
path.join(__dirname, '..', '.vscode-test', 'paths.json'),
).executablePath
// Where your extension lives on disk
export const EXT_PATH = path.resolve(__dirname, '..')
// Where the sushi project lives which we copy from
export const SUSHI_SOURCE_PATH = path.join(
__dirname,
'..',
'..',
'..',
'examples',
'sushi',
)
export const REPO_ROOT = path.join(__dirname, '..', '..', '..')
/**
* Launch VS Code and return the window and a function to close the app.
* @param workspaceDir The workspace directory to open.
* @returns The window and a function to close the app.
*/
export const startVSCode = async (
workspaceDir: string,
): Promise<{
window: Page
close: () => Promise<void>
}> => {
const userDataDir = await fs.mkdtemp(
path.join(os.tmpdir(), 'vscode-user-data-'),
)
const ciArgs = process.env.CI
? [
'--disable-gpu',
'--headless',
'--no-sandbox',
'--disable-dev-shm-usage',
'--window-position=-10000,0',
]
: []
const args = [
...ciArgs,
`--extensionDevelopmentPath=${EXT_PATH}`,
'--disable-workspace-trust',
'--disable-telemetry',
'--install-extension=ms-python.python',
`--user-data-dir=${userDataDir}`,
workspaceDir,
]
const electronApp = await electron.launch({
executablePath: VS_CODE_EXE,
args,
})
const window = await electronApp.firstWindow()
await window.waitForLoadState('domcontentloaded')
await window.waitForLoadState('networkidle')
await clickExplorerTab(window)
return {
window,
close: async () => {
await electronApp.close()
await fs.remove(userDataDir)
},
}
}
/**
* Click on the Explorer tab in the VS Code activity bar if the Explorer tab is not already active.
* This is necessary because the Explorer tab may not be visible if the user has not opened it yet.
*/
export const clickExplorerTab = async (page: Page): Promise<void> => {
const isExplorerActive = await page.locator("text='Explorer'").isVisible()
if (!isExplorerActive) {
// Wait for the activity bar to be loaded
await page.waitForSelector('.actions-container[role="tablist"]')
// Click on the Explorer tab using the codicon class
await page.click('.codicon-explorer-view-icon')
// Wait a bit for the explorer view to activate
await page.locator("text='Explorer'").waitFor({ state: 'visible' })
}
}
const execAsync = promisify(exec)
export interface PythonEnvironment {
pythonPath: string
pipPath: string
}
/**
* Create a virtual environment in the given directory.
* @param venvDir The directory to create the virtual environment in.
*/
export const createVirtualEnvironment = async (
venvDir: string,
): Promise<PythonEnvironment> => {
const pythonCmd = process.platform === 'win32' ? 'python' : 'python3'
const { stderr } = await execAsync(`${pythonCmd} -m venv "${venvDir}"`)
if (stderr && !stderr.includes('WARNING')) {
throw new Error(`Failed to create venv: ${stderr}`)
}
// Get paths
const isWindows = process.platform === 'win32'
const binDir = path.join(venvDir, isWindows ? 'Scripts' : 'bin')
const pythonPath = path.join(binDir, isWindows ? 'python.exe' : 'python')
const pipPath = path.join(binDir, isWindows ? 'pip.exe' : 'pip')
return {
pythonPath,
pipPath,
}
}
/**
* Install packages in the given virtual environment.
* @param pythonDetails The Python environment to use.
* @param packagePaths The paths to the packages to install (string[]).
*/
export const pipInstall = async (
pythonDetails: PythonEnvironment,
packagePaths: string[],
): Promise<void> => {
const { pipPath } = pythonDetails
const execString = `"${pipPath}" install -e "${packagePaths.join('" -e "')}"`
const { stderr } = await execAsync(execString)
if (stderr && !stderr.includes('WARNING') && !stderr.includes('notice')) {
throw new Error(`Failed to install package: ${stderr}`)
}
}
/**
* Open the lineage view in the given window.
*/
export const openLineageView = async (window: Page): Promise<void> => {
await window.keyboard.press(
process.platform === 'darwin' ? 'Meta+Shift+P' : 'Control+Shift+P',
)
await window.keyboard.type('Lineage: Focus On View')
await window.keyboard.press('Enter')
}
/**
* Restart the SQLMesh servers
*/
export const restartSqlmeshServers = async (window: Page): Promise<void> => {
await window.keyboard.press(
process.platform === 'darwin' ? 'Meta+Shift+P' : 'Control+Shift+P',
)
await window.keyboard.type('Restart SQLMesh servers')
await window.keyboard.press('Enter')
}