-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
build: move package.json validation to make recipe #9520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
f02bd2a
86f42ee
be4c8fc
8f6f49b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| #!/usr/bin/env bash | ||
| # | ||
| # @license Apache-2.0 | ||
| # | ||
| # Copyright (c) 2026 The Stdlib Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # Script to validate package.json metadata fields. | ||
| # | ||
| # Usage: validate_package_json_files file1 [file2 file3 ...] | ||
| # | ||
| # Arguments: | ||
| # | ||
| # file1 File path. | ||
| # file2 File path. | ||
| # file3 File path. | ||
|
|
||
| # Determine root directory: | ||
| root=$(git rev-parse --show-toplevel) | ||
|
|
||
| # Define the path to the package name validation tool: | ||
| validate_package_names="${root}/lib/node_modules/@stdlib/_tools/lint/pkg-json-names/bin/cli" | ||
|
|
||
| # Define paths to utilities for updating package.json metadata fields: | ||
| update_package_json_directories="${root}/lib/node_modules/@stdlib/_tools/package-json/scripts/update_directories" | ||
| update_package_json_gypfile="${root}/lib/node_modules/@stdlib/_tools/package-json/scripts/update_gypfile" | ||
|
|
||
| # Files to process: | ||
| files_to_process="$*" | ||
|
|
||
| # Initialize needs_changes flag: | ||
| needs_changes=0 | ||
|
|
||
| # Validate package.json package names: | ||
| files=$(echo "${files_to_process}" | tr ' ' '\n' | awk -F/ '$NF=="package.json"' | tr '\n' ' ' | sed 's/ $//') | ||
| if [ -n "${files}" ]; then | ||
| echo "Validating package names..." | ||
| if ! printf '%s' "${files}" | "${validate_package_names}" --split=" "; then | ||
| echo "ERROR: Package name validation failed" | ||
| needs_changes=1 | ||
| fi | ||
| else | ||
| echo "No package.json files to validate." | ||
| fi | ||
|
|
||
| # Check if metadata fields need to be updated in package.json files of affected packages: | ||
| dirs=$(echo "${files_to_process}" | tr ' ' '\n' | \ | ||
| xargs dirname | \ | ||
| sed -E 's/\/(benchmark|bin|data|docs|etc|examples|include|lib|scripts|src|test)(\/[^@]*)?$//' | \ | ||
| sort -u) | ||
|
|
||
| echo "Checking package.json files in directories: ${dirs}" | ||
| for dir in ${dirs}; do | ||
| echo "Checking package.json in ${dir}..." | ||
| package_json="${dir}/package.json" | ||
| if [ ! -f "${package_json}" ]; then | ||
| continue | ||
| fi | ||
| original_content=$(cat "${package_json}") | ||
|
|
||
| "${update_package_json_directories}" "${dir}" | ||
| "${update_package_json_gypfile}" "${dir}" | ||
|
|
||
| new_content=$(cat "${package_json}") | ||
| if [ "$original_content" != "$new_content" ]; then | ||
| echo "ERROR: package.json in ${dir} needs updates to directories and/or gypfile fields" | ||
| git --no-pager diff "${package_json}" | ||
| needs_changes=1 | ||
| fi | ||
| done | ||
|
|
||
| # Exit with failure if any needed changes were detected: | ||
| if [ $needs_changes -eq 1 ]; then | ||
| exit 1 | ||
| fi | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,9 @@ PACKAGE_JSON_LINTER ?= $(TOOLS_PKGS_DIR)/lint/pkg-json/bin/cli | |
| # Define the command-line options to be used when invoking the executable: | ||
| PACKAGE_JSON_LINTER_FLAGS ?= | ||
|
|
||
| # Define the path for script validating `package.json` metadata: | ||
| VALIDATE_PKG_JSON ?= "${TOOLS_PKGS_DIR}/package-json/scripts/validate_package_json_files" | ||
|
|
||
|
|
||
| # RULES # | ||
|
|
||
|
|
@@ -38,3 +41,17 @@ lint-pkg-json: $(NODE_MODULES) | |
| $(QUIET) NODE_PATH="$(NODE_PATH)" $(NODE) "$(PACKAGE_JSON_LINTER)" $(PACKAGE_JSON_LINTER_FLAGS) "$(ROOT_DIR)" | ||
|
|
||
| .PHONY: lint-pkg-json | ||
|
|
||
| #/ | ||
| # Validates `package.json` metadata associated with a list of files. | ||
| # | ||
| # @param {string} FILES - list of file paths | ||
| # | ||
| # @example | ||
| # make validate-pkg-json FILES='/foo/lib/index.js /bar/package.json' | ||
| #/ | ||
| validate-pkg-json: $(NODE_MODULES) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This recipe is somewhat oddly named for a few reasons.
Instead, I would name this target A few other comments:
Longer term, I think we would be best served by the following targets:
where For this PR, I suggest going ahead and at least creating a beachhead by renaming the target and updating the various other downstream consumers in this PR accordingly. I'd also suggest refactoring the recipe to support
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While its true that this is named somewhat differently and lacking the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, right. This is package-level linting (e.g., if a C implementation is added to a package, then we need to confirm that the In which case, this may be better placed in Then, I suggest we rename to And yes, you're right that
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pushed the draft recipe.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that works. Will update PR accordingly. |
||
| $(QUIET) echo 'Validating package.json metadata...' | ||
| $(QUIET) $(VALIDATE_PKG_JSON) $(FILES) | ||
|
|
||
| .PHONY: validate-pkg-json | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: Replaced
(\/.*)?with(\/[^@]*)?to avoid converting paths like./lib/node_modules/@stdlib/utils/libto./, which is undesired (we would want./lib/node_modules/@stdlib/utilsdirectory in this example). By forbidding@, we can ensure that onlylibfolders after/@stdlib/will be stripped.