From 7479db1ed86220c64be04d2cf95ff3b04069db9d Mon Sep 17 00:00:00 2001 From: Callum Morris Date: Fri, 7 Jun 2024 12:19:47 +1200 Subject: [PATCH 1/3] feat: add reusable eslint action Runs the eslint defined in the given package.json, configured by the associated eslint.config.mjs file. --- .github/workflows/reusable-eslint.yml | 43 +++++++++++++++++++++++++++ README.md | 25 ++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 .github/workflows/reusable-eslint.yml diff --git a/.github/workflows/reusable-eslint.yml b/.github/workflows/reusable-eslint.yml new file mode 100644 index 0000000..033e414 --- /dev/null +++ b/.github/workflows/reusable-eslint.yml @@ -0,0 +1,43 @@ +name: reusable eslint +on: + workflow_call: + inputs: + paths: + required: true + type: string + description: | + A list of root directory/s to where a package.json and eslint + configuration file (eslint.config.mjs) exist. + e.g.: my/root/one my/cool/root/two +jobs: + eslint: + runs-on: ubuntu-latest + steps: + - if: ${{ startsWith(github.repository, 'GeoNet/') == false }} + name: require GeoNet org + run: | + exit 1 + - name: Parse paths + id: parse-paths + run: | + if [ -n "${{ inputs.paths }}" ]; then + echo "paths=$(echo '${{ inputs.paths }}' | tr '\n' ' ')" >> $GITHUB_OUTPUT + else + exit 1 + fi + - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 + - uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2 + with: + node-version: '20.x' + - name: Install Node modules + id: install-node-modules + env: + PATHS: ${{ steps.parse-paths.outputs.paths }} + run: | + echo "$PATHS" | tr ' ' '\n' | xargs -i -n 1 npm install --prefix '{}' + - name: Run ESLint + id: eslint + env: + PATHS: ${{ steps.parse-paths.outputs.paths }} + run: | + echo "$PATHS" | tr ' ' '\n' | xargs -I '{}' -n 1 sh -c 'ESLINT_USE_FLAT_CONFIG=true $1/node_modules/.bin/eslint -c $1/eslint.config.mjs "$1"' _ '{}' diff --git a/README.md b/README.md index 1dbe576..d3b5d25 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ - [Markdown lint](#markdown-lint) - [Copy to S3](#copy-to-s3) - [Clean container versions](#clean-container-versions) + - [ESLint](#eslint) - [Composite Actions](#composite-actions) - [Tagging](#tagging) - [Other documentation](#other-documentation) @@ -1092,6 +1093,30 @@ jobs: number-kept: 7 ``` +### ESLint + +STATUS: alpha + +Used to run ESLint on one or more directories. The paths specified +should have a package.json with eslint defined, alongside an eslint config +file named eslint.config.mjs. + +```yaml +name: eslint +on: + push: {} + pull_request: {} + workflow_dispatch: {} +jobs: + eslint: + uses: GeoNet/Actions/.github/workflows/reusable-eslint.yml@main + with: + paths: | + ./root/folder/one + ./cool/root/folder/two +``` + + ## Composite Actions ### Tagging From c284e9c36a7a823437cd8173b211ddd7a334fe98 Mon Sep 17 00:00:00 2001 From: Callum Morris Date: Thu, 13 Jun 2024 14:39:54 +1200 Subject: [PATCH 2/3] refactor: loop through paths more simply Removes complicated bash syntax --- .github/workflows/reusable-eslint.yml | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/.github/workflows/reusable-eslint.yml b/.github/workflows/reusable-eslint.yml index 033e414..8c9c993 100644 --- a/.github/workflows/reusable-eslint.yml +++ b/.github/workflows/reusable-eslint.yml @@ -28,16 +28,20 @@ jobs: - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 - uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2 with: - node-version: '20.x' + node-version: "20.x" - name: Install Node modules id: install-node-modules env: - PATHS: ${{ steps.parse-paths.outputs.paths }} + PATHS: ${{ inputs.paths }} run: | - echo "$PATHS" | tr ' ' '\n' | xargs -i -n 1 npm install --prefix '{}' + for path in $PATHS; do + npm install --prefix $path + done - name: Run ESLint id: eslint env: - PATHS: ${{ steps.parse-paths.outputs.paths }} + PATHS: ${{ inputs.paths }} run: | - echo "$PATHS" | tr ' ' '\n' | xargs -I '{}' -n 1 sh -c 'ESLINT_USE_FLAT_CONFIG=true $1/node_modules/.bin/eslint -c $1/eslint.config.mjs "$1"' _ '{}' + for path in $PATHS; do + ESLINT_USE_FLAT_CONFIG=true $path/node_modules/.bin/eslint -c $path/eslint.config.mjs + done From cccf4719c7c51285b7bea51548a478967f952674 Mon Sep 17 00:00:00 2001 From: Callum Morris Date: Thu, 13 Jun 2024 14:43:12 +1200 Subject: [PATCH 3/3] refactor: remove unrequired parse step Now that it's using inputs.paths directly, don't need this parse step. Also, paths is required, so no need to check that paths have been given --- .github/workflows/reusable-eslint.yml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/.github/workflows/reusable-eslint.yml b/.github/workflows/reusable-eslint.yml index 8c9c993..2223cad 100644 --- a/.github/workflows/reusable-eslint.yml +++ b/.github/workflows/reusable-eslint.yml @@ -17,14 +17,6 @@ jobs: name: require GeoNet org run: | exit 1 - - name: Parse paths - id: parse-paths - run: | - if [ -n "${{ inputs.paths }}" ]; then - echo "paths=$(echo '${{ inputs.paths }}' | tr '\n' ' ')" >> $GITHUB_OUTPUT - else - exit 1 - fi - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 - uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2 with: