Skip to content

Commit 49c4481

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 add55ec commit 49c4481

File tree

3 files changed

+70
-32
lines changed

3 files changed

+70
-32
lines changed

ci/dev/watch.ts

+1-32
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import browserify from "browserify"
22
import * as cp from "child_process"
33
import * as fs from "fs"
44
import * as path from "path"
5+
import { onLine } from "../../src/node/util"
56

67
async function main(): Promise<void> {
78
try {
@@ -97,38 +98,6 @@ class Watcher {
9798
path.join(this.rootPath, "out/browser/pages/vscode.js"),
9899
]
99100

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

src/node/util.ts

+32
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
/**

test/unit/node/util.test.ts

+37
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as cp from "child_process"
2+
import { generateUuid } from "../../../src/common/util"
13
import * as util from "../../../src/node/util"
24

35
describe("getEnvPaths", () => {
@@ -397,3 +399,38 @@ describe("sanitizeString", () => {
397399
expect(util.sanitizeString(" ")).toBe("")
398400
})
399401
})
402+
403+
describe("onLine", () => {
404+
// Spawn a process that outputs anything given on stdin.
405+
let proc: cp.ChildProcess | undefined
406+
407+
beforeAll(() => {
408+
proc = cp.spawn("node", ["-e", 'process.stdin.setEncoding("utf8");process.stdin.on("data", console.log)'])
409+
})
410+
411+
afterAll(() => {
412+
proc?.kill()
413+
})
414+
415+
it("should call with individual lines", async () => {
416+
const size = 100
417+
const received = new Promise<string[]>((resolve) => {
418+
const lines: string[] = []
419+
util.onLine(proc!, (line) => {
420+
lines.push(line)
421+
if (lines.length === size) {
422+
resolve(lines)
423+
}
424+
})
425+
})
426+
427+
const expected: string[] = []
428+
for (let i = 0; i < size; ++i) {
429+
expected.push(generateUuid(i))
430+
}
431+
432+
proc?.stdin?.write(expected.join("\n"))
433+
434+
expect(await received).toEqual(expected)
435+
})
436+
})

0 commit comments

Comments
 (0)