Skip to content

Instantly share code, notes, and snippets.

@andreineculau
Last active September 18, 2017 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreineculau/1d47ede1f7726357213bd5ae4078245e to your computer and use it in GitHub Desktop.
Save andreineculau/1d47ede1f7726357213bd5ae4078245e to your computer and use it in GitHub Desktop.
List package.json dependencies' licenses recursively - stdout has the license text, stderr has the list
#!/usr/bin/env bash
# Usage path/to/list-npm-licenses.sh node_modules
function jqq() {
jq -r "$@" 2>/dev/null
}
function grepq() {
grep -q -w "$@" 2>/dev/null
}
NPM_DEPS=".dependencies, .bundledDependencies, .peerDependencies"
[[ ${NODE_ENV} != production ]] && {
NPM_DEPS="${NPM_DEPS}, .devDependencies, .optionalDependencies, .optionalDevDependencies"
}
NPM_DEPS=$(jqq "${NPM_DEPS} | keys | .[]" package.json)
function isNull() {
[[ -z ${1} || ${1} == '' || ${1} == 'null' || ${1} == '[]' ]]
}
function dodep() {
local DEP=${1}
local DEP_DIR=${2}/${DEP}
local DEP_NPM=${DEP_DIR}/package.json
function dojson() {
local JSON=${1}
export URL
export LICENSE
isNull ${URL} && URL="$(jqq '.homepage' ${JSON})"
isNull ${URL} && URL="$(jqq '.repository[0].url' ${JSON})"
isNull ${URL} && URL=''
isNull ${LICENSE} && LICENSE="$(jqq '.license.type' ${JSON})"
isNull ${LICENSE} && LICENSE="$(jqq '.license' ${JSON} | tr '\n' ' ' | tr -s ' ')"
isNull ${LICENSE} && LICENSE="$(jqq '.licenses[0].type' ${JSON})"
isNull ${LICENSE} && LICENSE="$(jqq '.licenses[0]' ${JSON})"
isNull ${LICENSE} && LICENSE="$(jqq '.licenses | map(.type) | .[]' ${JSON} | sed ':a;N;$!ba;s/\n/,/g')"
isNull ${LICENSE} && LICENSE=''
}
local DEP_LICENSE=(
${DEP_DIR}/*LICENSE*
${DEP_DIR}/*License*
${DEP_DIR}/*license*
)
DEP_LICENSE="$(cat ${DEP_LICENSE} 2>/dev/null)"
local DEP_LIC=(
${DEP_DIR}/*LICENSE*
${DEP_DIR}/*License*
${DEP_DIR}/*license*
${DEP_DIR}/*README*
${DEP_DIR}/*Readme*
${DEP_DIR}/*readme*
)
local URL=''
local LICENSE=''
[[ -f ${DEP_NPM} ]] && dojson ${DEP_NPM}
isNull ${LICENSE} && grepq 'MIT' ${DEP_LIC[*]} && LICENSE=MIT
isNull ${LICENSE} && grepq 'LGPL' ${DEP_LIC[*]} && LICENSE=LGPL
isNull ${LICENSE} && grepq 'GPL' ${DEP_LIC[*]} && LICENSE=GPL
isNull ${LICENSE} && grepq 'BSD' ${DEP_LIC[*]} && LICENSE=BSD
isNull ${LICENSE} && grepq 'Apache' ${DEP_LIC[*]} && LICENSE=Apache
isNull ${LICENSE} && grepq 'Domain Public' ${DEP_LIC[*]} && LICENSE='Public Domain'
isNull ${LICENSE} && (echo "${DEP_LIC[*]}" | grepq 'MIT') && LICENSE=MIT
isNull ${LICENSE} && (echo "${DEP_LIC[*]}" | grepq 'LGPL') && LICENSE=LGPL
isNull ${LICENSE} && (echo "${DEP_LIC[*]}" | grepq 'GPL') && LICENSE=GPL
isNull ${LICENSE} && (echo "${DEP_LIC[*]}" | grepq 'BSD') && LICENSE=BSD
isNull ${LICENSE} && (echo "${DEP_LIC[*]}" | grepq 'Apache') && LICENSE=Apache
isNull ${LICENSE} && grep -q 'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR' ${DEP_LIC} 2>/dev/null && LICENSE=MIT
[[ -z ${LICENSE} ]] && LICENSE=UNKNOWN
printf "%-30s %-30s %-30s %s\n" "${2}" "${LICENSE}" "${DEP}" "${URL}"
(
echo -e "-------------------------------------------------------------------------LICENSE"
echo -e "${LICENSE}\n${DEP_DIR}\n${URL}"
echo -e "\n\n"
echo -e "${DEP_LICENSE}"
echo -e "\n\n"
) >&2
}
(
[[ -n ${1} ]] && for DEP in ${NPM_DEPS}; do
dodep ${DEP} ${1}
done
) | sort | uniq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment