diff --git a/dev-scripts/.htaccess b/dev-scripts/.htaccess new file mode 100644 index 0000000..3a42882 --- /dev/null +++ b/dev-scripts/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/dev-scripts/pre-commit b/dev-scripts/pre-commit new file mode 100755 index 0000000..66d8854 --- /dev/null +++ b/dev-scripts/pre-commit @@ -0,0 +1,75 @@ +#!/usr/bin/env bash +# Enable this pre-commit hook by running 'make installhooks' +set -euo pipefail + +git-staged-files() { + git diff --cached -C -C -z --name-only --diff-filter=ACMRTUXB "$@" +} + +git-is-staged() { + local diff=0 + git-staged-files --quiet "$@" 2> /dev/null || diff=$? + [[ diff -eq 1 ]] || return 1 +} + +map() { IFS= read -rd $'\0' "$@"; } + +compress-png() { + git-staged-files \*.png|while map file; do + echo "Compressing $file" + optipng -q -o7 "$file" && advpng -z -4 "$file" && advdef -z -4 "$file" | grep Output + git add "$file" + done +} + +compress-svg() { + git-staged-files \*.svg|while map file; do + make nodecheck + echo "Compressing $file" + node_modules/.bin/svgo --config=.svgo.yml "$file" + git add "$file" + done +} + +test-whitespace() { git diff --cached --check; } + +test-js() { + local err=0 + + make eslint || err+=1 + + if git-is-staged \*.js; then + make qunit || err+=1 + fi + + return $err +} + +test-php() { + local err=0 + if git-is-staged \*.php; then + make phplint || err+=1 + fi + + # todo: where is result set? + if git-is-staged 'includes/skins/*.php'; then + make validatehtml > $result || err+=1 + fi + + return $err +} + +main() { + local err=0 + + compress-png + compress-svg + + test-whitespace || err+=1 + test-js || err+=1 + test-php || err+=1 + + return $err +} + +main "$@" diff --git a/dev-scripts/svg_check.sh b/dev-scripts/svg_check.sh new file mode 100755 index 0000000..3c7ef64 --- /dev/null +++ b/dev-scripts/svg_check.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +found=0 + +for svgfile in `find resources -type f -name "*.svg"`; do + outfile="$svgfile.tmp" + node_modules/.bin/svgo --config .svgo.yml -i $svgfile -o $outfile -q + if [ $(wc -c $svgfile | awk '{print $1}') -gt $(wc -c $outfile | awk '{print $1}') ]; then + echo "File $svgfile is not compressed" + found=$((found + 1)) + fi + rm $outfile +done + +if [ $found -gt 0 ]; then + echo "Found $found uncompressed SVG files. Please compress the files and re-submit the patch" + exit 1 +fi \ No newline at end of file diff --git a/package.json b/package.json index 9ae3ac4..7184a14 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,13 @@ { "private": true, "scripts": { - "test": "grunt test" + "test": "grunt test && dev-scripts/svg_check.sh" }, "dependencies": { "svgo": ">=0.4.4" }, "devDependencies": { - "eslint-config-wikimedia": "0.3.0", + "eslint-config-wikimedia": "0.4.0", "grunt": "^1.0.1", "grunt-banana-checker": "^0.5.0", "grunt-contrib-watch": "^1.0.0",