Skip to content

Commit

Permalink
fix: filter ANSI color in the outpu
Browse files Browse the repository at this point in the history
fix 运行报告中存在乱码 #19
  • Loading branch information
zxch3n committed Apr 29, 2022
1 parent d4d4f70 commit 816be56
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
7 changes: 3 additions & 4 deletions src/pure/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { readFile } from "fs-extra";
import { tmpdir } from "os";
import { existsSync } from "fs";
import * as path from "path";
import { Readable } from "stream";

import { chunksToLinesAsync } from "@rauschma/stringio";
import { sanitizeFilePath } from "./utils";
import { filterColorFormatOutput, sanitizeFilePath } from "./utils";
import { isWindows } from "./platform";

export function getDebuggerConfig() {}
Expand Down Expand Up @@ -112,8 +111,8 @@ export class TestRunner {
chunksToLinesAsync(child.stderr),
)
) {
log(line.trimEnd() + "\r\n");
outputs.push(line);
log((line.trimEnd()) + "\r\n");
outputs.push(filterColorFormatOutput(line));
}
} catch (e) {
error = e;
Expand Down
17 changes: 12 additions & 5 deletions src/pure/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export function getVitestPath(projectRoot: string): string | undefined {
const suffixes = [".js", "", ".cmd"];
for (const suffix of suffixes) {
if (existsSync(path.resolve(node_modules, ".bin", "vitest" + suffix))) {
return sanitizeFilePath(path.resolve(node_modules, ".bin", "vitest" + suffix));
return sanitizeFilePath(
path.resolve(node_modules, ".bin", "vitest" + suffix),
);
}
}

Expand Down Expand Up @@ -48,13 +50,18 @@ export async function getVitestVersion(vitestPath?: string): Promise<string> {
throw new Error(`Cannot get vitest version from "${vitestPath}"`);
}

const capitalizeFirstLetter = (string:string)=> string.charAt(0).toUpperCase() + string.slice(1);
const capitalizeFirstLetter = (string: string) =>
string.charAt(0).toUpperCase() + string.slice(1);

const replaceDoubleSlashes = (string:string)=> string.replace(/\\/g, "/");
const replaceDoubleSlashes = (string: string) => string.replace(/\\/g, "/");

export function sanitizeFilePath(path: string) {
if(isWindows) {
return capitalizeFirstLetter(replaceDoubleSlashes(path));
if (isWindows) {
return capitalizeFirstLetter(replaceDoubleSlashes(path));
}
return path;
}

export function filterColorFormatOutput(s: string): string {
return s.replace(/\u001b\[\d+m/g, "");
}

0 comments on commit 816be56

Please sign in to comment.