Skip to content

Commit 9578aba

Browse files
committed
fix: add setup-lint command for ui contents #1219
1 parent 3acc19c commit 9578aba

File tree

5 files changed

+168
-6
lines changed

5 files changed

+168
-6
lines changed

ui/.lintstagedrc.json

+8-4
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

+2-1
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

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
"pre-commit": "lint-staged",
1414
"build:packages": "pnpm -r --filter=./src/plugins/* run build",
1515
"clean": "rm -rf node_modules && rm -rf src/plugins/**/node_modules",
16-
"analyze": "source-map-explorer 'build/static/js/*.js'"
16+
"analyze": "source-map-explorer 'build/static/js/*.js'",
17+
"setup-lint": "node scripts/setup-eslint.js && cd .. && husky install",
18+
"format": "prettier --write \"src/**/*.{ts,tsx,css,scss,md}\"",
19+
"lint-staged": "lint-staged"
1720
},
1821
"dependencies": {
1922
"@codemirror/lang-markdown": "^6.2.4",

ui/scripts/setup-eslint.js

+153
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

+1
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)