Last active
January 14, 2020 08:38
-
-
Save hwgao/1682896f4e5507cec50346f2f90a4e7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
help() { | |
cat <<- EOF | |
Generate a npm managed project: | |
-h : print this help | |
-r : requireed packages | |
-d : development packages | |
Example: $0 -r express -d eslint | |
EOF | |
} | |
while getopts "hr:d:" opt; do | |
case $opt in | |
r) REQ_PKGS=($OPTARG);; | |
d) DEV_PKGS=($OPTARG);; | |
h) help; exit 0;; | |
*) echo "This option is not known: $opt"; help; exit 1;; | |
esac | |
done | |
shift $((OPTIND -1)) | |
if [ "$#" -eq 0 ]; then | |
echo "Configure project in current folder" | |
else | |
PROJECT_NAME="$1" | |
mkdir -p "${PROJECT_NAME}" && cd "${PROJECT_NAME}" | |
fi | |
if [ -e "package.json" ]; then | |
npm install | |
exit 0 | |
fi | |
npm init -y | |
for r in "${REQ_PKGS[@]}"; do | |
npm install $r | |
done | |
if [ "${#DEV_PKGS[@]}" -eq 0 ]; then | |
DEV_PKGS=(eslint) | |
fi | |
for d in "${DEV_PKGS[@]}"; do | |
npm install --save-dev $d | |
if [ "$d" = "eslint" ]; then | |
npx eslint --init | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment