Commit 0964815a by liimin

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

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