Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Fix lint-codebase script #188

Merged
merged 8 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .github/workflows/test-lint-codebase.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# Workflow only meant to be manually run to test the `lint-codebase` command
# `lint-codebase` runs all rules in `eslint-plugin-n8n-nodes-base` (as they
# exist in this commit) on `n8n-io/n8n/packages/nodes-base`. The purpose is to
# ensure the linter runs successfully (does not crash) on the entire codebase.
# `lint-codebase` is part of the `ci-release` action.

# This GitHub action `test-lint-codebase` is meant to be run manually for debugging.

name: Test lint-codebase

on:
workflow_dispatch:

jobs:
test-lint-codebase-command:
test-lint-codebase:
runs-on: ubuntu-latest

steps:
Expand Down
58 changes: 32 additions & 26 deletions scripts/lint-codebase.sh
Original file line number Diff line number Diff line change
@@ -1,49 +1,54 @@
#!/bin/bash

# This script runs this commit of `eslint-plugin-n8n-nodes-base` on the
# n8n codebase to ensure the linter does not crash during the run.
# This script runs all rules in `eslint-plugin-n8n-nodes-base` (as they exist in
# this commit) on `n8n-io/n8n/packages/nodes-base`. The purpose is to ensure the
# linter runs successfully (does not crash) on the entire codebase during a CI
# run. The actual lint results can be disregarded.

# ----- #
# To do so, we build all rules locally and run ESLint on a lint config file that
# points to those locally built rules. Specifically, we set up an
# `.eslintrc.lc.js` file that enables the `eslint-plugin-local` plugin and two
# configs: `eslint-plugin-local/nodes` and `eslint-plugin-local/credentials` .
# Those configs made up of locally built rules are in an `.eslintplugin.js`
# file, which `eslint-plugin-local` uses to resolve the rules.

# ----------------------------------------------------------- #

# 0. Install dependencies with npm. We must use npm instead of pnpm to
# allow `eslint-plugin-local` to `require('../../.eslintplugin')`.
# The flag `--ignore-scripts` bypasses the pnpm block at `preinstall`.
# 0. Install dependencies. Use npm instead of pnpm to allow `eslint-plugin-local`
# to `require('../../.eslintplugin')`. `--ignore-scripts` bypasses the pnpm block at
# `preinstall`.

echo 'Step 0: Setting up...'
echo 'Step 0: Installing dependencies...'

npm install --ignore-scripts
npm install eslint-plugin-local
npm install eslint-plugin-local@1.0.0
pnpm build

# ----- #

# 1. `index.js` is the entrypoint to this plugin - it is copied during build
# to `/dist/index.js` and references the rules dir at `/dist/lib/rules`.
# This reference is relative to the entrypoint's location after copy.
# 1. `index.js` is the entrypoint to this plugin - it is copied during build to
# `/dist/index.js` and references the rules dir at `/dist/lib/rules`. This reference
# is relative to the entrypoint's location after copy.

# `eslint-plugin-local` requires the entrypoint to be named `.eslintplugin.js`
# and located at root, so we copy `index.js` into `.eslintplugin.js`, with
# the rules dir path adjusted relative to the root of the project.
# `eslint-plugin-local` requires the entrypoint to be named `.eslintplugin.js` and located
# at root, so we copy `index.js` into `.eslintplugin.js`, with the rules dir path adjusted
# relative to the root of the project.

echo 'Step 1: Creating .eslintplugin.js...'
echo 'Step 1: Creating `.eslintplugin.js`...'

node scripts/make-dot-eslintplugin-js.mjs

# ----- #

# 2. Fetch the entire codebase from the n8n repo at GitHub.

echo 'Step 2: Fetching codebase...'

node scripts/fetch-codebase.mjs

# ----- #

# 3. Run this commit of `eslint-plugin-n8n-nodes-base` on the codebase,
# using the ESLint config `eslintrc.lc.js` (lc -> lint codebase), which
# references `.eslintplugin.js` (all rules) via `eslint-plugin-local`.
# The flag `--no-eslintrc` prevents ESLint from finding and using
# the sibling `.eslintrc.js` that is intended for this plugin.
# 3. Run this commit of `eslint-plugin-n8n-nodes-base` on the codebase, using the ESLint
# config `eslintrc.lc.js` (lc -> lint codebase), which references `.eslintplugin.js`
# (all rules) via `eslint-plugin-local`. The flag `--no-eslintrc` prevents ESLint from
# finding and using the sibling `.eslintrc.js` that is intended for this plugin.

echo 'Step 3: Linting codebase...'

Expand All @@ -56,10 +61,11 @@ echo 'Step 3: Linting codebase...'
# ----- #

if [[ $? == 2 ]] ; then
echo 'ERROR: Linter crashed while running through codebase'
echo '❌ Error: Linter crashed while running through codebase'
echo 'Review and fix the failing lint rule(s) before releasing'
exit 1
fi

echo 'SUCCESS: Linter successfully ran through codebase'
echo 'Ignore any actual lint errors or warnings from the run'
echo '✅ Success: Linter successfully ran through codebase'
echo 'You can disregard any actual lint errors or warnings from the run'
exit 0
10 changes: 6 additions & 4 deletions scripts/make-dot-eslintplugin-js.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ const pluginIndexFile = path.resolve(DIST_DIR, "index.js");
shell.cp(pluginIndexFile, ".eslintplugin.js");

const oldContent = shell.cat(".eslintplugin.js");
const newContent = oldContent.replace(
/__dirname, "lib", "rules"/,
'__dirname, "dist", "lib", "rules"' // adjust reference based on new location (root)
);
const newContent = oldContent
.replace(
/__dirname, "lib", "rules"/,
'__dirname, "dist", "lib", "rules"' // adjust reference based on new location (root)
)
.replace(/n8n-nodes-base/g, "local");

new ShellString(newContent).to(".eslintplugin.js");

Expand Down
Loading