Skip to content

Commit c40fe02

Browse files
author
shuai
committed
Merge branch 'setup-eslint' into test
2 parents 82a230e + de27cad commit c40fe02

6 files changed

Lines changed: 172 additions & 9 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ vendor/
3030
/new_answer
3131

3232
dist/
33+
34+
# Lint setup generated file
35+
.husky/

ui/.lintstagedrc.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
2-
"*.{js,jsx,ts,tsx}": ["eslint --cache --fix"],
3-
"*.{js,jsx,scss,md,json}": ["prettier --write"],
4-
"*.ts?(x)": ["prettier --parser=typescript --write"]
5-
}
2+
"src/**/*.{ts,tsx}": [
3+
"eslint --fix",
4+
"prettier --write"
5+
],
6+
"src/**/*.{scss,md}": [
7+
"prettier --write"
8+
]
9+
}

ui/.prettierrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"singleQuote": true,
55
"jsxBracketSameLine": true,
66
"printWidth": 80,
7-
"endOfLine": "auto"
7+
"endOfLine": "auto",
8+
"bracketSameLine": true
89
}

ui/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
"scripts": {
77
"start": "react-app-rewired start",
88
"build": "node ./scripts/env.js && react-app-rewired build",
9-
"lint": "eslint . --cache --fix --ext .ts,.tsx",
10-
"prettier": "prettier --write 'src/**/*.{js,jsx,ts,tsx,json,css,scss,md}' --config ./.prettierrc.json",
119
"pre-install": "node ./scripts/importPlugins.js && pnpm install && node ./scripts/preinstall.js ",
1210
"prepare": "pnpm build:packages",
13-
"pre-commit": "lint-staged",
1411
"build:packages": "pnpm -r --filter=./src/plugins/* run build",
1512
"clean": "rm -rf node_modules && rm -rf src/plugins/**/node_modules",
16-
"analyze": "source-map-explorer 'build/static/js/*.js'"
13+
"analyze": "source-map-explorer 'build/static/js/*.js'",
14+
"setup-lint": "node scripts/setup-eslint.js && cd .. && husky install",
15+
"lint": "eslint . --cache --fix --ext .ts,.tsx",
16+
"prettier": "prettier --write \"src/**/*.{ts,tsx,css,scss,md}\"",
17+
"lint-staged": "lint-staged"
1718
},
1819
"dependencies": {
1920
"@codemirror/lang-markdown": "^6.2.4",

ui/scripts/setup-eslint.js

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
const { execSync } = require('child_process');
6+
7+
const UI_DIR = path.resolve(__dirname, '..'); // UI directory
8+
const ROOT_DIR = path.resolve(UI_DIR, '..'); // Project root directory
9+
const GIT_DIR = getGitDir(ROOT_DIR); // Git root directory
10+
const HUSKY_DIR = path.join(GIT_DIR, '.husky');
11+
12+
// 查找 Git 目录
13+
function getGitDir(startDir) {
14+
let currentDir = startDir;
15+
16+
while (currentDir !== path.parse(currentDir).root) {
17+
const gitDir = path.join(currentDir, '.git');
18+
if (fs.existsSync(gitDir)) {
19+
return currentDir;
20+
}
21+
currentDir = path.dirname(currentDir);
22+
}
23+
24+
throw new Error('Could not find Git directory');
25+
}
26+
27+
28+
if (!fs.existsSync(HUSKY_DIR)) {
29+
console.log(`Creating husky directory: ${HUSKY_DIR}`);
30+
fs.mkdirSync(HUSKY_DIR, { recursive: true });
31+
}
32+
33+
if (!fs.existsSync(path.join(HUSKY_DIR, '_'))) {
34+
console.log(`Creating husky _ directory: ${path.join(HUSKY_DIR, '_')}`);
35+
fs.mkdirSync(path.join(HUSKY_DIR, '_'), { recursive: true });
36+
}
37+
38+
// init husky
39+
try {
40+
console.log('Initializing husky...');
41+
execSync('npx husky install', { cwd: GIT_DIR, stdio: 'inherit' });
42+
} catch (error) {
43+
console.error(`❌ Failed to initialize husky: ${error.message}`);
44+
process.exit(1);
45+
}
46+
47+
// create lint-staged config file
48+
const lintStagedConfig = {
49+
"src/**/*.{ts,tsx}": [
50+
"eslint --fix",
51+
"prettier --write"
52+
],
53+
"src/**/*.{scss,md}": [
54+
"prettier --write"
55+
]
56+
};
57+
58+
console.log(`Creating lint-staged config: ${path.join(UI_DIR, '.lintstagedrc.json')}`);
59+
fs.writeFileSync(
60+
path.join(UI_DIR, '.lintstagedrc.json'),
61+
JSON.stringify(lintStagedConfig, null, 2)
62+
);
63+
64+
// create pre-commit hook
65+
const preCommitContent = `#!/bin/sh
66+
. "$(dirname "$0")/_/husky.sh"
67+
68+
echo "🔍 Start running the code check..."
69+
70+
# Getting the Git Root Directory
71+
GIT_ROOT=$(git rev-parse --show-toplevel)
72+
73+
# Get a list of staging files
74+
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR)
75+
76+
# Check for files in the ui/ directory
77+
UI_FILES=$(echo "$STAGED_FILES" | grep '^ui/' || echo "")
78+
79+
if [ -n "$UI_FILES" ]; then
80+
echo "🔎 Discover ui file changes, run code checks..."
81+
82+
# Switch to the ui directory
83+
cd "$GIT_ROOT/ui" || {
84+
echo "❌ Unable to access the UI catalog"
85+
exit 1
86+
}
87+
88+
# 运行 lint-staged
89+
echo "✨ Running ESLint and Prettier Formatting..."
90+
npx lint-staged --verbose
91+
92+
LINT_STAGED_RESULT=$?
93+
if [ $LINT_STAGED_RESULT -ne 0 ]; then
94+
echo "❌ Code check failed, please fix the above problem"
95+
exit $LINT_STAGED_RESULT
96+
fi
97+
98+
echo "✅ Code check passes!"
99+
else
100+
echo "ℹ️ No front-end file changes found, skip code checking"
101+
fi
102+
103+
echo "🎉 Pre-submission check completed"
104+
`;
105+
106+
console.log(`Creating pre-commit hook: ${path.join(HUSKY_DIR, 'pre-commit')}`);
107+
fs.writeFileSync(path.join(HUSKY_DIR, 'pre-commit'), preCommitContent);
108+
execSync(`chmod +x ${path.join(HUSKY_DIR, 'pre-commit')}`);
109+
110+
// create husky.sh
111+
const huskyShContent = `#!/bin/sh
112+
if [ -z "$husky_skip_init" ]; then
113+
debug () {
114+
if [ "$HUSKY_DEBUG" = "1" ]; then
115+
echo "husky (debug) - $1"
116+
fi
117+
}
118+
119+
readonly hook_name="$(basename "$0")"
120+
debug "starting $hook_name..."
121+
122+
if [ "$HUSKY" = "0" ]; then
123+
debug "HUSKY=0, skip hook"
124+
exit 0
125+
fi
126+
127+
if [ -f ~/.huskyrc ]; then
128+
debug "sourcing ~/.huskyrc"
129+
. ~/.huskyrc
130+
fi
131+
132+
export readonly husky_skip_init=1
133+
sh -e "$0" "$@"
134+
exitCode="$?"
135+
136+
if [ $exitCode != 0 ]; then
137+
echo "husky - $hook_name hook exited with code $exitCode (error)"
138+
fi
139+
140+
exit $exitCode
141+
fi
142+
`;
143+
144+
console.log(`Creating husky.sh: ${path.join(HUSKY_DIR, '_', 'husky.sh')}`);
145+
fs.writeFileSync(
146+
path.join(HUSKY_DIR, '_', 'husky.sh'),
147+
huskyShContent
148+
);
149+
execSync(`chmod +x ${path.join(HUSKY_DIR, '_', 'husky.sh')}`);
150+
151+
console.log('Lint setup complete! Husky and lint-staged have been configured.');
152+
console.log(`Git root directory: ${GIT_DIR}`);
153+
console.log(`Husky directory: ${HUSKY_DIR}`);

ui/src/components/AccordionNav/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ function MenuNode({
3838
const { t } = useTranslation('translation', { keyPrefix: 'nav_menus' });
3939
const isLeaf = !menu.children.length;
4040
const href = isLeaf ? `${path}${menu.path}` : '#';
41+
4142
return (
4243
<Nav.Item key={menu.path} className="w-100">
4344
{isLeaf ? (

0 commit comments

Comments
 (0)