1
0
mirror of https://git.tuxpa.in/a/code-server.git synced 2024-12-28 13:15:25 +00:00

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.
This commit is contained in:
Asher 2021-06-23 15:57:16 -05:00
parent add55ecd62
commit 49c44818d9
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
3 changed files with 70 additions and 32 deletions

View File

@ -2,6 +2,7 @@ import browserify from "browserify"
import * as cp from "child_process"
import * as fs from "fs"
import * as path from "path"
import { onLine } from "../../src/node/util"
async function main(): Promise<void> {
try {
@ -97,38 +98,6 @@ class Watcher {
path.join(this.rootPath, "out/browser/pages/vscode.js"),
]
// From https://github.com/chalk/ansi-regex
const pattern = [
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))",
].join("|")
const re = new RegExp(pattern, "g")
/**
* Split stdout on newlines and strip ANSI codes.
*/
const onLine = (proc: cp.ChildProcess, callback: (strippedLine: string, originalLine: string) => void): void => {
let buffer = ""
if (!proc.stdout) {
throw new Error("no stdout")
}
proc.stdout.setEncoding("utf8")
proc.stdout.on("data", (d) => {
const data = buffer + d
const split = data.split("\n")
const last = split.length - 1
for (let i = 0; i < last; ++i) {
callback(split[i].replace(re, ""), split[i])
}
// The last item will either be an empty string (the data ended with a
// newline) or a partial line (did not end with a newline) and we must
// wait to parse it until we get a full line.
buffer = split[last]
})
}
let startingVscode = false
let startedVscode = false
onLine(vscode, (line, original) => {

View File

@ -17,6 +17,38 @@ export interface Paths {
runtime: string
}
// From https://github.com/chalk/ansi-regex
const pattern = [
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))",
].join("|")
const re = new RegExp(pattern, "g")
/**
* Split stdout on newlines and strip ANSI codes.
*/
export const onLine = (proc: cp.ChildProcess, callback: (strippedLine: string, originalLine: string) => void): void => {
let buffer = ""
if (!proc.stdout) {
throw new Error("no stdout")
}
proc.stdout.setEncoding("utf8")
proc.stdout.on("data", (d) => {
const data = buffer + d
const split = data.split("\n")
const last = split.length - 1
for (let i = 0; i < last; ++i) {
callback(split[i].replace(re, ""), split[i])
}
// The last item will either be an empty string (the data ended with a
// newline) or a partial line (did not end with a newline) and we must
// wait to parse it until we get a full line.
buffer = split[last]
})
}
export const paths = getEnvPaths()
/**

View File

@ -1,3 +1,5 @@
import * as cp from "child_process"
import { generateUuid } from "../../../src/common/util"
import * as util from "../../../src/node/util"
describe("getEnvPaths", () => {
@ -397,3 +399,38 @@ describe("sanitizeString", () => {
expect(util.sanitizeString(" ")).toBe("")
})
})
describe("onLine", () => {
// Spawn a process that outputs anything given on stdin.
let proc: cp.ChildProcess | undefined
beforeAll(() => {
proc = cp.spawn("node", ["-e", 'process.stdin.setEncoding("utf8");process.stdin.on("data", console.log)'])
})
afterAll(() => {
proc?.kill()
})
it("should call with individual lines", async () => {
const size = 100
const received = new Promise<string[]>((resolve) => {
const lines: string[] = []
util.onLine(proc!, (line) => {
lines.push(line)
if (lines.length === size) {
resolve(lines)
}
})
})
const expected: string[] = []
for (let i = 0; i < size; ++i) {
expected.push(generateUuid(i))
}
proc?.stdin?.write(expected.join("\n"))
expect(await received).toEqual(expected)
})
})