Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

fix: add setup-lint command for ui contents #1219

shuai9578aba58cbf
diff --git a/ui/.lintstagedrc.json b/ui/.lintstagedrc.json
index a456fe58..9eb3c445 100644
--- a/ui/.lintstagedrc.json
+++ b/ui/.lintstagedrc.json
@@ -1,5 +1,9 @@
{
- "*.{js,jsx,ts,tsx}": ["eslint --cache --fix"],
- "*.{js,jsx,scss,md,json}": ["prettier --write"],
- "*.ts?(x)": ["prettier --parser=typescript --write"]
-}
+ "src/**/*.{ts,tsx}": [
+ "eslint --fix",
+ "prettier --write"
+ ],
+ "src/**/*.{scss,md}": [
+ "prettier --write"
+ ]
+}
\ No newline at end of file
diff --git a/ui/.prettierrc.json b/ui/.prettierrc.json
index c707926a..37d1a10d 100644
--- a/ui/.prettierrc.json
+++ b/ui/.prettierrc.json
@@ -4,5 +4,6 @@
"singleQuote": true,
"jsxBracketSameLine": true,
"printWidth": 80,
- "endOfLine": "auto"
+ "endOfLine": "auto",
+ "bracketSameLine": true
}
diff --git a/ui/package.json b/ui/package.json
index 15bcaaa0..c7d1ec1b 100644
--- a/ui/package.json
+++ b/ui/package.json
@@ -13,7 +13,10 @@
"pre-commit": "lint-staged",
"build:packages": "pnpm -r --filter=./src/plugins/* run build",
"clean": "rm -rf node_modules && rm -rf src/plugins/**/node_modules",
- "analyze": "source-map-explorer 'build/static/js/*.js'"
+ "analyze": "source-map-explorer 'build/static/js/*.js'",
+ "setup-lint": "node scripts/setup-eslint.js && cd .. && husky install",
+ "format": "prettier --write \"src/**/*.{ts,tsx,css,scss,md}\"",
+ "lint-staged": "lint-staged"
},
"dependencies": {
"@codemirror/lang-markdown": "^6.2.4",
diff --git a/ui/scripts/setup-eslint.js b/ui/scripts/setup-eslint.js
new file mode 100644
index 00000000..fc612620
--- /dev/null
+++ b/ui/scripts/setup-eslint.js
@@ -0,0 +1,153 @@
+#!/usr/bin/env node
+
+const fs = require('fs');
+const path = require('path');
+const { execSync } = require('child_process');
+
+const UI_DIR = path.resolve(__dirname, '..'); // UI directory
+const ROOT_DIR = path.resolve(UI_DIR, '..'); // Project root directory
+const GIT_DIR = getGitDir(ROOT_DIR); // Git root directory
+const HUSKY_DIR = path.join(GIT_DIR, '.husky');
+
+// 查找 Git 目录
+function getGitDir(startDir) {
+ let currentDir = startDir;
+
+ while (currentDir !== path.parse(currentDir).root) {
+ const gitDir = path.join(currentDir, '.git');
+ if (fs.existsSync(gitDir)) {
+ return currentDir;
+ }
+ currentDir = path.dirname(currentDir);
+ }
+
+ throw new Error('Could not find Git directory');
+}
+
+
+if (!fs.existsSync(HUSKY_DIR)) {
+ console.log(`Creating husky directory: ${HUSKY_DIR}`);
+ fs.mkdirSync(HUSKY_DIR, { recursive: true });
+}
+
+if (!fs.existsSync(path.join(HUSKY_DIR, '_'))) {
+ console.log(`Creating husky _ directory: ${path.join(HUSKY_DIR, '_')}`);
+ fs.mkdirSync(path.join(HUSKY_DIR, '_'), { recursive: true });
+}
+
+// init husky
+try {
+ console.log('Initializing husky...');
+ execSync('npx husky install', { cwd: GIT_DIR, stdio: 'inherit' });
+} catch (error) {
+ console.error(`❌ Failed to initialize husky: ${error.message}`);
+ process.exit(1);
+}
+
+// create lint-staged config file
+const lintStagedConfig = {
+ "src/**/*.{ts,tsx}": [
+ "eslint --fix",
+ "prettier --write"
+ ],
+ "src/**/*.{scss,md}": [
+ "prettier --write"
+ ]
+};
+
+console.log(`Creating lint-staged config: ${path.join(UI_DIR, '.lintstagedrc.json')}`);
+fs.writeFileSync(
+ path.join(UI_DIR, '.lintstagedrc.json'),
+ JSON.stringify(lintStagedConfig, null, 2)
+);
+
+// create pre-commit hook
+const preCommitContent = `#!/bin/sh
+. "$(dirname "$0")/_/husky.sh"
+
+echo "🔍 Start running the code check..."
+
+# Getting the Git Root Directory
+GIT_ROOT=$(git rev-parse --show-toplevel)
+
+# Get a list of staging files
+STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR)
+
+# Check for files in the ui/ directory
+UI_FILES=$(echo "$STAGED_FILES" | grep '^ui/' || echo "")
+
+if [ -n "$UI_FILES" ]; then
+ echo "🔎 Discover ui file changes, run code checks..."
+
+ # Switch to the ui directory
+ cd "$GIT_ROOT/ui" || {
+ echo "❌ Unable to access the UI catalog"
+ exit 1
+ }
+
+ # 运行 lint-staged
+ echo "✨ Running ESLint and Prettier Formatting..."
+ npx lint-staged --verbose
+
+ LINT_STAGED_RESULT=$?
+ if [ $LINT_STAGED_RESULT -ne 0 ]; then
+ echo "❌ Code check failed, please fix the above problem"
+ exit $LINT_STAGED_RESULT
+ fi
+
+ echo "✅ Code check passes!"
+else
+ echo "ℹ️ No front-end file changes found, skip code checking"
+fi
+
+echo "🎉 Pre-submission check completed"
+`;
+
+console.log(`Creating pre-commit hook: ${path.join(HUSKY_DIR, 'pre-commit')}`);
+fs.writeFileSync(path.join(HUSKY_DIR, 'pre-commit'), preCommitContent);
+execSync(`chmod +x ${path.join(HUSKY_DIR, 'pre-commit')}`);
+
+// create husky.sh
+const huskyShContent = `#!/bin/sh
+if [ -z "$husky_skip_init" ]; then
+ debug () {
+ if [ "$HUSKY_DEBUG" = "1" ]; then
+ echo "husky (debug) - $1"
+ fi
+ }
+
+ readonly hook_name="$(basename "$0")"
+ debug "starting $hook_name..."
+
+ if [ "$HUSKY" = "0" ]; then
+ debug "HUSKY=0, skip hook"
+ exit 0
+ fi
+
+ if [ -f ~/.huskyrc ]; then
+ debug "sourcing ~/.huskyrc"
+ . ~/.huskyrc
+ fi
+
+ export readonly husky_skip_init=1
+ sh -e "$0" "$@"
+ exitCode="$?"
+
+ if [ $exitCode != 0 ]; then
+ echo "husky - $hook_name hook exited with code $exitCode (error)"
+ fi
+
+ exit $exitCode
+fi
+`;
+
+console.log(`Creating husky.sh: ${path.join(HUSKY_DIR, '_', 'husky.sh')}`);
+fs.writeFileSync(
+ path.join(HUSKY_DIR, '_', 'husky.sh'),
+ huskyShContent
+);
+execSync(`chmod +x ${path.join(HUSKY_DIR, '_', 'husky.sh')}`);
+
+console.log('Lint setup complete! Husky and lint-staged have been configured.');
+console.log(`Git root directory: ${GIT_DIR}`);
+console.log(`Husky directory: ${HUSKY_DIR}`);
diff --git a/ui/src/components/AccordionNav/index.tsx b/ui/src/components/AccordionNav/index.tsx
index a0e666e1..d6f84c19 100644
--- a/ui/src/components/AccordionNav/index.tsx
+++ b/ui/src/components/AccordionNav/index.tsx
@@ -38,6 +38,7 @@ function MenuNode({
const { t } = useTranslation('translation', { keyPrefix: 'nav_menus' });
const isLeaf = !menu.children.length;
const href = isLeaf ? `${path}${menu.path}` : '#';
+
return (
<Nav.Item key={menu.path} className="w-100">
{isLeaf ? (