zoukankan      html  css  js  c++  java
  • [Bash] Use jq and grep to Find Unused Dependencies in a Project

    In this lesson, we'll apply our jq skills and write a script that gets a list of dependencies from package.json and greps for usages of every package. If a package is unused, it's marked for removal.

    Get 'dependencies' from package.json

    jq '.dependencies' package.json
    
    ## {"express": "^4.16.4", "lodash": "^4.17.11"}
    

    Only get keys for dependencies:

    jq '.dependencies | keys | .[]' package.json
    
    ## "express"
    ## "lodash"
    

    Then drop the `"``

    jq -r '.dependencies | keys | .[]' package.json
    
    ## express
    ## lodash
    

    Creat check-unused-deps.sh:

    for dep in $(jq -r '.dependencies | keys | .[]' package.json); do
        if ! grep "require(.*$dep.*)" -Rq --exclude-dir="node_modules" .; then
            echo "You can probably remove $dep"
        fi
    done
    

    Give premission:

    chmod +x check-unused-deps.sh
    

    After running, it shows which deps can be removed for node applications.

  • 相关阅读:
    24. Swap Nodes in Pairs
    49. Group Anagrams
    280. Wiggle Sort
    274. H-Index
    K Closest Numbers In Sorted Array
    Closest Number in Sorted Array
    Last Position of Target
    Classical Binary Search
    350. Intersection of Two Arrays II
    Sort Integers II
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14398452.html
Copyright © 2011-2022 走看看