Skip to content

Commit f1a2df1

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 094fc8f commit f1a2df1

File tree

3 files changed

+70
-32
lines changed

3 files changed

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

test/unit/node/util.test.ts

Lines changed: 37 additions & 0 deletions
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)