|
| 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