Commit 0964815a by liimin

style: 修复文件末尾缺少换行符并统一代码格式

- 为所有文件添加末尾换行符以符合规范
- 统一代码缩进和格式化风格
- 简化部分冗余代码结构
- 更新README文档格式
1 parent e5706142
Showing 31 changed files with 79 additions and 70 deletions
...@@ -4,4 +4,6 @@ addwords ...@@ -4,4 +4,6 @@ addwords
noneles noneles
rangestart rangestart
compileroptions compileroptions
lintstagedrc
\ No newline at end of file \ No newline at end of file
lintstagedrc
EPERM
customflags
\ No newline at end of file \ No newline at end of file
...@@ -8,6 +8,9 @@ export const version = "0.0.1"; ...@@ -8,6 +8,9 @@ export const version = "0.0.1";
type Flags = Record<string, string | boolean>; type Flags = Record<string, string | boolean>;
type JsonPrimitive = string | number | boolean | null;
type JsonValue = JsonPrimitive | { [key: string]: JsonValue } | JsonValue[];
function parseArgs(argv: string[]): Flags { function parseArgs(argv: string[]): Flags {
const out: Flags = {}; const out: Flags = {};
for (let i = 0; i < argv.length; i++) { for (let i = 0; i < argv.length; i++) {
...@@ -62,19 +65,7 @@ function printHelp() { ...@@ -62,19 +65,7 @@ function printHelp() {
function isTextFile(file: string) { function isTextFile(file: string) {
const ext = path.extname(file).toLowerCase(); const ext = path.extname(file).toLowerCase();
return [ return [".ts", ".tsx", ".js", ".jsx", ".json", ".vue", ".css", ".scss", ".less", ".md", ".html"].includes(ext);
".ts",
".tsx",
".js",
".jsx",
".json",
".vue",
".css",
".scss",
".less",
".md",
".html"
].includes(ext);
} }
async function ensureDir(dir: string) { async function ensureDir(dir: string) {
...@@ -98,30 +89,32 @@ async function copyDir(src: string, dest: string, options?: { skip?: (relPath: s ...@@ -98,30 +89,32 @@ async function copyDir(src: string, dest: string, options?: { skip?: (relPath: s
} }
} }
async function mergeJson(targetPath: string, mergeObj: Record<string, any>) { async function mergeJson(targetPath: string, mergeObj: Record<string, JsonValue>) {
const exists = fs.existsSync(targetPath); const exists = fs.existsSync(targetPath);
const targetObj = exists ? JSON.parse(await fsp.readFile(targetPath, "utf-8")) : {}; const targetObj: JsonValue = exists ? (JSON.parse(await fsp.readFile(targetPath, "utf-8")) as JsonValue) : {};
const merged = deepMerge(targetObj, mergeObj); const merged = deepMerge(targetObj, mergeObj);
await fsp.writeFile(targetPath, JSON.stringify(merged, null, 2)); await fsp.writeFile(targetPath, JSON.stringify(merged, null, 2));
} }
function deepMerge(a: any, b: any): any { function deepMerge(a: JsonValue, b: JsonValue): JsonValue {
if (Array.isArray(a) && Array.isArray(b)) { if (Array.isArray(a) && Array.isArray(b)) {
return Array.from(new Set([...a, ...b])); return Array.from(new Set([...a, ...b]));
} }
if (isPlainObject(a) && isPlainObject(b)) { if (isPlainObject(a) && isPlainObject(b)) {
const out: Record<string, any> = { ...a }; const aObj = a as Record<string, JsonValue>;
for (const key of Object.keys(b)) { const bObj = b as Record<string, JsonValue>;
if (key in a) out[key] = deepMerge(a[key], b[key]); const out: Record<string, JsonValue> = { ...aObj };
else out[key] = b[key]; for (const key of Object.keys(bObj)) {
if (key in aObj) out[key] = deepMerge(aObj[key], bObj[key]);
else out[key] = bObj[key];
} }
return out; return out;
} }
return b ?? a; return b ?? a;
} }
function isPlainObject(v: any) { function isPlainObject(v: unknown): v is Record<string, JsonValue> {
return v && typeof v === "object" && !Array.isArray(v); return !!v && typeof v === "object" && !Array.isArray(v);
} }
async function replacePlaceholders(destRoot: string, replacements: Record<string, string>) { async function replacePlaceholders(destRoot: string, replacements: Record<string, string>) {
...@@ -147,7 +140,7 @@ async function applyOptionalFeature(destRoot: string, tplRoot: string, featurePa ...@@ -147,7 +140,7 @@ async function applyOptionalFeature(destRoot: string, tplRoot: string, featurePa
if (!fs.existsSync(src)) return; if (!fs.existsSync(src)) return;
// Copy files excluding merge manifest // Copy files excluding merge manifest
await copyDir(src, destRoot, { await copyDir(src, destRoot, {
skip: (rel) => rel.endsWith("_package.merge.json") skip: rel => rel.endsWith("_package.merge.json")
}); });
const mergeFile = path.join(src, "_package.merge.json"); const mergeFile = path.join(src, "_package.merge.json");
if (fs.existsSync(mergeFile)) { if (fs.existsSync(mergeFile)) {
...@@ -157,7 +150,7 @@ async function applyOptionalFeature(destRoot: string, tplRoot: string, featurePa ...@@ -157,7 +150,7 @@ async function applyOptionalFeature(destRoot: string, tplRoot: string, featurePa
} }
async function runCreate(argv: string[]) { async function runCreate(argv: string[]) {
const [, , cmd, name, ...rest] = argv; const [, , , name, ...rest] = argv;
const flags = parseArgs(rest); const flags = parseArgs(rest);
if (!name) { if (!name) {
console.error("请输入项目名称,例如: venus create my-app --template react"); console.error("请输入项目名称,例如: venus create my-app --template react");
...@@ -201,7 +194,7 @@ async function runCreate(argv: string[]) { ...@@ -201,7 +194,7 @@ async function runCreate(argv: string[]) {
// Copy base template excluding optional directory and any stray merge manifests // Copy base template excluding optional directory and any stray merge manifests
await copyDir(tplRoot, destRoot, { await copyDir(tplRoot, destRoot, {
skip: (rel) => rel.startsWith("optional") || path.basename(rel) === "_package.merge.json" skip: rel => rel.startsWith("optional") || path.basename(rel) === "_package.merge.json"
}); });
// Apply optional features // Apply optional features
...@@ -223,14 +216,16 @@ async function runCreate(argv: string[]) { ...@@ -223,14 +216,16 @@ async function runCreate(argv: string[]) {
console.log("安装依赖中 (pnpm install)..."); console.log("安装依赖中 (pnpm install)...");
try { try {
child_process.execSync("pnpm install", { stdio: "inherit", cwd: destRoot, shell: true }); child_process.execSync("pnpm install", { stdio: "inherit", cwd: destRoot, shell: true });
} catch (e) { } catch {
console.warn("依赖安装失败,请手动在项目目录执行 pnpm install。"); console.warn("依赖安装失败,请手动在项目目录执行 pnpm install。");
} }
} }
console.log(`项目创建成功: ${destRoot}`); console.log(`项目创建成功: ${destRoot}`);
console.log(`模板: ${template}`); console.log(`模板: ${template}`);
console.log(`可选功能: router=${hasRouter ? "on" : "off"}, store=${hasStore ? "on" : "off"}, eslint=${hasEslint ? "on" : "off"}, ui=${ui || "none"}`); console.log(
`可选功能: router=${hasRouter ? "on" : "off"}, store=${hasStore ? "on" : "off"}, eslint=${hasEslint ? "on" : "off"}, ui=${ui || "none"}`
);
console.log("下一步:"); console.log("下一步:");
console.log(` cd ${destRoot}`); console.log(` cd ${destRoot}`);
if (noInstall) { if (noInstall) {
...@@ -247,7 +242,7 @@ async function runBuild() { ...@@ -247,7 +242,7 @@ async function runBuild() {
} }
try { try {
child_process.execSync(`node ${JSON.stringify(buildScript)}`, { stdio: "inherit", shell: true }); child_process.execSync(`node ${JSON.stringify(buildScript)}`, { stdio: "inherit", shell: true });
} catch (e) { } catch {
process.exit(1); process.exit(1);
} }
} }
...@@ -276,7 +271,9 @@ export async function main() { ...@@ -276,7 +271,9 @@ export async function main() {
return; return;
} }
console.log("可用命令: create, build"); console.log("可用命令: create, build");
console.log("示例: venus create my-app --template react --no-install --dir apps/customflags --router --store --ui antd --eslint"); console.log(
"示例: venus create my-app --template react --no-install --dir apps/customflags --router --store --ui antd --eslint"
);
} }
// 自动执行 // 自动执行
...@@ -306,7 +303,10 @@ async function runListTemplates() { ...@@ -306,7 +303,10 @@ async function runListTemplates() {
const uiDir = path.join(optionalDir, "ui"); const uiDir = path.join(optionalDir, "ui");
if (fs.existsSync(uiDir)) { if (fs.existsSync(uiDir)) {
const uis = await fsp.readdir(uiDir, { withFileTypes: true }); const uis = await fsp.readdir(uiDir, { withFileTypes: true });
const uiNames = uis.filter(u => u.isDirectory()).map(u => u.name).join(", "); const uiNames = uis
.filter(u => u.isDirectory())
.map(u => u.name)
.join(", ");
features.push(`ui: [${uiNames}]`); features.push(`ui: [${uiNames}]`);
} }
} else { } else {
......
# __APP_NAME__ (React + Vite + TS) # **APP_NAME** (React + Vite + TS)
- 开发:`pnpm dev` - 开发:`pnpm dev`
- 构建:`pnpm build` - 构建:`pnpm build`
- 预览:`pnpm preview` - 预览:`pnpm preview`
可选特性(若在脚手架选择): 可选特性(若在脚手架选择):
- 路由:`react-router-dom` - 路由:`react-router-dom`
- 状态管理:`@reduxjs/toolkit` + `react-redux` - 状态管理:`@reduxjs/toolkit` + `react-redux`
- UI:`antd` - UI:`antd`
- ESLint:基础配置 `.eslintrc.json`
\ No newline at end of file \ No newline at end of file
- ESLint:基础配置 `.eslintrc.json`
...@@ -9,4 +9,4 @@ ...@@ -9,4 +9,4 @@
<div id="root"></div> <div id="root"></div>
<script type="module" src="/src/main.tsx"></script> <script type="module" src="/src/main.tsx"></script>
</body> </body>
</html>
\ No newline at end of file \ No newline at end of file
</html>
...@@ -4,4 +4,4 @@ ...@@ -4,4 +4,4 @@
"parserOptions": { "ecmaVersion": 2020, "sourceType": "module" }, "parserOptions": { "ecmaVersion": 2020, "sourceType": "module" },
"extends": ["eslint:recommended", "plugin:react/recommended", "plugin:react-hooks/recommended"], "extends": ["eslint:recommended", "plugin:react/recommended", "plugin:react-hooks/recommended"],
"settings": { "react": { "version": "detect" } } "settings": { "react": { "version": "detect" } }
}
\ No newline at end of file \ No newline at end of file
}
...@@ -2,4 +2,4 @@ ...@@ -2,4 +2,4 @@
"dependencies": { "dependencies": {
"react-router-dom": "^6.27.0" "react-router-dom": "^6.27.0"
} }
}
\ No newline at end of file \ No newline at end of file
}
...@@ -8,4 +8,4 @@ createRoot(container).render( ...@@ -8,4 +8,4 @@ createRoot(container).render(
<BrowserRouter> <BrowserRouter>
<App /> <App />
</BrowserRouter> </BrowserRouter>
);
\ No newline at end of file \ No newline at end of file
);
...@@ -2,4 +2,4 @@ ...@@ -2,4 +2,4 @@
"dependencies": { "dependencies": {
"zustand": "^4.4.0" "zustand": "^4.4.0"
} }
}
\ No newline at end of file \ No newline at end of file
}
...@@ -10,4 +10,4 @@ export function Counter() { ...@@ -10,4 +10,4 @@ export function Counter() {
<button onClick={inc}>+</button> <button onClick={inc}>+</button>
</div> </div>
); );
}
\ No newline at end of file \ No newline at end of file
}
...@@ -6,8 +6,8 @@ type CounterState = { ...@@ -6,8 +6,8 @@ type CounterState = {
dec: () => void; dec: () => void;
}; };
export const useCounter = create<CounterState>((set) => ({ export const useCounter = create<CounterState>(set => ({
count: 0, count: 0,
inc: () => set((s) => ({ count: s.count + 1 })),
dec: () => set((s) => ({ count: s.count - 1 })),
}));
\ No newline at end of file \ No newline at end of file
inc: () => set(s => ({ count: s.count + 1 })),
dec: () => set(s => ({ count: s.count - 1 }))
}));
...@@ -2,4 +2,4 @@ ...@@ -2,4 +2,4 @@
"dependencies": { "dependencies": {
"antd": "^5.19.4" "antd": "^5.19.4"
} }
}
\ No newline at end of file \ No newline at end of file
}
...@@ -3,4 +3,4 @@ import { Button } from "antd"; ...@@ -3,4 +3,4 @@ import { Button } from "antd";
export default function Demo() { export default function Demo() {
return <Button type="primary">Antd Button</Button>; return <Button type="primary">Antd Button</Button>;
}
\ No newline at end of file \ No newline at end of file
}
...@@ -19,4 +19,4 @@ ...@@ -19,4 +19,4 @@
"typescript": "^5.4.0", "typescript": "^5.4.0",
"vite": "^5.0.0" "vite": "^5.0.0"
} }
}
\ No newline at end of file \ No newline at end of file
}
...@@ -7,4 +7,4 @@ export default function App() { ...@@ -7,4 +7,4 @@ export default function App() {
<p>This project was scaffolded by Venus CLI.</p> <p>This project was scaffolded by Venus CLI.</p>
</div> </div>
); );
}
\ No newline at end of file \ No newline at end of file
}
...@@ -3,4 +3,4 @@ import { createRoot } from "react-dom/client"; ...@@ -3,4 +3,4 @@ import { createRoot } from "react-dom/client";
import App from "./App"; import App from "./App";
const container = document.getElementById("root")!; const container = document.getElementById("root")!;
createRoot(container).render(<App />);
\ No newline at end of file \ No newline at end of file
createRoot(container).render(<App />);
...@@ -13,4 +13,4 @@ ...@@ -13,4 +13,4 @@
"strict": true "strict": true
}, },
"include": ["src"] "include": ["src"]
}
\ No newline at end of file \ No newline at end of file
}
...@@ -2,5 +2,5 @@ import { defineConfig } from "vite"; ...@@ -2,5 +2,5 @@ import { defineConfig } from "vite";
import react from "@vitejs/plugin-react"; import react from "@vitejs/plugin-react";
export default defineConfig({ export default defineConfig({
plugins: [react()], plugins: [react()]
}); });
# __APP_NAME__ # **APP_NAME**
该项目由 Venus CLI 脚手架生成,使用 Vite + Vue 3 + TypeScript。
\ No newline at end of file \ No newline at end of file
该项目由 Venus CLI 脚手架生成,使用 Vite + Vue 3 + TypeScript。
...@@ -9,4 +9,4 @@ ...@@ -9,4 +9,4 @@
<div id="app"></div> <div id="app"></div>
<script type="module" src="/src/main.ts"></script> <script type="module" src="/src/main.ts"></script>
</body> </body>
</html>
\ No newline at end of file \ No newline at end of file
</html>
...@@ -4,4 +4,4 @@ ...@@ -4,4 +4,4 @@
"parserOptions": { "ecmaVersion": 2020, "sourceType": "module" }, "parserOptions": { "ecmaVersion": 2020, "sourceType": "module" },
"extends": ["eslint:recommended", "plugin:vue/vue3-recommended"], "extends": ["eslint:recommended", "plugin:vue/vue3-recommended"],
"rules": {} "rules": {}
}
\ No newline at end of file \ No newline at end of file
}
...@@ -2,4 +2,4 @@ ...@@ -2,4 +2,4 @@
"dependencies": { "dependencies": {
"vue-router": "^4.3.0" "vue-router": "^4.3.0"
} }
}
\ No newline at end of file \ No newline at end of file
}
import { createRouter, createWebHistory, type RouteRecordRaw } from "vue-router"; import { createRouter, createWebHistory, type RouteRecordRaw } from "vue-router";
const routes: RouteRecordRaw[] = [ const routes: RouteRecordRaw[] = [{ path: "/", component: () => import("./App.vue") }];
{ path: "/", component: () => import("./App.vue") }
];
export const router = createRouter({ export const router = createRouter({
history: createWebHistory(), history: createWebHistory(),
routes routes
});
\ No newline at end of file \ No newline at end of file
});
...@@ -2,4 +2,4 @@ ...@@ -2,4 +2,4 @@
"dependencies": { "dependencies": {
"pinia": "^2.1.7" "pinia": "^2.1.7"
} }
}
\ No newline at end of file \ No newline at end of file
}
import { createPinia } from "pinia"; import { createPinia } from "pinia";
export const pinia = createPinia();
\ No newline at end of file \ No newline at end of file
export const pinia = createPinia();
...@@ -2,4 +2,4 @@ ...@@ -2,4 +2,4 @@
"dependencies": { "dependencies": {
"element-plus": "^2.6.3" "element-plus": "^2.6.3"
} }
}
\ No newline at end of file \ No newline at end of file
}
...@@ -3,4 +3,4 @@ import ElementPlus from "element-plus"; ...@@ -3,4 +3,4 @@ import ElementPlus from "element-plus";
export function setupUI(app: App) { export function setupUI(app: App) {
app.use(ElementPlus); app.use(ElementPlus);
}
\ No newline at end of file \ No newline at end of file
}
...@@ -17,4 +17,4 @@ ...@@ -17,4 +17,4 @@
"vite": "^5.2.10", "vite": "^5.2.10",
"vue-tsc": "^2.0.21" "vue-tsc": "^2.0.21"
} }
}
\ No newline at end of file \ No newline at end of file
}
...@@ -3,10 +3,18 @@ ...@@ -3,10 +3,18 @@
<h1>Welcome to __APP_NAME__</h1> <h1>Welcome to __APP_NAME__</h1>
<p>This project was scaffolded by Venus CLI.</p> <p>This project was scaffolded by Venus CLI.</p>
</div> </div>
</template> </template>
<script setup lang="ts"></script> <script setup lang="ts"></script>
<style> <style>
body { font-family: system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial; }
</style>
\ No newline at end of file \ No newline at end of file
body {
font-family:
system-ui,
-apple-system,
Segoe UI,
Roboto,
Helvetica,
Arial;
}
</style>
/// <reference types="vite/client" />
\ No newline at end of file \ No newline at end of file
/// <reference types="vite/client" />
import { createApp } from "vue"; import { createApp } from "vue";
import App from "./App.vue"; import App from "./App.vue";
createApp(App).mount("#app");
\ No newline at end of file \ No newline at end of file
createApp(App).mount("#app");
...@@ -14,4 +14,4 @@ ...@@ -14,4 +14,4 @@
"strict": true, "strict": true,
"types": ["vite/client"] "types": ["vite/client"]
} }
}
\ No newline at end of file \ No newline at end of file
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!