Skip to content

Commit 5a1cea2

Browse files
committed
build: move package.json linting to own script and make more robust
1 parent c556d55 commit 5a1cea2

File tree

2 files changed

+75
-37
lines changed

2 files changed

+75
-37
lines changed

.github/workflows/lint_changed_files.yml

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -142,43 +142,7 @@ jobs:
142142
- name: 'Lint package.json files'
143143
if: success() || failure()
144144
run: |
145-
# Determine root directory:
146-
root=$(git rev-parse --show-toplevel)
147-
148-
# Define the path to a utility for linting package.json files:
149-
lint_package_json="${root}/lib/node_modules/@stdlib/_tools/lint/pkg-json/bin/cli"
150-
151-
# Define paths to utilities for updating package.json metadata fields:
152-
update_package_json_directories="${root}/lib/node_modules/@stdlib/_tools/package-json/scripts/update_directories"
153-
update_package_json_gypfile="${root}/lib/node_modules/@stdlib/_tools/package-json/scripts/update_gypfile"
154-
155-
# Lint changed package.json files:
156-
files=$(echo "${{ steps.changed-files.outputs.files }}" | tr ' ' '\n' | grep 'package\.json$' | grep -v 'datapackage\.json$' | tr '\n' ' ' | sed 's/ $//')
157-
if [ -n "${files}" ]; then
158-
echo "Linting package.json files that have changed..."
159-
printf "${files}" | "${lint_package_json}" --split=" "
160-
else
161-
echo "No package.json files to lint."
162-
fi
163-
164-
# Check if metadata fields need to be updated in package.json files of affected packages:
165-
dirs=$(echo "${{ steps.changed-files.outputs.files }}" | tr ' ' '\n' | xargs dirname | sort -u)
166-
needs_changes=0
167-
for dir in ${dirs}; do
168-
echo "Checking package.json in ${dir}..."
169-
"${update_package_json_directories}" "${dir}"
170-
"${update_package_json_gypfile}" "${dir}"
171-
if [[ `git status --porcelain` ]]; then
172-
echo "::error::Package.json in ${dir} needs updates to directories and/or gypfile fields"
173-
git diff
174-
needs_changes=1
175-
fi
176-
done
177-
178-
# Exit with failure if any needed changes were detected:
179-
if [ $needs_changes -eq 1 ]; then
180-
exit 1
181-
fi
145+
. "$GITHUB_WORKSPACE/.github/workflows/scripts/lint_package_json_files" "${{ steps.changed-files.outputs.files }}"
182146
183147
# Lint REPL help files...
184148
- name: 'Lint REPL help files'
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env bash
2+
#
3+
# @license Apache-2.0
4+
#
5+
# Copyright (c) 2024 The Stdlib Authors.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
# Script to lint package.json files and check/update metadata fields.
20+
#
21+
# Usage: lint_package_json_files file1 [file2 file3 ...]
22+
#
23+
# Arguments:
24+
#
25+
# file1 File path.
26+
# file2 File path.
27+
# file3 File path.
28+
29+
# Determine root directory:
30+
root=$(git rev-parse --show-toplevel)
31+
32+
# Define the path to a utility for linting package.json files:
33+
lint_package_json="${root}/lib/node_modules/@stdlib/_tools/lint/pkg-json/bin/cli"
34+
35+
# Define paths to utilities for updating package.json metadata fields:
36+
update_package_json_directories="${root}/lib/node_modules/@stdlib/_tools/package-json/scripts/update_directories"
37+
update_package_json_gypfile="${root}/lib/node_modules/@stdlib/_tools/package-json/scripts/update_gypfile"
38+
39+
# Files to process:
40+
files_to_process="$*"
41+
42+
# Lint package.json files:
43+
files=$(echo "${files_to_process}" | tr ' ' '\n' | grep 'package\.json$' | grep -v 'datapackage\.json$' | tr '\n' ' ' | sed 's/ $//')
44+
if [ -n "${files}" ]; then
45+
echo "Linting package.json files..."
46+
printf '%s' "${files}" | "${lint_package_json}" --split=" "
47+
else
48+
echo "No package.json files to lint."
49+
fi
50+
51+
# Check if metadata fields need to be updated in package.json files of affected packages:
52+
dirs=$(echo "${files_to_process}" | tr ' ' '\n' | \
53+
xargs dirname | \
54+
sed -E 's/\/(benchmark|bin|data|docs|etc|examples|include|lib|scripts|src|test)\/?$//' | \
55+
sort -u)
56+
57+
echo "Checking package.json files in directories: ${dirs}"
58+
59+
needs_changes=0
60+
for dir in ${dirs}; do
61+
echo "Checking package.json in ${dir}..."
62+
"${update_package_json_directories}" "${dir}"
63+
"${update_package_json_gypfile}" "${dir}"
64+
if ! git diff-index --quiet HEAD -- "${dir}/package.json"; then
65+
echo "ERROR: Package.json in ${dir} needs updates to directories and/or gypfile fields"
66+
git --no-pager diff "${dir}/package.json"
67+
needs_changes=1
68+
fi
69+
done
70+
71+
# Exit with failure if any needed changes were detected:
72+
if [ $needs_changes -eq 1 ]; then
73+
exit 1
74+
fi

0 commit comments

Comments
 (0)