Skip to content

Commit c3c09bb

Browse files
committed
build: move package.json validation to make recipe
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 88af63a commit c3c09bb

5 files changed

Lines changed: 126 additions & 49 deletions

File tree

.github/workflows/lint_changed_files.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ jobs:
171171
run: |
172172
. "$GITHUB_WORKSPACE/.github/workflows/scripts/lint_package_json_files" "${{ steps.changed-files.outputs.files }}"
173173
174+
# Validate package.json metadata:
175+
- name: 'Validate package.json metadata'
176+
if: success() || failure()
177+
run: |
178+
if [[ -n "${files}" ]]; then
179+
make validate-pkg-json FILES="${{ steps.changed-files.outputs.files }}"
180+
fi
181+
174182
# Lint REPL help files...
175183
- name: 'Lint REPL help files'
176184
if: success() || failure()

.github/workflows/scripts/lint_package_json_files

100755100644
Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# @license Apache-2.0
44
#
5-
# Copyright (c) 2024 The Stdlib Authors.
5+
# Copyright (c) 2026 The Stdlib Authors.
66
#
77
# Licensed under the Apache License, Version 2.0 (the "License");
88
# you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616
# See the License for the specific language governing permissions and
1717
# limitations under the License.
1818

19-
# Script to lint package.json files and check/update metadata fields.
19+
# Script to lint package.json files.
2020
#
2121
# Usage: lint_package_json_files file1 [file2 file3 ...]
2222
#
@@ -32,61 +32,14 @@ root=$(git rev-parse --show-toplevel)
3232
# Define the path to a utility for linting package.json files:
3333
lint_package_json="${root}/lib/node_modules/@stdlib/_tools/lint/pkg-json/bin/cli"
3434

35-
# Define the path to the package name validation tool:
36-
validate_package_names="${root}/lib/node_modules/@stdlib/_tools/lint/pkg-json-names/bin/cli"
37-
38-
# Define paths to utilities for updating package.json metadata fields:
39-
update_package_json_directories="${root}/lib/node_modules/@stdlib/_tools/package-json/scripts/update_directories"
40-
update_package_json_gypfile="${root}/lib/node_modules/@stdlib/_tools/package-json/scripts/update_gypfile"
41-
4235
# Files to process:
4336
files_to_process="$*"
4437

45-
# Initialize needs_changes flag:
46-
needs_changes=0
47-
4838
# Lint package.json files:
4939
files=$(echo "${files_to_process}" | tr ' ' '\n' | awk -F/ '$NF=="package.json"' | tr '\n' ' ' | sed 's/ $//')
5040
if [ -n "${files}" ]; then
5141
echo "Linting package.json files..."
5242
printf '%s' "${files}" | "${lint_package_json}" --split=" "
53-
54-
echo "Validating package names..."
55-
if ! printf '%s' "${files}" | "${validate_package_names}" --split=" "; then
56-
echo "ERROR: Package name validation failed"
57-
needs_changes=1
58-
fi
5943
else
6044
echo "No package.json files to lint."
6145
fi
62-
63-
# Check if metadata fields need to be updated in package.json files of affected packages:
64-
dirs=$(echo "${files_to_process}" | tr ' ' '\n' | \
65-
xargs dirname | \
66-
sed -E 's/\/(benchmark|bin|data|docs|etc|examples|include|lib|scripts|src|test)(\/.*)?$//' | \
67-
sort -u)
68-
69-
echo "Checking package.json files in directories: ${dirs}"
70-
for dir in ${dirs}; do
71-
echo "Checking package.json in ${dir}..."
72-
package_json="${dir}/package.json"
73-
if [ ! -f "${package_json}" ]; then
74-
continue
75-
fi
76-
original_content=$(cat "${package_json}")
77-
78-
"${update_package_json_directories}" "${dir}"
79-
"${update_package_json_gypfile}" "${dir}"
80-
81-
new_content=$(cat "${package_json}")
82-
if [ "$original_content" != "$new_content" ]; then
83-
echo "ERROR: package.json in ${dir} needs updates to directories and/or gypfile fields"
84-
git --no-pager diff "${package_json}"
85-
needs_changes=1
86-
fi
87-
done
88-
89-
# Exit with failure if any needed changes were detected:
90-
if [ $needs_changes -eq 1 ]; then
91-
exit 1
92-
fi
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 validate package.json metadata fields.
20+
#
21+
# Usage: validate_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 the package name validation tool:
33+
validate_package_names="${root}/lib/node_modules/@stdlib/_tools/lint/pkg-json-names/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+
# Initialize needs_changes flag:
43+
needs_changes=0
44+
45+
# Validate package.json package names:
46+
files=$(echo "${files_to_process}" | tr ' ' '\n' | awk -F/ '$NF=="package.json"' | tr '\n' ' ' | sed 's/ $//')
47+
if [ -n "${files}" ]; then
48+
echo "Validating package names..."
49+
if ! printf '%s' "${files}" | "${validate_package_names}" --split=" "; then
50+
echo "ERROR: Package name validation failed"
51+
needs_changes=1
52+
fi
53+
else
54+
echo "No package.json files to validate."
55+
fi
56+
57+
# Check if metadata fields need to be updated in package.json files of affected packages:
58+
dirs=$(echo "${files_to_process}" | tr ' ' '\n' | \
59+
xargs dirname | \
60+
sed -E 's/\/(benchmark|bin|data|docs|etc|examples|include|lib|scripts|src|test)(\/[^@]*)?$//' | \
61+
sort -u)
62+
63+
echo "Checking package.json files in directories: ${dirs}"
64+
for dir in ${dirs}; do
65+
echo "Checking package.json in ${dir}..."
66+
package_json="${dir}/package.json"
67+
if [ ! -f "${package_json}" ]; then
68+
continue
69+
fi
70+
original_content=$(cat "${package_json}")
71+
72+
"${update_package_json_directories}" "${dir}"
73+
"${update_package_json_gypfile}" "${dir}"
74+
75+
new_content=$(cat "${package_json}")
76+
if [ "$original_content" != "$new_content" ]; then
77+
echo "ERROR: package.json in ${dir} needs updates to directories and/or gypfile fields"
78+
git --no-pager diff "${package_json}"
79+
needs_changes=1
80+
fi
81+
done
82+
83+
# Exit with failure if any needed changes were detected:
84+
if [ $needs_changes -eq 1 ]; then
85+
exit 1
86+
fi

tools/git/hooks/pre-commit

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,19 @@ run_lint() {
254254
else
255255
task_status 'skipped'
256256
fi
257+
add_task 'validate_package_json'
258+
if [[ -z "${skip_package_json}" ]]; then
259+
make FILES="${files}" validate-pkg-json > /dev/null >&2
260+
if [[ "$?" -ne 0 ]]; then
261+
task_status 'failed'
262+
echo '' >&2
263+
echo 'package.json metadata out of date' >&2
264+
return 1
265+
fi
266+
task_status 'passed'
267+
else
268+
task_status 'skipped'
269+
fi
257270
# Lint REPL help files...
258271
add_task 'lint_repl_help'
259272
if [[ -z "${skip_repl_help}" ]]; then

tools/make/lib/lint/package_json.mk

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ PACKAGE_JSON_LINTER ?= $(TOOLS_PKGS_DIR)/lint/pkg-json/bin/cli
2424
# Define the command-line options to be used when invoking the executable:
2525
PACKAGE_JSON_LINTER_FLAGS ?=
2626

27+
# Define the path for script validating `package.json` metadata:
28+
VALIDATE_PKG_JSON ?= "${TOOLS_PKGS_DIR}/package-json/scripts/validate_package_json_files"
29+
2730

2831
# RULES #
2932

@@ -38,3 +41,17 @@ lint-pkg-json: $(NODE_MODULES)
3841
$(QUIET) NODE_PATH="$(NODE_PATH)" $(NODE) "$(PACKAGE_JSON_LINTER)" $(PACKAGE_JSON_LINTER_FLAGS) "$(ROOT_DIR)"
3942

4043
.PHONY: lint-pkg-json
44+
45+
#/
46+
# Validates `package.json` metadata associated with a list of files.
47+
#
48+
# @param {string} FILES - list of file paths
49+
#
50+
# @example
51+
# make validate-pkg-json FILES='/foo/lib/index.js /bar/package.json'
52+
#/
53+
validate-pkg-json: $(NODE_MODULES)
54+
$(QUIET) echo 'Validating package.json metadata...'
55+
$(QUIET) ${VALIDATE_PKG_JSON} $(FILES)
56+
57+
.PHONY: validate-pkg-json

0 commit comments

Comments
 (0)