tsc-output-format
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

tsc-output-format

Format Typescript compiler (tsc) diagnostic output into JSON, Github Actions Annotation and more.

License NPM Latest NPM Downloads NPM Min Size

CI CodeQL Codecov Type Coverage

  • 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 --pretty flag 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-format

Usage

CLI

CLI Options

  • --formatOnly: boolean
  • --formatOutput: raw | gha | grouped | groupedMin | json | jsonPretty | suppressed
  • Other tsc cli options, such as:
    • --watch
    • --noEmit
    • etc.

Compile and Format

# NODE
npx tsc-output-format --formatOutput=json

# BUN
bunx tsc-output-format --formatOutput=json

use -w or --watch flag for watch-mode.

# NODE
npx tsc-output-format --formatOutput=json --watch

# BUN
bunx tsc-output-format --formatOutput=json --watch

Format Only

# NODE
npx -p typescript tsc | npx tsc-output-format --formatOnly --formatOutput=json

# BUN
bunx tsc | bunx tsc-output-format --formatOnly --formatOutput=json

use -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 --watch

Parse 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 supressed = 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.

Package Sidebar

Install

npm i tsc-output-format

Weekly Downloads

8

Version

1.1.0

License

MIT

Unpacked Size

93 kB

Total Files

78

Last publish

Collaborators

  • codeismyid