From 1a8129f64a5b718b39e667cae1c4fd41c2aa3ae5 Mon Sep 17 00:00:00 2001 From: gc <30398469+gc@users.noreply.github.com> Date: Sun, 24 Nov 2024 23:14:49 +1100 Subject: [PATCH] Use lint script for all linting --- package.json | 6 +----- scripts/lint.ts | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 scripts/lint.ts diff --git a/package.json b/package.json index 231c905c5ab..f85e1e9d8af 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "dev": "pnpm dev:stage1 && pnpm dev:stage2 && pnpm dev:stage3", "watch:tsc": "tsc -w -p src", "start": "pnpm watch", + "lint": "tsx scripts/lint.ts", "============SCRIPTS": "============", "spritesheet": "tsx ./scripts/spritesheet.ts && pnpm lint:biome", "data": "concurrently \"tsx ./scripts/monster_table.ts\" && pnpm lint", @@ -32,11 +33,6 @@ "test:docker": "docker compose up --no-attach db --no-attach redis --build --abort-on-container-exit && docker compose down -v", "test:watch": "vitest --config vitest.unit.config.mts --coverage", "test:ci:unit": "pnpm concurrent \"pnpm test:unit\" \"pnpm test:lint\" \"tsc -p tests/integration\" \"tsc -p tests/unit\"", - "============LINTING": "============", - "lint:biome": "git diff --name-only --diff-filter=d HEAD | xargs biome check --write --unsafe --diagnostic-level=error", - "lint:prettier": "prettier --use-tabs \"./**/*.{md,yml}\" --write --log-level silent", - "lint:prisma": "pnpm concurrent \"prisma format --schema ./prisma/robochimp.prisma\" \"prisma format --schema ./prisma/schema.prisma\"", - "lint": "pnpm concurrent \"pnpm lint:biome\" \"pnpm lint:prettier\"", "============MISC": "============", "concurrent": "concurrently --raw --kill-others-on-fail", "build:packages": "concurrently --raw \"pnpm --stream -r build:esbuild\" \"pnpm --stream -r build:types\"", diff --git a/scripts/lint.ts b/scripts/lint.ts new file mode 100644 index 00000000000..0310ebf67ca --- /dev/null +++ b/scripts/lint.ts @@ -0,0 +1,39 @@ +import { execSync } from 'node:child_process'; + +try { + const changedFiles = execSync( + 'git diff --name-only --diff-filter=AM HEAD && git ls-files --others --exclude-standard' + ) + .toString() + .trim() + .split('\n'); + + if (changedFiles.length > 0) { + execSync(`biome check ${changedFiles.join(' ')} --write --unsafe --diagnostic-level=error`, { + stdio: 'inherit' + }); + + // Prettier + const prettierFiles = changedFiles.filter(f => /\.(md|yaml)$/.exec(f)); + if (prettierFiles.length) { + console.log(`Prettier is formatting: ${prettierFiles.join(', ')}`); + execSync(`prettier --use-tabs ${prettierFiles.join(' ')} --write --log-level silent`, { + stdio: 'inherit' + }); + } else { + console.log('Nothing for prettier to format.'); + } + + // Prisma + if (changedFiles.some(f => f.includes('prisma/schema.prisma'))) { + console.log('Formatting Prisma schema files'); + execSync( + 'prisma format --schema ./prisma/robochimp.prisma && prisma format --schema ./prisma/schema.prisma', + { stdio: 'inherit' } + ); + } + } +} catch (error) { + console.error('Error occurred while running biome lint:', error); + process.exit(1); +}