Skip to content

Commit

Permalink
feat: add eslint module
Browse files Browse the repository at this point in the history
Signed-off-by: Vasek - Tom C <[email protected]>
  • Loading branch information
TomChv committed Feb 12, 2024
1 parent 5efdf2a commit 4557e47
Show file tree
Hide file tree
Showing 11 changed files with 215 additions and 0 deletions.
44 changes: 44 additions & 0 deletions daggerverse.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"folders": [
{
"path": ""
},
{
"path": "dagger"
},
{
"path": "ci"
},
{
"path": "golang"
},
{
"path": "golangci-lint"
},
{
"path": "launcher"
},
{
"path": "minio"
},
{
"path": "node"
},
{
"path": "postgres"
},
{
"path": "redis"
},
{
"path": "redis-shell"
},
{
"path": "rust"
},
{
"path": "eslint"
},
],
"settings": {}
}
1 change: 1 addition & 0 deletions eslint/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/sdk/** linguist-generated
2 changes: 2 additions & 0 deletions eslint/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/sdk
/testdata
24 changes: 24 additions & 0 deletions eslint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Eslint module

A Typescript linter module for standalone typescript files and Node projects.

## Usage

:bulb: If the given files have no `package.json`, `eslintrc` config, or `tsconfig.json`, a default
one will be plugged into the container.

### Open a shell inside the linter container

```shell
dagger -m github.com/quartz-technology/daggerverse/eslint --files=<path> call container terminal
```

:bulb: It's a practical way to test the module / debug any issue that may happen inside the container.

### Run the linter on Typescript files

```shell
dagger -m github.com/quartz-technology/daggerverse/eslint --files=<path> call run stdout
```

Made with ❤️ by Quartz.
5 changes: 5 additions & 0 deletions eslint/dagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "eslint",
"sdk": "typescript",
"engineVersion": "v0.9.9"
}
8 changes: 8 additions & 0 deletions eslint/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"dependencies": {
"typescript": "^5.3.2"
},
"devDependencies": {
"@dagger.io/dagger": "./sdk"
}
}
12 changes: 12 additions & 0 deletions eslint/src/default-config/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
env: {
node: true
},
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
],
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
root: true,
}
21 changes: 21 additions & 0 deletions eslint/src/default-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^6.21.0",
"eslint": "^8.56.0",
"eslint-config-standard-with-typescript": "^43.0.1",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-n": "^16.6.2",
"eslint-plugin-promise": "^6.1.1",
"typescript": "^5.3.3"
}
}
8 changes: 8 additions & 0 deletions eslint/src/default-config/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"include": ["**/*.ts"],
"compilerOptions": {
"target": "ES2022",
"moduleResolution": "Node",
}
}

80 changes: 80 additions & 0 deletions eslint/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { dag, Container, Directory, object, func, field } from '@dagger.io/dagger';

@object()
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class Eslint {
/**
* The version of eslint to use (default to: 8.56.0).
*/
@field()
version = "8.56.0"

/**
* The version of node to use (default to: 21-alpine3.18).
*/
@field()
nodeVersion = "21-alpine3.18"

/**
* The files to lint.
*/
@field()
files: Directory

/*
* Default configuration files .
*/
defaultConfig = dag.currentModule().source().directory("src/default-config")

constructor(files: Directory, version?: string, nodeVersion?: string) {
this.version = version ?? this.version
this.nodeVersion = nodeVersion ?? this.nodeVersion
this.files = files ?? this.files
}

/**
* Return a container with eslint installed in it.
*
* Example usage: `dagger --files=. call container terminal`
*/
@func()
async container(): Promise<Container> {
let ctr = dag
.container()
.from(`node:${this.nodeVersion}`)
.withExec(["npm", "install", "-g", `eslint@${this.version}`])
.withMountedDirectory("/app", this.files)
.withWorkdir("/app")

// Check if there's an existing project configuration
// and add missing files if they are missing
const files = await ctr.directory(".").entries()
if (!files.find((f) => f === "package.json")) {
ctr = ctr.withFile("package.json", this.defaultConfig.file("package.json"))
}

if (!files.find((f) => /\.eslintrc\.(js|json|cjs)$/.test(f))) {
ctr = ctr.withFile(".eslintrc.js", this.defaultConfig.file(".eslintrc.js"))
}

if (!files.find((f) => f === "tsconfig.json")) {
ctr = ctr.withFile("tsconfig.json", this.defaultConfig.file("tsconfig.json"))
}

return ctr
.withExec(["npm", "install"])
.withEntrypoint(["eslint"])
.withDefaultTerminalCmd(["/bin/sh"])
}

/**
* Lint the files.
*
* Example usage: `dagger --files=. call run stdout`
*/
@func()
async run(...args: string[]): Promise<Container> {
return (await this.container())
.withExec([".", ...args])
}
}
10 changes: 10 additions & 0 deletions eslint/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "ES2022",
"moduleResolution": "Node",
"experimentalDecorators": true,
"paths": {
"@dagger.io/dagger": ["./sdk"]
}
}
}

0 comments on commit 4557e47

Please sign in to comment.