Skip to content

Commit 23b13cf

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 8a307f0 commit 23b13cf

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,47 @@ jobs:
117117

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

Taskfile.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ tasks:
4949
- task: github:sync
5050
- task: js:fix
5151
- task: markdown:fix
52+
- task: npm:fix-config
5253
- task: python:format
5354
- task: shell:format
5455
vars:
@@ -730,6 +731,21 @@ tasks:
730731
cmds:
731732
- npm install
732733

734+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-npm-task/Taskfile.yml
735+
npm:fix-config:
736+
desc: |
737+
Fix problems with the npm configuration file.
738+
Environment variable parameters:
739+
- PROJECT_PATH: Path of the npm-managed project (default: {{.DEFAULT_NPM_PROJECT_PATH}}).
740+
dir: |
741+
"{{default "./" .PROJECT_PATH}}"
742+
cmds:
743+
- |
744+
npm \
745+
config \
746+
--location project \
747+
fix
748+
733749
# Parameter variables:
734750
# - PROJECT_PATH: path of the npm-managed project. Default value: "./"
735751
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-npm-task/Taskfile.yml

workflow-templates/assets/check-npm-task/Taskfile.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22
version: "3"
33

44
tasks:
5+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-npm-task/Taskfile.yml
6+
npm:fix-config:
7+
desc: |
8+
Fix problems with the npm configuration file.
9+
Environment variable parameters:
10+
- PROJECT_PATH: Path of the npm-managed project (default: ./).
11+
dir: |
12+
"{{default "./" .PROJECT_PATH}}"
13+
cmds:
14+
- |
15+
npm \
16+
config \
17+
--location project \
18+
fix
19+
520
# Parameter variables:
621
# - PROJECT_PATH: path of the npm-managed project. Default value: "./"
722
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-npm-task/Taskfile.yml

workflow-templates/check-npm-task.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,47 @@ jobs:
117117

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

0 commit comments

Comments
 (0)