Skip to content

Instantly share code, notes, and snippets.

@andreineculau
Created March 18, 2014 08:33
Show Gist options
  • Save andreineculau/9615879 to your computer and use it in GitHub Desktop.
Save andreineculau/9615879 to your computer and use it in GitHub Desktop.
Searching for NPM/Bower licenses
#!/usr/bin/env bash
function jqq() {
jq -r "$@" 2>/dev/null
}
function grepq() {
grep -q -w "$@" 2>/dev/null
}
NPM_DEPS=$(jqq '.dependencies, .devDependencies, .optionalDependencies, .optionalDevDependencies | keys | .[]' package.json)
BOWER_DEPS=$(jqq '.dependencies, .devDependencies | keys | .[]' bower.json)
function isNull() {
[[ -z ${1} || ${1} == '' || ${1} == 'null' ]]
}
function dodep() {
DEP=${1}
DEP_DIR=${2}/${DEP}
DEP_NPM=${DEP_DIR}/package.json
DEP_BOWER=${DEP_DIR}/bower.json
DEP_BOWER2=${DEP_DIR}/bower.json
function dojson() {
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})
isNull ${LICENSE} && LICENSE=$(jqq '.licenses[0].type' ${JSON})
isNull ${LICENSE} && LICENSE=$(jqq '.licenses | map(.type) | .[]' ${JSON} | sed ':a;N;$!ba;s/\n/,/g')
isNull ${LICENSE} && LICENSE=''
}
DEP_LICENSE=(
${DEP_DIR}/*LICENSE*
${DEP_DIR}/*License*
${DEP_DIR}/*license*
)
DEP_LICENSE="$(cat ${DEP_LICENSE} 2>/dev/null)"
DEP_LIC=(
${DEP_DIR}/*LICENSE*
${DEP_DIR}/*License*
${DEP_DIR}/*license*
${DEP_DIR}/*README*
${DEP_DIR}/*Readme*
${DEP_DIR}/*readme*
)
URL=''
LICENSE=''
[[ -f ${DEP_NPM} ]] && dojson ${DEP_NPM}
[[ -f ${DEP_BOWER} ]] && dojson ${DEP_BOWER}
[[ -f ${DEP_BOWER2} ]] && dojson ${DEP_BOWER2}
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 %s\n" "${LICENSE}" "${DEP}" "${URL}"
(
echo -e "-------------------------------------------------------------------------LICENSE"
echo -e "${LICENSE}\n${DEP}\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
[[ -n ${2} ]] && for DEP in ${BOWER_DEPS[*]}; do
dodep ${DEP} ${2}
done
) | sort | uniq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment