Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis-c committed Aug 16, 2024
1 parent d2775b2 commit 7ab04de
Show file tree
Hide file tree
Showing 11 changed files with 426 additions and 1 deletion.
62 changes: 62 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Continuous Integration

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
release:
types: [ created ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 16

- name: Install dependencies
run: npm ci

- name: Run test suite
run: npm run test

- name: Build
run: npm run build


publish:
needs: build
runs-on: ubuntu-latest
if: ${{ github.event_name == 'release' }}

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 16
registry-url: https://registry.npmjs.org/

- name: Install dependencies
run: npm ci

- name: Build
run: npm run build

- name: Set new version
run: npm version --allow-same-version --no-git-tag-version ${{ github.event.release.tag_name }}

- name: Publish release
run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea/
/node_modules/
/package-lock.json
102 changes: 101 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,101 @@
# typescript-gitlab-format
[![Continuous Integration](https://github.com/Moxio/typescript-gitlab-format/actions/workflows/ci.yml/badge.svg)](https://github.com/Moxio/typescript-gitlab-format/actions/workflows/ci.yml)
[![NPM version](https://img.shields.io/npm/v/typescript-gitlab-format.svg)](https://www.npmjs.com/package/typescript-gitlab-format)

typescript-gitlab-format
===================
Library for converting output of tsc --noEmit command to the gitlab ci codeclimate format, and filter unwanted entries.

Installation
------------
This library can be installed from the NPM package registry.
Depending on your use case it might be better to install this package globally
(if you want to run these commands from your continuous integration server for example)

Using NPM:
```
npm install typescript-gitlab-format
```
or Yarn
```
yarn add typescript-gitlab-format
```

Usage
-----
From the command line you can run this command
```
./node_modules/.bin/tsc --project ./tsconfig.json --noEmit | ./node_modules/.bin/typescript-gitlab-format -e [exclude regex] > build-logs/typescript-error.json"
```
There is one optional command line argument:

| short arg | long arg | effect |
|---|---|---|
| -e | --exclude | the javascript regex to determine which entries get excluded |

Provided filters
--------------

### -e --exclude
Passing along the -e or --exclude flag will exclude entries within the typescript output based on filepaths that match the given regular expression.
A check is performed using standard javascript regexp: `RegExp(..passes argument..).test(..tsc filepath..)`.
If this function returns true the file is skipped.

#### example
Given tsc output that produces errors in these files:
- `node_modules/example/example.d.ts`
- `example/example.d.ts`

Running this command:
`./node_modules/.bin/tsc --project ./tsconfig.json --noEmit | ./node_modules/.bin/typescript-gitlab-format -e "node_modules\\/" > build-logs/typescript-error.json"`

Will result in a file with these rules:
```json
[
{
"categories": [
"Compatibility"
],
"check_name": "TS7006",
"description": " Parameter 'node' implicitly has an 'any' type.\n",
"location": {
"path": "example/example.d.ts",
"positions": {
"begin": {
"column": 22,
"line": 296
},
"end": {
"column": 22,
"line": 296
}
}
},
"severity": "major",
"type": "issue"
}
]
```

Versioning
----------
This project adheres to [Semantic Versioning](http://semver.org/).

Contributing
------------
Contributions to this project are more than welcome.

License
-------
This project is released under the MIT license.

Treeware
--------
This package is [Treeware](https://treeware.earth/). If you use it in production,
then we'd appreciate it if you [**buy the world a tree**](https://plant.treeware.earth/Moxio/typescript-gitlab-format)
to thank us for our work. By contributing to the Treeware forest you'll be creating
employment for local families and restoring wildlife habitats.

---
Made with love, coffee and fun by the [Moxio](https://www.moxio.com) team from
Delft, The Netherlands. Interested in joining our awesome team? Check out our
[vacancies](https://werkenbij.moxio.com/) (in Dutch).
7 changes: 7 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// babel.config.js
module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript',
],
};
45 changes: 45 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "typescript-gitlab-format",
"version": "1.0.0",
"description": "Convert and filter tsc output into gitlab codeclimate format",
"keywords": [
"gitlab",
"tsc",
"typescript",
"format"
],
"scripts": {
"build": "tsc",
"test": "jest"
},
"bin": "dist/cli.js",
"files": [
"dist/"
],
"author": {
"name": "Moxio",
"email": "[email protected]",
"url": "https://www.moxio.com"
},
"repository": {
"type": "git",
"url": "https://github.com/Moxio/typescript-gitlab-format.git"
},
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.15.5",
"@babel/preset-env": "^7.15.6",
"@babel/preset-typescript": "^7.15.0",
"@types/jest": "^27.0.1",
"babel-jest": "^27.2.0",
"jest": "^27.2.0",
"ts-node": "^10.2.1"
},
"dependencies": {
"commander": "^8.2.0",
"typescript": "^4.4.3",
"@aivenio/tsc-output-parser": "^2.1.1",
"codeclimate-types": "^0.3.1",
"get-stdin": "^9.0.0"
}
}
25 changes: 25 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env node

import { Command } from "commander";
import getStdin from "get-stdin";
import format from "./format";
import filter from "./filter";

const program = new Command();

program
.option("-e --exclude <paths>", "paths to exclude, default js regex");


program.parse(process.argv);
const options = program.opts();

getStdin().then((stdIn) => {
let output = format(stdIn);

if (options.exclude) {
output = filter(output, new RegExp(options.exclude));
}

console.log(output);
});
7 changes: 7 additions & 0 deletions src/filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Issue } from "codeclimate-types";

export default function filter (input: Issue[], excludePattern: RegExp): Issue[] {
return input.filter((issue) => {
return excludePattern.test(issue.location.path) === false;
});
}
29 changes: 29 additions & 0 deletions src/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { parse, GrammarItem } from '@aivenio/tsc-output-parser';
import { Issue } from 'codeclimate-types';

export default function format (input: string): Issue[] {
const foo: GrammarItem[] = parse(input);

return foo.map((inputItem) => {
return {
type: 'issue',
categories: [ "Compatibility" ],
check_name: inputItem.value.tsError.value.errorString,
description: inputItem.value.message.value,
severity: 'major',
location: {
path: inputItem.value.path.value,
positions: {
begin: {
line: inputItem.value.cursor.value.line,
column: inputItem.value.cursor.value.col
},
end: {
line: inputItem.value.cursor.value.line,
column: inputItem.value.cursor.value.col
},
}
}
}
});
}
76 changes: 76 additions & 0 deletions test/filter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import filter from "../src/filter";

describe("filter", () => {
it("filters output", () => {
const output = filter([
{
"categories": [
"Compatibility"
],
"check_name": "TS7006",
"description": " Parameter 'node' implicitly has an 'any' type.\n",
"location": {
"path": "node_modules/example/example.d.ts",
"positions": {
"begin": {
"column": 22,
"line": 296
},
"end": {
"column": 22,
"line": 296
}
}
},
"severity": "major",
"type": "issue"
},
{
"categories": [
"Compatibility"
],
"check_name": "TS7006",
"description": " Parameter 'node' implicitly has an 'any' type.\n",
"location": {
"path": "example/example.d.ts",
"positions": {
"begin": {
"column": 22,
"line": 296
},
"end": {
"column": 22,
"line": 296
}
}
},
"severity": "major",
"type": "issue"
}
], new RegExp("node_modules\\/"));
expect(output).toEqual([
{
"categories": [
"Compatibility"
],
"check_name": "TS7006",
"description": " Parameter 'node' implicitly has an 'any' type.\n",
"location": {
"path": "example/example.d.ts",
"positions": {
"begin": {
"column": 22,
"line": 296
},
"end": {
"column": 22,
"line": 296
}
}
},
"severity": "major",
"type": "issue"
}
]);
})
});
Loading

0 comments on commit 7ab04de

Please sign in to comment.