tsc-output-format
v1.1.1
Published
Format Typescript compiler (tsc) diagnostic output into JSON, GHA Annotations, and more
Maintainers
Readme
tsc-output-format
Format Typescript compiler (tsc) diagnostic output into JSON, Github Actions Annotation and more.
- Supports CLI and programmatic use: Use it directly from the command line for quick compile and formatting, or integrate it into your workflows programmatically.
- TSC command friendly: Works with the existing tsc CLI options.
- Works on Pretty Mode: Works with
--prettyflag enabled. - Supports Watch Mode: Use it directly from the command line to enable watch mode.
- GitHub-Action Output: Format into GitHub-Action annotations for more visible and actionable error reporting in CI-environment.
- JSON Output: Format into a structured JSON format for easy integration with other tools or workflows.
- Customizable: Leverage the Formatter and Parser blueprint to create custom formatter and parser.
Format Example
From:
src/index.ts(1,1): error TS1000: Unexpected Error.
1 const unexpected_error = unexpected_error;
~~~~~~~~~~~~~~~~
src/index2.ts:1:1 - error TS1000: Unexpected Error.
1 const unexpected_error = unexpected_error;
~~~~~~~~~~~~~~~~To GHA annotations:
::error title=TS Diagnostic (TS1000),file=src/index.ts,line=1,endLine=1,col=1::Unexpected Error.
::error title=TS Diagnostic (TS1000),file=src/index2.ts,line=1,endLine=1,col=1::Unexpected Error.To JSON Pretty:
[
{
"file": "src/index.ts",
"line": "1",
"column": "1",
"errorCode": "TS1000",
"message": "Unexpected Error",
"source": "1 const unexpected_error = unexpected_error;\n ~~~~~~~~~~~~~~~~",
"sourceClean": "const unexpected_error = unexpected_error;"
},
{
"file": "src/index2.ts",
"line": "2",
"column": "2",
"errorCode": "TS1000",
"message": "Unexpected Error",
"source": "1 const unexpected_error = unexpected_error;\n ~~~~~~~~~~~~~~~~",
"sourceClean": "const unexpected_error = unexpected_error;"
}
]To Grouped:
src/index.ts: found 1 errors.
src/index.ts(1,1): error TS1000: Unexpected Error.
1 const unexpected_error = unexpected_error;
~~~~~~~~~~~~~~~~
src/index2.ts: found 1 errors.
src/index.ts:1:1 - error TS1000: Unexpected Error.
1 const unexpected_error = unexpected_error;
~~~~~~~~~~~~~~~~To Grouped Minify:
src/index.ts: found 1 errors.
src/index2.ts: found 1 errors.To Suppressed:
suppressed 2 tsc errors.Installation
# NPM
npm install --save-dev tsc-output-format
# BUN
bun add -d tsc-output-formatUsage
CLI
CLI Options
--formatOnly:boolean--formatOutput:raw|gha|grouped|groupedMin|json|jsonPretty|suppressed- Other
tsccli options, such as:--watch--noEmit- etc.
Compile and Format
# NODE
npx tsc-output-format --formatOutput=json
# BUN
bunx tsc-output-format --formatOutput=jsonuse -w or --watch flag for watch-mode.
# NODE
npx tsc-output-format --formatOutput=json --watch
# BUN
bunx tsc-output-format --formatOutput=json --watchFormat Only
# NODE
npx -p typescript tsc | npx tsc-output-format --formatOnly --formatOutput=json
# BUN
bunx tsc | bunx tsc-output-format --formatOnly --formatOutput=jsonuse -w or --watch flag for watch-mode.
# NODE
npx -p typescript tsc --watch | npx tsc-output-format --formatOnly --formatOutput=json --watch
# BUN
bunx tsc --watch | bunx tsc-output-format --formatOnly --formatOutput=json --watchParse only
Not available with CLI.
Custom
Not available with CLI.
Programmatically
Compile and Format
Not available programmatically.
Format Only
import { Formatter } from "tsc-output-format";
const errors = `src/index.ts(1,1): error TS1000: Unexpected error.`;
const gha = Formatter.ghaFormatter.format(errors);
const json = Formatter.jsonFormatter.format(errors);
const jsonPretty = Formatter.jsonPrettyFormatter.format(errors);
const grouped = Formatter.groupedFormatter.format(errors);
const groupedMin = Formatter.groupedMinFormatter.format(errors);
const suppressed = Formatter.suppressedFormatter.format(errors);
// do anything with all outputs...Parse only
import { Parser } from "tsc-output-format";
const errors = `src/index.ts(1,1): error TS1000: Unexpected error.`;
const _default = Parser.defaultParser.parse(errors);
// do anything with parse result...Custom
import { Blueprint } from "tsc-output-format";
const errors = `
src/index.ts(1,1): error TS1000: Unexpected error.
src/index.ts(2,1): error TS1001: Unknown error.
`;
const errorCodeParser = new Blueprint.Parser(/^.*(TS\d+).*$/gm, ['errorCode']);
const errorCodeFormatter = new Blueprint.Formatter(errorCodeParser, (parseResults) => {
return JSON.stringify(parseResults.map(result => result.errorCode));
})
const errorCodeList = errorCodeFormatter.format(errors); // [TS1000, TS1001]Developed With
- Typescript - Strongly typed programming language that builds on JavaScript.
- Bun - All-in-one JavaScript runtime & toolkit designed for speed, complete with a bundler, test runner, and Node.js-compatible package manager.
License
The code in this project is released under the MIT License.
