christopherthielen / check-peer-dependencies

Checks peer dependencies of the current NodeJS package. Offers solutions for any that are unmet.
MIT License
60 stars 19 forks source link

[feature] Allow programmatic use that returns an object of data #13

Closed Whoaa512 closed 1 year ago

Whoaa512 commented 3 years ago

I'd like to use this within a larger set of CI checks that I currently have in a custom script.

As is I need to spawn this via exec and parse the output, would be great if I could import and call it directly without all the console.logs and get an object back

internalsystemerror commented 3 years ago

likewise for use with renovate... there are certain peer dependencies that I would like to skip, and to fail for anything else...

For reference, here is the bash script I'm using at the moment:

#!/bin/bash

set -e

# Set allowed missing peer dependencies
ALLOWED_MISSING=(
    "jquery"
    "popper.js"
)

# Set allowed incorrect peer dependencies
ALLOWED_INCORRECT=(
)

# Perform check for peer dependencies
if ! PEERCHECK=$(yarn check-peer-dependencies --yarn 2>&1); then
    # Gather packages that are not installed
    mapfile -t MISSING < <((grep '❌' <<<"$PEERCHECK") | grep -oP '(?<=\()[^ \(]+(?= is not installed)')
    for PACKAGE in ${!MISSING[*]}; do
        # Check if the missing package is not allowed to be
        if ! [[ ${ALLOWED_MISSING[*]} =~ ${MISSING[$PACKAGE]} ]]; then
            # Show full output and error
            printf "%s\n" "${PEERCHECK[*]}"
            exit 1
        fi
    done
    # Gather packages that are the wrong version
    mapfile -t WRONGVERSION < <((grep '❌' <<<"$PEERCHECK") | grep -oP '(?<= is required by ).+(?=@[^ ]+ \([^ ]+ is installed)')
    for PACKAGE in ${!WRONGVERSION[*]}; do
        # Check if the missing package is not allowed to be
        if ! [[ ${ALLOWED_INCORRECT[*]} =~ ${WRONGVERSION[$PACKAGE]} ]]; then
            # Show full output and error
            printf "%s\n" "${PEERCHECK[*]}"
            exit 1
        fi
    done
fi

Edits: Updating bash script

christopherthielen commented 2 years ago

@internalsystemerror

likewise for use with renovate... there are certain peer dependencies that I would like to skip, and to fail for anything else...

I added a cli option --ignore pkg1 --ignore pkg2 in version 4.1.0

internalsystemerror commented 2 years ago

I added a cli option --ignore pkg1 --ignore pkg2 in version 4.1.0

Oh nice! Thanks I will check it out.

internalsystemerror commented 2 years ago

@christopherthielen That new cli option works for me so I no longer need my bash script. Thanks.

christopherthielen commented 1 year ago

I'm closing this for now, but would gladly merge a reasonable PR.

jpnelson commented 9 months ago

Hi! Thanks for making and supporting a really useful library!

My use case is somewhat similar to what was originally described, but a little more particular – I'd like to ignore, but also restrict the violations to a specific set of known violations. We're effectively creating an allow-list of exceptions, so we can ratchet down those exceptions over time.

For that I made a custom script that does what was originally described here, but a node API that returned strings and statuses (rather than console.log / process.exit) would be helpful for that purpose. I imagine this use case is a little bit too niche to support as cli arguments, but maybe a node API would be useful for it. I've similarly made do with using exec and parsing the output in the meantime though, so it's not a blocker for us using this library.