forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspawn.ts
More file actions
71 lines (65 loc) · 2.11 KB
/
Copy pathspawn.ts
File metadata and controls
71 lines (65 loc) · 2.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
import * as vscode from "vscode"
import { checkFileExists, readFileText } from "./fs"
import { versionTryParse } from "jacdac-ts"
import { Utils } from "vscode-uri"
function spawnAndWatch(
options: vscode.TerminalOptions & {
text?: string
outFile: vscode.Uri
timeout?: number
}
): Promise<string> {
return new Promise(resolve => {
const { outFile, text, timeout = 10000, ...rest } = options
// handle terminal crash
let onclose = vscode.window.onDidCloseTerminal(e => {
if (e === terminal) {
ready()
}
})
let terminal: vscode.Terminal
let timeoutId: any
const cleanup = () => {
const canResolve = !!terminal || !!timeoutId
terminal?.dispose?.()
onclose?.dispose?.()
clearTimeout(timeoutId)
terminal = undefined
onclose = undefined
timeoutId = undefined
return canResolve
}
const ready = async () => {
if (cleanup()) {
const text = await readFileText(outFile)
resolve(text)
}
}
timeoutId = setTimeout(() => {
if (cleanup()) resolve(undefined)
}, timeout)
try {
terminal = vscode.window.createTerminal(rest)
terminal.show()
if (text) terminal.sendText(text, true)
} catch (e) {
console.debug(e)
if (cleanup()) resolve(undefined)
}
})
}
export async function tryGetNodeVersion(nodePath: string, outFile: vscode.Uri) {
await vscode.workspace.fs.createDirectory(Utils.dirname(outFile))
if (await checkFileExists(outFile))
await vscode.workspace.fs.delete(outFile, { useTrash: false })
const v = await spawnAndWatch({
name: "DeviceScript - Installation Diagnostics",
shellPath: nodePath,
shellArgs: [
"-e",
`require('fs').writeFileSync('${outFile.fsPath}',process.version, { encoding: 'utf8' })`,
],
outFile,
})
return versionTryParse(v)
}