From 5e5145a94f58642094cc25a4d8d7ae627a0548da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Sun, 12 Feb 2023 17:19:50 +0100 Subject: [PATCH] Refactor `lint-codebase` command (#151) --- .github/workflows/checks.yml | 26 ----------------- .github/workflows/on-push.yml | 26 +++++++++++++++++ .../{publish-to-npm.yml => on-release.yml} | 15 +++++----- .gitignore | 3 +- jest.config.js | 2 +- lint-codebase-config.js | 4 ++- package.json | 6 ++-- scripts/get-codebase.mjs | 7 ++++- scripts/lint-codebase.sh | 28 +++++++++++++++++-- scripts/make-lint-codebase-config.mjs | 3 ++ 10 files changed, 76 insertions(+), 44 deletions(-) delete mode 100644 .github/workflows/checks.yml create mode 100644 .github/workflows/on-push.yml rename .github/workflows/{publish-to-npm.yml => on-release.yml} (67%) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml deleted file mode 100644 index 243590f..0000000 --- a/.github/workflows/checks.yml +++ /dev/null @@ -1,26 +0,0 @@ -name: Node CI - -on: push - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - name: Check out commit - uses: actions/checkout@v3 - - - name: Set up Node.js - uses: actions/setup-node@v3 - with: - node-version: '16' - cache: 'npm' - - - name: Run checks - run: | - npm install - npm run lint-plugin - npm test - npm run build - env: - CI: true diff --git a/.github/workflows/on-push.yml b/.github/workflows/on-push.yml new file mode 100644 index 0000000..a194cbc --- /dev/null +++ b/.github/workflows/on-push.yml @@ -0,0 +1,26 @@ +name: On push + +on: push + +jobs: + run-checks: + runs-on: ubuntu-latest + + steps: + - name: Check out commit + uses: actions/checkout@v3 + + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: "16" + cache: "npm" + + - name: Run plugin checks + run: | + npm ci + npm run lint-plugin + npm test + npm run typecheck + env: + CI: true diff --git a/.github/workflows/publish-to-npm.yml b/.github/workflows/on-release.yml similarity index 67% rename from .github/workflows/publish-to-npm.yml rename to .github/workflows/on-release.yml index e923179..a3fc761 100644 --- a/.github/workflows/publish-to-npm.yml +++ b/.github/workflows/on-release.yml @@ -1,11 +1,11 @@ -name: Publish to npm +name: On release on: release: types: [created] jobs: - build: + publish-to-npm: runs-on: ubuntu-latest steps: @@ -18,11 +18,10 @@ jobs: node-version: 16 registry-url: https://registry.npmjs.org - - name: Check codebase lint, publish - run: | - npm ci - npm run build - npm run lint-codebase - npm publish + - name: Lint codebase + run: npm run lint-codebase + + - name: Publish to npm + run: npm publish env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.gitignore b/.gitignore index d78d721..bcee1d2 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ node_modules .DS_Store .todo scripts/downloads/* -.eslintplugin.js \ No newline at end of file +.eslintplugin.js +lint-run.log \ No newline at end of file diff --git a/jest.config.js b/jest.config.js index 408deeb..5b6e807 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,5 +1,5 @@ module.exports = { testEnvironment: "node", transform: { "^.+\\.ts$": "esbuild-jest" }, - testPathIgnorePatterns: ["/dist/", "/node_modules/"], + testPathIgnorePatterns: ["/dist/", "/node_modules/", "/scripts/downloads"], }; diff --git a/lint-codebase-config.js b/lint-codebase-config.js index 7bdc22b..7024c3a 100644 --- a/lint-codebase-config.js +++ b/lint-codebase-config.js @@ -1,5 +1,7 @@ /** - * Config for the `lint-codebase` command. + * Base config for the `lint-codebase` command. + * + * Implicitly references rules in `.eslintplugin.js` via `eslint-plugin-local`. * * @type {import('@types/eslint').ESLint.ConfigData} */ diff --git a/package.json b/package.json index c44b909..94dc4d4 100644 --- a/package.json +++ b/package.json @@ -21,13 +21,13 @@ ], "scripts": { "build": "node scripts/build.mjs", + "typecheck": "tsc --noEmit", "doc": "npm run build; node scripts/make-docs.mjs", "format": "prettier lib scripts tests --write", "lint-plugin": "eslint lib tests --ext .ts", - "lint-codebase": "node scripts/get-codebase.mjs && node scripts/make-lint-codebase-config.mjs && npm install eslint-plugin-n8n-nodes-base && chmod +x ./scripts/lint-codebase.sh && ./scripts/lint-codebase.sh", + "lint-codebase": "chmod +x scripts/lint-codebase.sh && scripts/lint-codebase.sh", "test": "jest", - "test:watch": "jest --watch", - "prepublishOnly": "npm run lint-plugin && npm run test && npm run build && npm run lint-codebase" + "test:watch": "jest --watch" }, "dependencies": { "@typescript-eslint/utils": "^5.17.0", diff --git a/scripts/get-codebase.mjs b/scripts/get-codebase.mjs index be934ec..985a280 100644 --- a/scripts/get-codebase.mjs +++ b/scripts/get-codebase.mjs @@ -38,7 +38,12 @@ async function getFilenames() { }); return files.filter( - (file) => file.path.endsWith(".ts") && !file.path.endsWith(".d.ts") + (file) => + file.path.endsWith(".ts") && + !file.path.endsWith(".d.ts") && + !file.path.endsWith(".test.ts") && + !file.path.includes("nodes-base/test") && + !file.path.includes("nodes-base/utils") ); } diff --git a/scripts/lint-codebase.sh b/scripts/lint-codebase.sh index 256545a..71d8c9b 100755 --- a/scripts/lint-codebase.sh +++ b/scripts/lint-codebase.sh @@ -1,12 +1,34 @@ #!/bin/bash -echo 'Linting codebase...' +echo 'Step 1: Setting up linter...' -./node_modules/eslint/bin/eslint.js --no-eslintrc --config lint-codebase-config.js --ext .ts scripts/downloads +npm ci +npm run build +node scripts/make-lint-codebase-config.mjs + +# `make-lint-codebase-config.mjs` creates `.eslintplugin.js`, based on +# `dist/index.js` (same as `index.js`) but with the rules dir path adjusted +# relative to the root. `.eslintplugin.js` contains all the linter's rules +# and is referenced implicitly by `lint-codebase-config.js`` + +echo 'Step 2: Fetching codebase...' + +node scripts/get-codebase.mjs + +echo 'Step 3: Linting codebase...' + +./node_modules/eslint/bin/eslint.js \ + --no-eslintrc \ + --config lint-codebase-config.js \ + --ext .ts \ + scripts/downloads \ + > lint-run.log if [[ $? == 2 ]] ; then - echo "Linter crashed while linting codebase" + echo 'Linter crashed while running through codebase' exit 1 fi +echo 'Linter successfully ran through codebase' + exit 0 \ No newline at end of file diff --git a/scripts/make-lint-codebase-config.mjs b/scripts/make-lint-codebase-config.mjs index c328fa1..535d9cd 100644 --- a/scripts/make-lint-codebase-config.mjs +++ b/scripts/make-lint-codebase-config.mjs @@ -25,4 +25,7 @@ const newContent = oldContent.replace( /__dirname, "lib", "rules"/, '__dirname, "dist", "lib", "rules"' // adjust reference based on new (root) location ); + new ShellString(newContent).to(".eslintplugin.js"); + +console.log("Created .eslintplugin.js"); \ No newline at end of file