Skip to content

Commit

Permalink
chore: Pretty pre-commit (#6423)
Browse files Browse the repository at this point in the history
## Summary

Making `pre-commit` script more readable, improving the overall quality
of precommit checks and some random repo improvements.

## Test plan

🚀
  • Loading branch information
tjzel authored Aug 19, 2024
1 parent 467289a commit c984e42
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 60 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,6 @@ plugin/types
!.yarn/releases
!.yarn/sdks
!.yarn/versions

# pre-commit
.husky/log.txt
72 changes: 50 additions & 22 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,36 +1,64 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

REANIMATED_PATH=packages/react-native-reanimated
print() {
echo " [PRECOMMIT]: $1" >&2
}

# `lint-staged` is great, but its' parsing capabilites of more advanced CLIs is poor.
# We can use it reliably only with programs that expect the following syntax:
# command [options] [arg1] [arg2] [arg3] ...
# more sophisiticated checks should be done in this script without `lint-staged`.
print 'Running "lint-staged" ...'
yarn lint-staged

cd $REANIMATED_PATH
# This step can take several seconds, so we don't want to run it on irrelevant commits.
! git diff-index --name-only HEAD -- | grep "$REANIMATED_PATH/src/.*\.\(ts\|tsx\)$" >/dev/null
if [ $? -eq 1 ]; then
yarn type:check
yarn find-unused-code:js
LOGFILE=.husky/log.txt
try() {
if ! "$@" >"$LOGFILE" 2>&1; then
print "Errors detected. Aborting commit."
print "Error logs:\n"
cat "$LOGFILE"
exit 1
fi
}

# Some precommit checks can take several seconds. Therefore, we
# want them to trigger only when relevant files are staged.
STAGED_FILES=$(git diff-index HEAD --cached --name-only)

print "Checking for changes in Reanimated TypeScript source files..."
REANIMATED_PATH="packages/react-native-reanimated"
if echo "$STAGED_FILES" | grep "$REANIMATED_PATH/src/.*\.\(ts\|tsx\)$" --silent; then
print "Changes spotted. Running TypeScript checks..."

try yarn workspace react-native-reanimated type:check
try yarn workspace react-native-reanimated find-unused-code:js

print "Success."
else
echo "[PRECOMMIT]: Reanimated TypeScript source files weren't changed. Skipping TypeScript checks." >&2
print "No changes. Skipping TypeScript checks."
fi

# This automatically builds Reanimated Babel plugin JavaScript files if their
# TypeScript counterparts were changed. It also adds the output file to the commit
# if the built file differs from currently committed one.
! git diff-index HEAD --name-only |
grep -E "$REANIMATED_PATH/plugin/" >/dev/null
if [ $? -eq 1 ]; then
yarn build:plugin
! git status -u |
grep -E 'plugin/build/plugin.js' >/dev/null
if [ $? -eq 1 ]; then
git add plugin/build/plugin.js
echo "[PRECOMMIT]: Babel plugin files were automatically built and changes were spotted.\
\n Those changes were added to the commit." >&2
# if the built file differs from currently staged one.
print "Checking for changes in Reanimated Babel plugin source files..."
PLUGIN_PATH="$REANIMATED_PATH/plugin"
if echo "$STAGED_FILES" | grep -E "$PLUGIN_PATH" >/dev/null; then
print "Changes spotted. Building Reanimated Babel plugin files..."

yarn workspace babel-plugin-reanimated build

PLUGIN_OUTPUT_PATH="$PLUGIN_PATH/build/plugin.js"
if git status -u | grep -E "$PLUGIN_OUTPUT_PATH" --silent; then
git add "$PLUGIN_PATH"
print "Non-commited changes were automatically added to the commit."
else
echo "[PRECOMMIT]: All Babel plugin files were already commited." >&2
print "All Babel plugin files were already commited."
fi
else
echo "[PRECOMMIT]: Babel plugin files weren't changed. Skipping plugin checks." >&2
print "No changes. Skipping plugin checks."
fi

print "All good, committing..."

exit 0
3 changes: 3 additions & 0 deletions .lintstagedrc-common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
'*.(js|ts|tsx)': ['yarn eslint', 'yarn prettier --write'],
};
5 changes: 5 additions & 0 deletions apps/common-app/.lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const commonConfig = require('../../.lintstagedrc-common.js');

module.exports = {
...commonConfig,
};
4 changes: 2 additions & 2 deletions apps/web-example/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "web-example",
"main": "index.js",
"private": true,
"version": "0.0.1",
"scripts": {
"start": "expo start --web",
Expand Down Expand Up @@ -30,6 +31,5 @@
"prettier": "2.8.8",
"serve": "^14.2.3",
"typescript": "~5.3.0"
},
"private": true
}
}
1 change: 1 addition & 0 deletions packages/eslint-plugin-reanimated/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "eslint-plugin-reanimated",
"version": "1.0.0",
"private": true,
"license": "MIT",
"types": "lib",
"scripts": {
Expand Down
15 changes: 3 additions & 12 deletions packages/react-native-reanimated/.lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
const REANIMATED_PATH = 'packages/react-native-reanimated';
const commonConfig = require('../../.lintstagedrc-common.js');

module.exports = {
'*.(js|ts|tsx)': [
`cd ${REANIMATED_PATH} && yarn eslint `,
`cd ${REANIMATED_PATH} && yarn eslint --quiet --ext '.js,.ts,.tsx' src/`,
`cd ${REANIMATED_PATH} && yarn prettier --write`,
],
'plugin/**/*.{js,ts,tsx}': `cd ${REANIMATED_PATH} && yarn lint:plugin`,
'**/*.{h,cpp}': `cd ${REANIMATED_PATH} && yarn lint:cpp`,
'android/src/**/*.java': `cd ${REANIMATED_PATH} && yarn format:java`,
'android/src/**/*.{h,cpp}': `cd ${REANIMATED_PATH} && yarn format:android`,
'apple/**/*.{h,m,mm,cpp}': `cd ${REANIMATED_PATH} && yarn format:ios`,
'Common/**/*.{h,cpp}': `cd ${REANIMATED_PATH} && yarn format:common`,
...commonConfig,
};
5 changes: 5 additions & 0 deletions packages/react-native-reanimated/plugin/.lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const commonConfig = require('../../../.lintstagedrc-common.js');

module.exports = {
...commonConfig,
};
3 changes: 3 additions & 0 deletions packages/react-native-reanimated/plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"name": "babel-plugin-reanimated",
"version": "1.0.0",
"private": true,
"devDependencies": {
"@babel/cli": "^7.20.0",
"@babel/core": "^7.20.0",
Expand Down
48 changes: 24 additions & 24 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8842,6 +8842,30 @@ __metadata:
languageName: node
linkType: hard

"babel-plugin-reanimated@workspace:packages/react-native-reanimated/plugin":
version: 0.0.0-use.local
resolution: "babel-plugin-reanimated@workspace:packages/react-native-reanimated/plugin"
dependencies:
"@babel/cli": "npm:^7.20.0"
"@babel/core": "npm:^7.20.0"
"@babel/plugin-transform-class-properties": "npm:^7.24.7"
"@babel/plugin-transform-classes": "npm:^7.24.8"
"@babel/plugin-transform-unicode-regex": "npm:^7.24.7"
"@babel/traverse": "npm:^7.20.0"
"@babel/types": "npm:^7.20.0"
"@react-native/eslint-config": "npm:^0.72.1"
"@types/node": "npm:^18.15.11"
"@typescript-eslint/eslint-plugin": "npm:^7.0.2"
"@typescript-eslint/parser": "npm:^7.0.2"
esbuild: "npm:^0.17.11"
eslint: "npm:^8.0.0-0"
eslint-plugin-standard: "npm:^5.0.0"
prettier: "npm:^2.5.1"
tsc-watch: "npm:^6.0.0"
typescript: "npm:^4.1.3"
languageName: unknown
linkType: soft

"babel-plugin-syntax-trailing-function-commas@npm:^7.0.0-beta.0":
version: 7.0.0-beta.0
resolution: "babel-plugin-syntax-trailing-function-commas@npm:7.0.0-beta.0"
Expand Down Expand Up @@ -17804,30 +17828,6 @@ __metadata:
languageName: node
linkType: hard

"plugin-568de2@workspace:packages/react-native-reanimated/plugin":
version: 0.0.0-use.local
resolution: "plugin-568de2@workspace:packages/react-native-reanimated/plugin"
dependencies:
"@babel/cli": "npm:^7.20.0"
"@babel/core": "npm:^7.20.0"
"@babel/plugin-transform-class-properties": "npm:^7.24.7"
"@babel/plugin-transform-classes": "npm:^7.24.8"
"@babel/plugin-transform-unicode-regex": "npm:^7.24.7"
"@babel/traverse": "npm:^7.20.0"
"@babel/types": "npm:^7.20.0"
"@react-native/eslint-config": "npm:^0.72.1"
"@types/node": "npm:^18.15.11"
"@typescript-eslint/eslint-plugin": "npm:^7.0.2"
"@typescript-eslint/parser": "npm:^7.0.2"
esbuild: "npm:^0.17.11"
eslint: "npm:^8.0.0-0"
eslint-plugin-standard: "npm:^5.0.0"
prettier: "npm:^2.5.1"
tsc-watch: "npm:^6.0.0"
typescript: "npm:^4.1.3"
languageName: unknown
linkType: soft

"pluralize@npm:^8.0.0":
version: 8.0.0
resolution: "pluralize@npm:8.0.0"
Expand Down

0 comments on commit c984e42

Please sign in to comment.