Skip to content

Commit 33bc65b

Browse files
Add CI workflow to validate tsconfig files
On every push or pull request that affects the repository's tsconfig files, and periodically, validate them against the JSON schema.
1 parent e52d9eb commit 33bc65b

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: Check TypeScript Configuration
2+
3+
env:
4+
# See: https://github.com/actions/setup-node/#readme
5+
NODE_VERSION: 16.x
6+
7+
# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows
8+
on:
9+
push:
10+
paths:
11+
- ".github/workflows/check-tsconfig-task.ya?ml"
12+
- "**/tsconfig*.json"
13+
- "Taskfile.ya?ml"
14+
pull_request:
15+
paths:
16+
- ".github/workflows/check-tsconfig-task.ya?ml"
17+
- "**/tsconfig*.json"
18+
- "Taskfile.ya?ml"
19+
schedule:
20+
# Run every Tuesday at 8 AM UTC to catch breakage from changes to the JSON schema.
21+
- cron: "0 8 * * TUE"
22+
workflow_dispatch:
23+
repository_dispatch:
24+
25+
jobs:
26+
validate:
27+
runs-on: ubuntu-latest
28+
29+
strategy:
30+
fail-fast: false
31+
32+
matrix:
33+
file:
34+
- ./tsconfig.json
35+
36+
steps:
37+
- name: Checkout repository
38+
uses: actions/checkout@v3
39+
40+
- name: Setup Node.js
41+
uses: actions/setup-node@v3
42+
with:
43+
node-version: ${{ env.NODE_VERSION }}
44+
45+
- name: Install Task
46+
uses: arduino/setup-task@v1
47+
with:
48+
repo-token: ${{ secrets.GITHUB_TOKEN }}
49+
version: 3.x
50+
51+
- name: Validate ${{ matrix.file }}
52+
env:
53+
TSCONFIG_PATH: ${{ matrix.file }}
54+
run: task --silent ts:validate

Taskfile.yml

Lines changed: 71 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,70 @@ tasks:
116120
- task: npm:install-deps
117121
cmds:
118122
- npx markdownlint-cli "**/*.md"
123+
124+
ts:validate:
125+
desc: Validate TypeScript configuration file against its JSON schema
126+
vars:
127+
# Source: https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/tsconfig.json
128+
SCHEMA_URL: https://json.schemastore.org/tsconfig.json
129+
SCHEMA_PATH:
130+
sh: task utility:mktemp-file TEMPLATE="tsconfig-schema-XXXXXXXXXX.json"
131+
INSTANCE_PATH: '{{default "./tsconfig.json" .TSCONFIG_PATH}}'
132+
WORKING_FOLDER:
133+
sh: task utility:mktemp-folder TEMPLATE="ts-validate-XXXXXXXXXX"
134+
WORKING_INSTANCE_PATH:
135+
sh: echo "{{.WORKING_FOLDER}}/$(basename "{{.INSTANCE_PATH}}")"
136+
cmds:
137+
- |
138+
# TypeScript allows comments in tsconfig.json.
139+
# ajv-cli did not support comments in JSON at the 3.x version in use (support was added in a later version).
140+
npx strip-json-comments-cli \
141+
--no-whitespace \
142+
"{{.INSTANCE_PATH}}" \
143+
> "{{.WORKING_INSTANCE_PATH}}"
144+
- |
145+
wget \
146+
--quiet \
147+
--output-document="{{.SCHEMA_PATH}}" \
148+
{{.SCHEMA_URL}}
149+
- |
150+
cd "{{.WORKING_FOLDER}}" # Workaround for https://github.com/npm/cli/issues/3210
151+
npx ajv-cli@{{.SCHEMA_DRAFT_4_AJV_CLI_VERSION}} validate \
152+
--all-errors \
153+
-s "{{.SCHEMA_PATH}}" \
154+
-d "{{.WORKING_INSTANCE_PATH}}"
155+
156+
# Make a temporary file named according to the passed TEMPLATE variable and print the path passed to stdout
157+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/windows-task/Taskfile.yml
158+
utility:mktemp-file:
159+
vars:
160+
RAW_PATH:
161+
sh: mktemp --tmpdir "{{.TEMPLATE}}"
162+
cmds:
163+
- task: utility:normalize-path
164+
vars:
165+
RAW_PATH: "{{.RAW_PATH}}"
166+
167+
# Make a temporary folder named according to the passed TEMPLATE variable and print the path passed to stdout
168+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/windows-task/Taskfile.yml
169+
utility:mktemp-folder:
170+
vars:
171+
RAW_PATH:
172+
sh: mktemp --directory --tmpdir "{{.TEMPLATE}}"
173+
cmds:
174+
- task: utility:normalize-path
175+
vars:
176+
RAW_PATH: "{{.RAW_PATH}}"
177+
178+
# Print a normalized version of the path passed via the RAW_PATH variable to stdout
179+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/windows-task/Taskfile.yml
180+
utility:normalize-path:
181+
cmds:
182+
- |
183+
if [[ "{{.OS}}" == "Windows_NT" ]] && which cygpath &>/dev/null; then
184+
# Even though the shell handles POSIX format absolute paths as expected, external applications do not.
185+
# So paths passed to such applications must first be converted to Windows format.
186+
cygpath -w "{{.RAW_PATH}}"
187+
else
188+
echo "{{.RAW_PATH}}"
189+
fi

0 commit comments

Comments
 (0)