Skip to content

Instantly share code, notes, and snippets.

@adalinesimonian
Last active September 18, 2015 01:03

Revisions

  1. adalinesimonian revised this gist Sep 18, 2015. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions node-find-dep-requires.sh
    Original file line number Diff line number Diff line change
    @@ -13,10 +13,6 @@ dependencies="$(grep -Pzo "(?s)(?<=\"dependencies\": {)[^}]+" package.json | gre
    devDependencies="$(grep -Pzo "(?s)(?<=\"devDependencies\": {)[^}]+" package.json | grep -ohP "(?<=\")[^\"]+(?=\":)")"
    nativeModules="$(node -e "console.log(Object.keys(process.binding('natives')).filter(function (el) {return !/^_|^internal/.test(el) && ['freelist', 'sys'].indexOf(el) === -1;}).join('\\n'))")"



    echo $dependencies

    if [ "$1" = "-d" ]; then
    printf '%s\n' "$requires" | while IFS= read -r line
    do
  2. adalinesimonian revised this gist Sep 18, 2015. 1 changed file with 48 additions and 2 deletions.
    50 changes: 48 additions & 2 deletions node-find-dep-requires.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,50 @@
    # One-liner
    #!/bin/sh

    # Requires ag - The Silver Searcher (https://github.com/ggreer/the_silver_searcher)

    grep -Pzo "(?s)(?<=\"dependencies\": {)[^}]+" package.json | grep -ohP "(?<=\")[^\"]+(?=\":)" | xargs -L1 --replace sh -c "if ! ag \"require[\\( ]'{}'\" > /dev/null 2> /dev/null; then printf '\\033[1;31mREQUIRE NOT FOUND:\\t%s\033[m\\n' {}; else printf 'Require found:\\t\\t%s\\n' {}; fi"
    # (no options) - checks if dependencies are in use
    # -d - checks if all requires have a matching dependency entry

    # One-liner that checks for in-use dependencies
    # grep -Pzo "(?s)(?<=\"dependencies\": {)[^}]+" package.json | grep -ohP "(?<=\")[^\"]+(?=\":)" | xargs -L1 --replace sh -c "if ! ag \"require[\\( ]'{}'\" > /dev/null 2> /dev/null; then printf '\\033[1;31mREQUIRE NOT FOUND:\\t%s\033[m\\n' {}; else printf 'Require found:\\t\\t%s\\n' {}; fi"

    requires="$(ag "require(\(| )('|\")[^'\"]+" | grep -ohP "(?<=require[\( ]('|\"))[^'\"/]+(?=('|\"))" | sort -u)"
    dependencies="$(grep -Pzo "(?s)(?<=\"dependencies\": {)[^}]+" package.json | grep -ohP "(?<=\")[^\"]+(?=\":)")"
    devDependencies="$(grep -Pzo "(?s)(?<=\"devDependencies\": {)[^}]+" package.json | grep -ohP "(?<=\")[^\"]+(?=\":)")"
    nativeModules="$(node -e "console.log(Object.keys(process.binding('natives')).filter(function (el) {return !/^_|^internal/.test(el) && ['freelist', 'sys'].indexOf(el) === -1;}).join('\\n'))")"



    echo $dependencies

    if [ "$1" = "-d" ]; then
    printf '%s\n' "$requires" | while IFS= read -r line
    do
    if [ ! -z "$(echo "$dependencies" | grep -Pzo "^$line$")" ]; then
    printf 'Dependency found:\t%s\n' $line
    elif [ ! -z "$(echo "$devDependencies" | grep -Pzo "^$line$")" ]; then
    printf 'Dev dependency found:\t%s\n' $line
    elif [ ! -z "$(echo "$nativeModules" | grep -Pzo "^$line$")" ]; then
    printf 'Native module found:\t%s\n' $line
    else
    printf '\033[1;31mDEPENDENCY NOT FOUND:\t%s\033[m\n' $line
    fi
    done
    else
    printf '%s\n' "$dependencies" | while IFS= read -r line
    do
    if [ ! -z "$(echo "$requires" | grep -Pzo "^$line$")" ]; then
    printf 'Require found:\t\t%s\n' $line
    else
    printf '\033[1;31mREQUIRE NOT FOUND:\t%s\033[m\n' $line
    fi
    done
    printf '%s\n' "$devDependencies" | while IFS= read -r line
    do
    if [ ! -z "$(echo "$requires" | grep -Pzo "^$line$")" ]; then
    printf 'Require found:\t\t%s\n' $line
    else
    printf '\033[1;31mREQUIRE NOT FOUND:\t%s\033[m\n' $line
    fi
    done
    fi
  3. adalinesimonian created this gist Sep 17, 2015.
    4 changes: 4 additions & 0 deletions node-find-dep-requires.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    # One-liner
    # Requires ag - The Silver Searcher (https://github.com/ggreer/the_silver_searcher)

    grep -Pzo "(?s)(?<=\"dependencies\": {)[^}]+" package.json | grep -ohP "(?<=\")[^\"]+(?=\":)" | xargs -L1 --replace sh -c "if ! ag \"require[\\( ]'{}'\" > /dev/null 2> /dev/null; then printf '\\033[1;31mREQUIRE NOT FOUND:\\t%s\033[m\\n' {}; else printf 'Require found:\\t\\t%s\\n' {}; fi"