Skip to content

Commit 5631824

Browse files
committed
Check/fix problems with npm configuration
The "Check npm" template provides actions and a GitHub Actions workflow used to check for problems with a project's npm configuration files. In addition to the `package.json` file previously used, we are now using an `.npmrc` configuration file. It will be useful to have some validation for this file. npm provides a `config fix` command which automatically fixes any problems that are detected with the npm configuration. In addition to using it for that purpose, it can also serve as a check by running the command via the GitHub Actions workflow, then checking for any diff. Beyond the automated fixes, it is hoped that the parsing of the configuration that npm must perform to check for any needed fixes provides a basic validation of the configuration. Unfortunately npm's parser is extremely lenient, so the validation is not at all comprehensive. However, it is probably better than nothing, simple to implement, and no alternatives were found.
1 parent 4f64f22 commit 5631824

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

.github/workflows/check-npm-task.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,45 @@ jobs:
125125
--color \
126126
--exit-code \
127127
"${{ matrix.project.path }}/package-lock.json"
128+
129+
check-config:
130+
name: check-config (${{ matrix.project.path }})
131+
needs: run-determination
132+
if: needs.run-determination.outputs.result == 'true'
133+
runs-on: ubuntu-latest
134+
permissions:
135+
contents: read
136+
137+
strategy:
138+
fail-fast: false
139+
matrix:
140+
project:
141+
# TODO: add paths of all npm-managed projects in the repository here.
142+
- path: .
143+
144+
steps:
145+
- name: Checkout repository
146+
uses: actions/checkout@v5
147+
148+
- name: Setup Node.js
149+
uses: actions/setup-node@v4
150+
with:
151+
node-version-file: "${{ matrix.project.path }}/package.json"
152+
153+
- name: Install Task
154+
uses: arduino/setup-task@v2
155+
with:
156+
repo-token: ${{ secrets.GITHUB_TOKEN }}
157+
version: 3.x
158+
159+
- name: Fix problems in npm configuration file
160+
run: |
161+
task npm:fix-config \
162+
PROJECT_PATH="${{ matrix.project.path }}"
163+
164+
- name: Check if fixes are needed in npm configuration file
165+
run: |
166+
git diff \
167+
--color \
168+
--exit-code \
169+
"${{ matrix.project.path }}/.npmrc"

.npmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# See: https://docs.npmjs.com/cli/configuring-npm/npmrc
22

3-
engine-strict = true
3+
engine-strict=true

Taskfile.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,19 @@ tasks:
307307
markdownlint-cli \
308308
"**/*.md"
309309
310+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-npm-task/Taskfile.yml
311+
npm:fix-config:
312+
desc: |
313+
Fix problems with the npm configuration file.
314+
Environment variable parameters:
315+
- PROJECT_PATH: Path of the npm-managed project (default: {{.DEFAULT_NPM_PROJECT_PATH}}).
316+
dir: "{{default .DEFAULT_NPM_PROJECT_PATH .PROJECT_PATH}}"
317+
cmds:
318+
- |
319+
npm config \
320+
--location project \
321+
fix
322+
310323
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/npm-task/Taskfile.yml
311324
npm:install-deps:
312325
desc: |

0 commit comments

Comments
 (0)