Skip to content

Instantly share code, notes, and snippets.

@alpercitak
Last active June 2, 2022 13:47
Show Gist options
  • Save alpercitak/49f23339409ef15e1d112a915abaa67d to your computer and use it in GitHub Desktop.
Save alpercitak/49f23339409ef15e1d112a915abaa67d to your computer and use it in GitHub Desktop.
init code style packages
#!/bin/sh
while getopts f flag; do
case "${flag}" in
f) force='true';;
esac
done
gitignoreFile=".gitignore"
eslintconfigFile=".eslintrc"
eslintignoreFile=".eslintignore"
prettierconfigFile=".prettierrc"
prettierignoreFile=".prettierignore"
gitignore() {
ignorePatterns=("node_modules" "dist" "build" "coverage" "yarn-error.log" "npm-debug.log")
if [ ! -f "$gitignoreFile" ] || [ $force ]; then
printf '%b\n' "${ignorePatterns[@]}" > $gitignoreFile
fi
}
prettier() {
ignorePatterns=(
"node_modules" "dist" "build" "coverage" ".husky" "package.json" "package-lock.json" "yarn.lock"
"$prettierconfigFile" "$prettierignoreFile" "$eslintconfigFile" "$eslintignoreFile" "$gitignoreFile"
)
npm list --depth=0 | grep prettier || yarn add prettier -D
if [ ! -f "$prettierconfigFile"* ] || [ $force ]; then
echo {} > $prettierconfigFile
json -I -f $prettierconfigFile -e "this.singleQuote=true"
json -I -f $prettierconfigFile -e "this.trailingComma=\"es5\""
fi
if [ ! -f "$prettierIgnore" ] || [ $force ]; then
printf '%b\n' "${ignorePatterns[@]}" > $prettierignoreFile
fi
}
eslint() {
ignorePatterns=(
"node_modules" "dist" "build" "coverage" ".husky" "package.json" "package-lock.json" "yarn.lock"
"$prettierconfigFile" "$prettierignoreFile" "$eslintconfigFile" "$eslintignoreFile" "$gitignoreFile"
)
npm list --depth=0 | grep eslint || yarn add eslint -D
if [ ! -f "$eslintconfigFile"* ] || [ $force ]; then
yarn create @eslint/config
fi
if [ ! -f "$eslintignoreFile" ] || [ $force ]; then
printf '%b\n' "${ignorePatterns[@]}" > $eslintignoreFile
fi
}
husky() {
npm list --depth=0 | grep husky || yarn add husky -D
npm list --depth=0 | grep lint-staged || yarn add lint-staged -D
if [ ! -d ".husky" ]; then
npm set-script prepare "husky install" && yarn prepare
fi
rm -f .husky/pre-commit;
npx husky add .husky/pre-commit "yarn lint-staged"
json -I -f package.json -e "if (!this.scripts.lint) this.scripts.lint=\"eslint . --fix\""
json -I -f package.json -e "if (!this.scripts.format) this.scripts.format=\"prettier --write .\""
json -I -f package.json -e "this['lint-staged']={...this['lint-staged']}"
json -I -f package.json -e "this['lint-staged']['**/*.{js,jsx,ts,tsx}']=[\"yarn format\", \"yarn lint\"]"
}
yarn init -y
npm list --depth=0 | grep json || yarn add json -D
gitignore
prettier
eslint
husky
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment