From 4557e47fed55f885e615e9960d8bd3fa61d05772 Mon Sep 17 00:00:00 2001 From: Vasek - Tom C Date: Mon, 12 Feb 2024 23:38:56 +0100 Subject: [PATCH] feat: add eslint module Signed-off-by: Vasek - Tom C --- daggerverse.code-workspace | 44 ++++++++++++++ eslint/.gitattributes | 1 + eslint/.gitignore | 2 + eslint/README.md | 24 ++++++++ eslint/dagger.json | 5 ++ eslint/package.json | 8 +++ eslint/src/default-config/.eslintrc.js | 12 ++++ eslint/src/default-config/package.json | 21 +++++++ eslint/src/default-config/tsconfig.json | 8 +++ eslint/src/index.ts | 80 +++++++++++++++++++++++++ eslint/tsconfig.json | 10 ++++ 11 files changed, 215 insertions(+) create mode 100644 daggerverse.code-workspace create mode 100644 eslint/.gitattributes create mode 100644 eslint/.gitignore create mode 100644 eslint/README.md create mode 100644 eslint/dagger.json create mode 100644 eslint/package.json create mode 100644 eslint/src/default-config/.eslintrc.js create mode 100644 eslint/src/default-config/package.json create mode 100644 eslint/src/default-config/tsconfig.json create mode 100644 eslint/src/index.ts create mode 100644 eslint/tsconfig.json diff --git a/daggerverse.code-workspace b/daggerverse.code-workspace new file mode 100644 index 0000000..246eca9 --- /dev/null +++ b/daggerverse.code-workspace @@ -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": {} +} \ No newline at end of file diff --git a/eslint/.gitattributes b/eslint/.gitattributes new file mode 100644 index 0000000..8274184 --- /dev/null +++ b/eslint/.gitattributes @@ -0,0 +1 @@ +/sdk/** linguist-generated diff --git a/eslint/.gitignore b/eslint/.gitignore new file mode 100644 index 0000000..91a784a --- /dev/null +++ b/eslint/.gitignore @@ -0,0 +1,2 @@ +/sdk +/testdata \ No newline at end of file diff --git a/eslint/README.md b/eslint/README.md new file mode 100644 index 0000000..5864fb3 --- /dev/null +++ b/eslint/README.md @@ -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= 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= call run stdout +``` + +Made with ❤️ by Quartz. \ No newline at end of file diff --git a/eslint/dagger.json b/eslint/dagger.json new file mode 100644 index 0000000..ccecb10 --- /dev/null +++ b/eslint/dagger.json @@ -0,0 +1,5 @@ +{ + "name": "eslint", + "sdk": "typescript", + "engineVersion": "v0.9.9" +} diff --git a/eslint/package.json b/eslint/package.json new file mode 100644 index 0000000..0464116 --- /dev/null +++ b/eslint/package.json @@ -0,0 +1,8 @@ +{ + "dependencies": { + "typescript": "^5.3.2" + }, + "devDependencies": { + "@dagger.io/dagger": "./sdk" + } +} diff --git a/eslint/src/default-config/.eslintrc.js b/eslint/src/default-config/.eslintrc.js new file mode 100644 index 0000000..2e49194 --- /dev/null +++ b/eslint/src/default-config/.eslintrc.js @@ -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, + } \ No newline at end of file diff --git a/eslint/src/default-config/package.json b/eslint/src/default-config/package.json new file mode 100644 index 0000000..e86d3e5 --- /dev/null +++ b/eslint/src/default-config/package.json @@ -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" + } +} \ No newline at end of file diff --git a/eslint/src/default-config/tsconfig.json b/eslint/src/default-config/tsconfig.json new file mode 100644 index 0000000..51e0534 --- /dev/null +++ b/eslint/src/default-config/tsconfig.json @@ -0,0 +1,8 @@ +{ + "include": ["**/*.ts"], + "compilerOptions": { + "target": "ES2022", + "moduleResolution": "Node", + } +} + \ No newline at end of file diff --git a/eslint/src/index.ts b/eslint/src/index.ts new file mode 100644 index 0000000..ee09130 --- /dev/null +++ b/eslint/src/index.ts @@ -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 { + 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 { + return (await this.container()) + .withExec([".", ...args]) + } +} diff --git a/eslint/tsconfig.json b/eslint/tsconfig.json new file mode 100644 index 0000000..0327130 --- /dev/null +++ b/eslint/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "target": "ES2022", + "moduleResolution": "Node", + "experimentalDecorators": true, + "paths": { + "@dagger.io/dagger": ["./sdk"] + } + } +}