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:
parent
add55ecd62
commit
49c44818d9
|
@ -2,6 +2,7 @@ import browserify from "browserify"
|
||||||
import * as cp from "child_process"
|
import * as cp from "child_process"
|
||||||
import * as fs from "fs"
|
import * as fs from "fs"
|
||||||
import * as path from "path"
|
import * as path from "path"
|
||||||
|
import { onLine } from "../../src/node/util"
|
||||||
|
|
||||||
async function main(): Promise<void> {
|
async function main(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
|
@ -97,38 +98,6 @@ class Watcher {
|
||||||
path.join(this.rootPath, "out/browser/pages/vscode.js"),
|
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 startingVscode = false
|
||||||
let startedVscode = false
|
let startedVscode = false
|
||||||
onLine(vscode, (line, original) => {
|
onLine(vscode, (line, original) => {
|
||||||
|
|
|
@ -17,6 +17,38 @@ export interface Paths {
|
||||||
runtime: string
|
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()
|
export const paths = getEnvPaths()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import * as cp from "child_process"
|
||||||
|
import { generateUuid } from "../../../src/common/util"
|
||||||
import * as util from "../../../src/node/util"
|
import * as util from "../../../src/node/util"
|
||||||
|
|
||||||
describe("getEnvPaths", () => {
|
describe("getEnvPaths", () => {
|
||||||
|
@ -397,3 +399,38 @@ describe("sanitizeString", () => {
|
||||||
expect(util.sanitizeString(" ")).toBe("")
|
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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
Loading…
Reference in New Issue