Skip to content

Commit

Permalink
fix: Use stdout and stderr instead of console
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Jul 18, 2024
1 parent f7d3aaf commit cba5bcc
Show file tree
Hide file tree
Showing 24 changed files with 2,591 additions and 262 deletions.
188 changes: 188 additions & 0 deletions packages/cspell-json-reporter/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,194 @@ exports[`getReporter > saves additional data 1`] = `
}"
`;

exports[`getReporter > saves json to console { outFile: 'stderr' } 1`] = `
"{
"issues": [
{
"text": "fulll",
"offset": 13,
"line": {
"text": "This text is fulll of errrorrrs.",
"offset": 0
},
"row": 1,
"col": 14,
"uri": "text.txt",
"context": {
"text": "This text is fulll of errrorrrs.",
"offset": 0
}
}
],
"info": [
{
"message": "some warnings",
"msgType": "Warning"
}
],
"debug": [],
"error": [
{
"message": "something went wrong",
"error": {}
}
],
"progress": [],
"result": {
"files": 1,
"filesWithIssues": [
"text.txt"
],
"issues": 2,
"errors": 1,
"cachedFiles": 0
}
}"
`;

exports[`getReporter > saves json to console { outFile: 'stderr' } 2`] = `""`;

exports[`getReporter > saves json to console { outFile: 'stdout' } 1`] = `""`;

exports[`getReporter > saves json to console { outFile: 'stdout' } 2`] = `
"{
"issues": [
{
"text": "fulll",
"offset": 13,
"line": {
"text": "This text is fulll of errrorrrs.",
"offset": 0
},
"row": 1,
"col": 14,
"uri": "text.txt",
"context": {
"text": "This text is fulll of errrorrrs.",
"offset": 0
}
}
],
"info": [
{
"message": "some warnings",
"msgType": "Warning"
}
],
"debug": [],
"error": [
{
"message": "something went wrong",
"error": {}
}
],
"progress": [],
"result": {
"files": 1,
"filesWithIssues": [
"text.txt"
],
"issues": 2,
"errors": 1,
"cachedFiles": 0
}
}"
`;

exports[`getReporter > saves json to console { outFile: undefined } 1`] = `""`;

exports[`getReporter > saves json to console { outFile: undefined } 2`] = `
"{
"issues": [
{
"text": "fulll",
"offset": 13,
"line": {
"text": "This text is fulll of errrorrrs.",
"offset": 0
},
"row": 1,
"col": 14,
"uri": "text.txt",
"context": {
"text": "This text is fulll of errrorrrs.",
"offset": 0
}
}
],
"info": [
{
"message": "some warnings",
"msgType": "Warning"
}
],
"debug": [],
"error": [
{
"message": "something went wrong",
"error": {}
}
],
"progress": [],
"result": {
"files": 1,
"filesWithIssues": [
"text.txt"
],
"issues": 2,
"errors": 1,
"cachedFiles": 0
}
}"
`;

exports[`getReporter > saves json to console undefined 1`] = `""`;

exports[`getReporter > saves json to console undefined 2`] = `
"{
"issues": [
{
"text": "fulll",
"offset": 13,
"line": {
"text": "This text is fulll of errrorrrs.",
"offset": 0
},
"row": 1,
"col": 14,
"uri": "text.txt",
"context": {
"text": "This text is fulll of errrorrrs.",
"offset": 0
}
}
],
"info": [
{
"message": "some warnings",
"msgType": "Warning"
}
],
"debug": [],
"error": [
{
"message": "something went wrong",
"error": {}
}
],
"progress": [],
"result": {
"files": 1,
"filesWithIssues": [
"text.txt"
],
"issues": 2,
"errors": 1,
"cachedFiles": 0
}
}"
`;

exports[`getReporter > saves json to file 1`] = `
"{
"issues": [
Expand Down
19 changes: 19 additions & 0 deletions packages/cspell-json-reporter/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ describe('getReporter', () => {
expect(joinCalls(stdout.mock.calls)).toMatchSnapshot();
});

test.each`
settings
${undefined}
${{ outFile: undefined }}
${{ outFile: 'stdout' }}
${{ outFile: 'stderr' }}
`('saves json to console $settings', async ({ settings }) => {
const console = {
log: vi.fn(),
error: vi.fn(),
warn: vi.fn(),
};

const reporter = getReporter(settings, { console });
await runReporter(reporter);
expect(joinCalls(console.error.mock.calls)).toMatchSnapshot();
expect(joinCalls(console.log.mock.calls)).toMatchSnapshot();
});

test('saves additional data', async () => {
const reporter = getReporter({ outFile: 'out.json', verbose: true, debug: true, progress: true });
await runReporter(reporter);
Expand Down
11 changes: 10 additions & 1 deletion packages/cspell-json-reporter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,26 @@ function mkdirp(p: string) {

const noopReporter = () => undefined;

type ReporterConsole = Pick<Console, 'log' | 'warn' | 'error'>;

export interface CSpellJSONReporterConfiguration extends ReporterConfiguration {
console?: ReporterConsole;
}

const STDOUT = 'stdout';
const STDERR = 'stderr';

type Data = Omit<CSpellJSONReporterOutput, 'result'>;

const _console = console;

export function getReporter(
settings: unknown | CSpellJSONReporterSettings,
cliOptions?: ReporterConfiguration,
cliOptions?: CSpellJSONReporterConfiguration,
): Required<CSpellReporter> {
const useSettings = normalizeSettings(settings);
const reportData: Data = { issues: [], info: [], debug: [], error: [], progress: [] };
const console = cliOptions?.console ?? _console;
return {
issue: (issue) => {
reportData.issues.push(issue);
Expand Down
5 changes: 4 additions & 1 deletion packages/cspell/bin.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#!/usr/bin/env node
import { format } from 'node:util';

import { CommanderError, program } from 'commander';

import * as app from './dist/esm/app.mjs';

app.run(program, process.argv).catch((e) => {
if (!(e instanceof CommanderError) && !(e instanceof app.CheckFailed)) {
console.log(e);
const msg = format(e) + '\n';
process.stdout.write(msg);
// It is possible an explicit exit code was set, use it if it was.
process.exitCode = process.exitCode || 1;
}
Expand Down
1 change: 1 addition & 0 deletions packages/cspell/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"@cspell/cspell-pipe": "workspace:*",
"@cspell/cspell-types": "workspace:*",
"@cspell/dynamic-import": "workspace:*",
"ansi-escapes": "^7.0.0",
"chalk": "^5.3.0",
"chalk-template": "^1.1.0",
"commander": "^12.1.0",
Expand Down
Loading

0 comments on commit cba5bcc

Please sign in to comment.