-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.ts
193 lines (160 loc) · 6.03 KB
/
utils.ts
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import * as child_process from 'child_process'
import * as vscode from 'vscode'
import { alloglot } from './config'
export interface IDisposal extends vscode.Disposable {
insert(disposable: vscode.Disposable): void
}
export namespace Disposal {
/**
* Create a {@link IDisposal disposal} that when disposed will dispose of all inserted disposables.
*/
export function make(): IDisposal {
const disposables: Array<vscode.Disposable> = []
return {
insert(disposable) {
disposables.push(disposable)
},
dispose() {
disposables.forEach(disposable => disposable.dispose())
}
}
}
}
export interface IAsyncProcess<T> {
disposable: vscode.Disposable
promise: Promise<T>
}
export namespace AsyncProcess {
type Spec = {
command: string
basedir?: vscode.Uri
stdin?: string
output?: vscode.OutputChannel
}
/**
* Create a (potentially) long-lived {@link IAsyncProcess async process}.
* `spawn(spec)` runs a command and returns a promise that resolves when the process exits.
* Method `dispose()` kills the process and is idempotent.
*/
export function spawn(spec: Spec): IAsyncProcess<void> {
return make<void>(spec, (cmd, opts, resolve) => {
const proc = child_process.spawn(cmd, [], { ...opts, shell: true })
proc.on('exit', resolve)
return proc
})
}
/**
* Create a short-lived {@link IAsyncProcess async process} that runs a command and returns a promise of the result.
* `exec(spec, f)` computes the result by running the `f` on the process stdout.
* Method `dispose()` kills the process and is idempotent.
*/
export function exec<T>(spec: Spec, f: (stdout: string) => T): IAsyncProcess<T> {
return make(spec, (cmd, opts, resolve) => {
return child_process.exec(cmd, opts, (error, stdout, stderr) => {
!stdout && spec.output?.appendLine(alloglot.ui.commandNoOutput(spec.command))
resolve(f(stdout))
})
})
}
function make<T>(spec: Spec, makeProc: (command: string, opts: { cwd?: string, signal?: AbortSignal }, resolve: (t: T) => void) => child_process.ChildProcess): IAsyncProcess<T> {
const { output, command, basedir, stdin } = spec
const cwd = basedir?.fsPath
let proc: child_process.ChildProcess | undefined = undefined
const disposable = vscode.Disposable.from({
dispose: () => {
if (proc) {
try {
output?.appendLine(alloglot.ui.killingCommand(command))
proc.kill('SIGINT')
while (!proc.killed) { }
proc = undefined
output?.appendLine(alloglot.ui.commandKilled(command))
} catch (err) {
output?.appendLine(alloglot.ui.errorKillingCommand(command, err))
}
}
}
})
// giving this an `any` signature allows us to add a `dispose` method.
// it's a little bit jank, but i don't know how else to do it.
const promise: Promise<T> = new Promise<T>((resolve, reject) => {
output?.appendLine(alloglot.ui.runningCommand(command, cwd))
try {
proc = makeProc(command, { cwd }, resolve)
proc.on('error', error => {
output?.appendLine(alloglot.ui.errorRunningCommand(command, error))
reject(error)
})
proc.stdout?.on('data', chunk => output?.append(stripAnsi(chunk.toString())))
proc.stderr?.on('data', chunk => output?.append(stripAnsi(chunk.toString())))
stdin && proc.stdin?.write(stdin)
proc.stdin?.end()
} catch (err) {
output?.appendLine(alloglot.ui.errorRunningCommand(command, err))
reject(err)
}
})
promise.then(() => output?.appendLine(alloglot.ui.ranCommand(command)))
return { disposable, promise }
}
const stripAnsi: (raw: string) => string = require('strip-ansi').default
}
export interface IHierarchicalOutputChannel extends vscode.OutputChannel {
prefixPath: Array<string>
local(prefix: string): IHierarchicalOutputChannel
split(): IHierarchicalOutputChannel
}
export namespace HierarchicalOutputChannel {
/**
* Create an {@link IHierarchicalOutputChannel output channel} that can spawn children output channels that prefix lines with a path.
* Method `local(prefix)` spawns a child channel with the supplied prefix appended to the prefix path. The spawned channel can spawn further children.
* Method `split()` creates a separate output channel named after the current prefix path. It will have an empty prefix path.
* Spawned channels will have the same name as the parent channel, so messages appear in the same output window.
*/
export function make(name: string): IHierarchicalOutputChannel {
return addTimestamps(makeNoTimestamps(name))
}
function makeNoTimestamps(name: string): IHierarchicalOutputChannel {
return promote([], vscode.window.createOutputChannel(name))
}
function addTimestamps(output: IHierarchicalOutputChannel): IHierarchicalOutputChannel {
return {
...output,
append(value: string) {
output.append(`[${timestamp()}] ${value}`)
},
appendLine(value: string) {
output.appendLine(`[${timestamp()}] ${value}`)
}
}
}
function timestamp(): string {
return new Date().toJSON().split('T')[1].replace(/\.\d*Z/, ' UTC')
}
function addPrefix(output: vscode.OutputChannel, prefix: string): vscode.OutputChannel {
return {
...output,
append(value: string) {
output.append(`[${prefix}] ${value}`)
},
appendLine(value: string) {
output.appendLine(`[${prefix}] ${value}`)
}
}
}
function promote(prefixPath: Array<string>, output: vscode.OutputChannel): IHierarchicalOutputChannel {
return {
...output,
prefixPath,
local(prefix) {
// this part was a PITA to figure out :-p
return promote([...prefixPath, prefix], addPrefix(output, prefix))
},
split() {
const name = [output.name, ...prefixPath].join('-')
output.appendLine(alloglot.ui.splittingOutputChannel(name))
return makeNoTimestamps(name)
}
}
}
}