Skip to content

Commit 774d29a

Browse files
committed
Move onLine to utilities
This way it can be used by the tests when spawning code-server on a random port to look for the address.
1 parent d56bda2 commit 774d29a

File tree

2 files changed

+33
-32
lines changed

2 files changed

+33
-32
lines changed

ci/dev/watch.ts

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as cp from "child_process"
22
import Bundler from "parcel-bundler"
33
import * as path from "path"
4+
import { onLine } from "../../src/node/util"
45

56
async function main(): Promise<void> {
67
try {
@@ -101,38 +102,6 @@ class Watcher {
101102
plugin.stderr.on("data", (d) => process.stderr.write(d))
102103
}
103104

104-
// From https://github.com/chalk/ansi-regex
105-
const pattern = [
106-
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
107-
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))",
108-
].join("|")
109-
const re = new RegExp(pattern, "g")
110-
111-
/**
112-
* Split stdout on newlines and strip ANSI codes.
113-
*/
114-
const onLine = (proc: cp.ChildProcess, callback: (strippedLine: string, originalLine: string) => void): void => {
115-
let buffer = ""
116-
if (!proc.stdout) {
117-
throw new Error("no stdout")
118-
}
119-
proc.stdout.setEncoding("utf8")
120-
proc.stdout.on("data", (d) => {
121-
const data = buffer + d
122-
const split = data.split("\n")
123-
const last = split.length - 1
124-
125-
for (let i = 0; i < last; ++i) {
126-
callback(split[i].replace(re, ""), split[i])
127-
}
128-
129-
// The last item will either be an empty string (the data ended with a
130-
// newline) or a partial line (did not end with a newline) and we must
131-
// wait to parse it until we get a full line.
132-
buffer = split[last]
133-
})
134-
}
135-
136105
let startingVscode = false
137106
let startedVscode = false
138107
onLine(vscode, (line, original) => {

src/node/util.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,38 @@ export interface Paths {
1717
runtime: string
1818
}
1919

20+
// From https://github.com/chalk/ansi-regex
21+
const pattern = [
22+
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
23+
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))",
24+
].join("|")
25+
const re = new RegExp(pattern, "g")
26+
27+
/**
28+
* Split stdout on newlines and strip ANSI codes.
29+
*/
30+
export const onLine = (proc: cp.ChildProcess, callback: (strippedLine: string, originalLine: string) => void): void => {
31+
let buffer = ""
32+
if (!proc.stdout) {
33+
throw new Error("no stdout")
34+
}
35+
proc.stdout.setEncoding("utf8")
36+
proc.stdout.on("data", (d) => {
37+
const data = buffer + d
38+
const split = data.split("\n")
39+
const last = split.length - 1
40+
41+
for (let i = 0; i < last; ++i) {
42+
callback(split[i].replace(re, ""), split[i])
43+
}
44+
45+
// The last item will either be an empty string (the data ended with a
46+
// newline) or a partial line (did not end with a newline) and we must
47+
// wait to parse it until we get a full line.
48+
buffer = split[last]
49+
})
50+
}
51+
2052
export const paths = getEnvPaths()
2153

2254
/**

0 commit comments

Comments
 (0)