Skip to content

Commit 64e2156

Browse files
Merge pull request #60 from MatteoPologruto/check-npm
Add CI workflow to check for problems with npm configuration files
2 parents 39d6187 + 19b6bb9 commit 64e2156

File tree

3 files changed

+207
-0
lines changed

3 files changed

+207
-0
lines changed

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

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-npm-task.md
2+
name: Check npm
3+
4+
env:
5+
# See: https://github.com/actions/setup-node/#readme
6+
NODE_VERSION: 16.x
7+
8+
# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows
9+
on:
10+
create:
11+
push:
12+
paths:
13+
- ".github/workflows/check-npm-task.ya?ml"
14+
- "**/package.json"
15+
- "**/package-lock.json"
16+
- "Taskfile.ya?ml"
17+
pull_request:
18+
paths:
19+
- ".github/workflows/check-npm-task.ya?ml"
20+
- "**/package.json"
21+
- "**/package-lock.json"
22+
- "Taskfile.ya?ml"
23+
schedule:
24+
# Run every Tuesday at 8 AM UTC to catch breakage resulting from changes to the JSON schema.
25+
- cron: "0 8 * * TUE"
26+
workflow_dispatch:
27+
repository_dispatch:
28+
29+
permissions:
30+
contents: read
31+
32+
jobs:
33+
run-determination:
34+
runs-on: ubuntu-latest
35+
outputs:
36+
result: ${{ steps.determination.outputs.result }}
37+
steps:
38+
- name: Determine if the rest of the workflow should run
39+
id: determination
40+
run: |
41+
RELEASE_BRANCH_REGEX="refs/heads/[0-9]+.[0-9]+.x"
42+
# The `create` event trigger doesn't support `branches` filters, so it's necessary to use Bash instead.
43+
if [[
44+
"${{ github.event_name }}" != "create" ||
45+
"${{ github.ref }}" =~ $RELEASE_BRANCH_REGEX
46+
]]; then
47+
# Run the other jobs.
48+
RESULT="true"
49+
else
50+
# There is no need to run the other jobs.
51+
RESULT="false"
52+
fi
53+
54+
echo "result=$RESULT" >> $GITHUB_OUTPUT
55+
56+
validate:
57+
needs: run-determination
58+
if: needs.run-determination.outputs.result == 'true'
59+
runs-on: ubuntu-latest
60+
61+
steps:
62+
- name: Checkout repository
63+
uses: actions/checkout@v3
64+
65+
- name: Setup Node.js
66+
uses: actions/setup-node@v3
67+
with:
68+
node-version: ${{ env.NODE_VERSION }}
69+
70+
- name: Install Task
71+
uses: arduino/setup-task@v1
72+
with:
73+
repo-token: ${{ secrets.GITHUB_TOKEN }}
74+
version: 3.x
75+
76+
- name: Validate package.json
77+
run: task --silent npm:validate
78+
79+
check-sync:
80+
needs: run-determination
81+
if: needs.run-determination.outputs.result == 'true'
82+
runs-on: ubuntu-latest
83+
84+
steps:
85+
- name: Checkout repository
86+
uses: actions/checkout@v3
87+
88+
- name: Setup Node.js
89+
uses: actions/setup-node@v3
90+
with:
91+
node-version: ${{ env.NODE_VERSION }}
92+
93+
- name: Install Task
94+
uses: arduino/setup-task@v1
95+
with:
96+
repo-token: ${{ secrets.GITHUB_TOKEN }}
97+
version: 3.x
98+
99+
- name: Install npm dependencies
100+
run: task npm:install-deps
101+
102+
- name: Check package-lock.json
103+
run: git diff --color --exit-code package-lock.json

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
[![Check License status](https://github.com/arduino/setup-protoc/actions/workflows/check-license.yml/badge.svg)](https://github.com/arduino/setup-protoc/actions/workflows/check-license.yml)
88
[![Check Taskfiles status](https://github.com/arduino/setup-protoc/actions/workflows/check-taskfiles.yml/badge.svg)](https://github.com/arduino/setup-protoc/actions/workflows/check-taskfiles.yml)
99
[![Integration Tests status](https://github.com/arduino/setup-protoc/actions/workflows/test-integration.yml/badge.svg)](https://github.com/arduino/setup-protoc/actions/workflows/test-integration.yml)
10+
[![Check npm status](https://github.com/arduino/setup-protoc/actions/workflows/check-npm-task.yml/badge.svg)](https://github.com/arduino/setup-protoc/actions/workflows/check-npm-task.yml)
1011

1112
This action makes the `protoc` compiler available to Workflows.
1213

Taskfile.yml

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# See: https://taskfile.dev/#/usage
22
version: "3"
33

4+
vars:
5+
# Last version of ajv-cli with support for the JSON schema "Draft 4" specification
6+
SCHEMA_DRAFT_4_AJV_CLI_VERSION: 3.3.0
7+
48
tasks:
59
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-dependencies-task/Taskfile.yml
610
general:cache-dep-licenses:
@@ -116,3 +120,102 @@ tasks:
116120
- task: npm:install-deps
117121
cmds:
118122
- npx markdownlint-cli "**/*.md"
123+
124+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-npm-task/Taskfile.yml
125+
npm:validate:
126+
desc: Validate npm configuration files against their JSON schema
127+
vars:
128+
# Source: https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/package.json
129+
SCHEMA_URL: https://json.schemastore.org/package.json
130+
SCHEMA_PATH:
131+
sh: task utility:mktemp-file TEMPLATE="package-json-schema-XXXXXXXXXX.json"
132+
# Source: https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/ava.json
133+
AVA_SCHEMA_URL: https://json.schemastore.org/ava.json
134+
AVA_SCHEMA_PATH:
135+
sh: task utility:mktemp-file TEMPLATE="ava-schema-XXXXXXXXXX.json"
136+
# Source: https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/eslintrc.json
137+
ESLINTRC_SCHEMA_URL: https://json.schemastore.org/eslintrc.json
138+
ESLINTRC_SCHEMA_PATH:
139+
sh: task utility:mktemp-file TEMPLATE="eslintrc-schema-XXXXXXXXXX.json"
140+
# Source: https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/jscpd.json
141+
JSCPD_SCHEMA_URL: https://json.schemastore.org/jscpd.json
142+
JSCPD_SCHEMA_PATH:
143+
sh: task utility:mktemp-file TEMPLATE="jscpd-schema-XXXXXXXXXX.json"
144+
# Source: https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/npm-badges.json
145+
NPM_BADGES_SCHEMA_URL: https://json.schemastore.org/npm-badges.json
146+
NPM_BADGES_SCHEMA_PATH:
147+
sh: task utility:mktemp-file TEMPLATE="npm-badges-schema-XXXXXXXXXX.json"
148+
# Source: https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/prettierrc.json
149+
PRETTIERRC_SCHEMA_URL: https://json.schemastore.org/prettierrc.json
150+
PRETTIERRC_SCHEMA_PATH:
151+
sh: task utility:mktemp-file TEMPLATE="prettierrc-schema-XXXXXXXXXX.json"
152+
# Source: https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/semantic-release.json
153+
SEMANTIC_RELEASE_SCHEMA_URL: https://json.schemastore.org/semantic-release.json
154+
SEMANTIC_RELEASE_SCHEMA_PATH:
155+
sh: task utility:mktemp-file TEMPLATE="semantic-release-schema-XXXXXXXXXX.json"
156+
# Source: https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/stylelintrc.json
157+
STYLELINTRC_SCHEMA_URL: https://json.schemastore.org/stylelintrc.json
158+
STYLELINTRC_SCHEMA_PATH:
159+
sh: task utility:mktemp-file TEMPLATE="stylelintrc-schema-XXXXXXXXXX.json"
160+
INSTANCE_PATH: "**/package.json"
161+
PROJECT_FOLDER:
162+
sh: pwd
163+
WORKING_FOLDER:
164+
sh: task utility:mktemp-folder TEMPLATE="dependabot-validate-XXXXXXXXXX"
165+
cmds:
166+
- wget --quiet --output-document="{{.SCHEMA_PATH}}" {{.SCHEMA_URL}}
167+
- wget --quiet --output-document="{{.AVA_SCHEMA_PATH}}" {{.AVA_SCHEMA_URL}}
168+
- wget --quiet --output-document="{{.ESLINTRC_SCHEMA_PATH}}" {{.ESLINTRC_SCHEMA_URL}}
169+
- wget --quiet --output-document="{{.JSCPD_SCHEMA_PATH}}" {{.JSCPD_SCHEMA_URL}}
170+
- wget --quiet --output-document="{{.NPM_BADGES_SCHEMA_PATH}}" {{.NPM_BADGES_SCHEMA_URL}}
171+
- wget --quiet --output-document="{{.PRETTIERRC_SCHEMA_PATH}}" {{.PRETTIERRC_SCHEMA_URL}}
172+
- wget --quiet --output-document="{{.SEMANTIC_RELEASE_SCHEMA_PATH}}" {{.SEMANTIC_RELEASE_SCHEMA_URL}}
173+
- wget --quiet --output-document="{{.STYLELINTRC_SCHEMA_PATH}}" {{.STYLELINTRC_SCHEMA_URL}}
174+
- |
175+
cd "{{.WORKING_FOLDER}}" # Workaround for https://github.com/npm/cli/issues/3210
176+
npx ajv-cli@{{.SCHEMA_DRAFT_4_AJV_CLI_VERSION}} validate \
177+
--all-errors \
178+
-s "{{.SCHEMA_PATH}}" \
179+
-r "{{.AVA_SCHEMA_PATH}}" \
180+
-r "{{.ESLINTRC_SCHEMA_PATH}}" \
181+
-r "{{.JSCPD_SCHEMA_PATH}}" \
182+
-r "{{.NPM_BADGES_SCHEMA_PATH}}" \
183+
-r "{{.PRETTIERRC_SCHEMA_PATH}}" \
184+
-r "{{.SEMANTIC_RELEASE_SCHEMA_PATH}}" \
185+
-r "{{.STYLELINTRC_SCHEMA_PATH}}" \
186+
-d "{{.PROJECT_FOLDER}}/{{.INSTANCE_PATH}}"
187+
188+
# Make a temporary file named according to the passed TEMPLATE variable and print the path passed to stdout
189+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/windows-task/Taskfile.yml
190+
utility:mktemp-file:
191+
vars:
192+
RAW_PATH:
193+
sh: mktemp --tmpdir "{{.TEMPLATE}}"
194+
cmds:
195+
- task: utility:normalize-path
196+
vars:
197+
RAW_PATH: "{{.RAW_PATH}}"
198+
199+
# Make a temporary folder named according to the passed TEMPLATE variable and print the path passed to stdout
200+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/windows-task/Taskfile.yml
201+
utility:mktemp-folder:
202+
vars:
203+
RAW_PATH:
204+
sh: mktemp --directory --tmpdir "{{.TEMPLATE}}"
205+
cmds:
206+
- task: utility:normalize-path
207+
vars:
208+
RAW_PATH: "{{.RAW_PATH}}"
209+
210+
# Print a normalized version of the path passed via the RAW_PATH variable to stdout
211+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/windows-task/Taskfile.yml
212+
utility:normalize-path:
213+
cmds:
214+
- |
215+
if [[ "{{.OS}}" == "Windows_NT" ]] && which cygpath &>/dev/null; then
216+
# Even though the shell handles POSIX format absolute paths as expected, external applications do not.
217+
# So paths passed to such applications must first be converted to Windows format.
218+
cygpath -w "{{.RAW_PATH}}"
219+
else
220+
echo "{{.RAW_PATH}}"
221+
fi

0 commit comments

Comments
 (0)