Skip to content

Commit 1e405d8

Browse files
committed
update: gen compoents script
1 parent 0195881 commit 1e405d8

File tree

5 files changed

+170
-6
lines changed

5 files changed

+170
-6
lines changed

docs/home/show/badge.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66

77
基础用法
88

9-
:::raw
109
<preview path="../../components/show/badge/badge.vue" ></preview>
11-
:::
1210

1311
## 限制
1412

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"dev": "pnpm -C play dev",
77
"build": "pnpm build:comp && pnpm gen",
88
"gen": "ts-node script/gen-comp-types dist",
9+
"cp": "ts-node script/cp",
910
"dev:docs": "pnpm -C docs docs:dev",
1011
"build:comp": "ts-node script/build dist",
1112
"build:docs": "pnpm -C docs docs:build",

packages/theme-chalk/src/button.scss

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
padding: 0 20px;
3838
transition: 0.1s;
3939
background-color: $li--color-default;
40+
position: relative;
41+
4042
& + .li-button {
4143
margin-left: 12px;
4244
}
@@ -85,7 +87,7 @@
8587
scale: 0.95;
8688
}
8789
@include when(plain) {
88-
border: solid 1px $li--color-default-light-1;
90+
border: solid 1px $li--color-black;
8991
background-color: $li--color-default;
9092
}
9193
@include when(disabled) {

script/cp.ts

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import {
2+
existsSync,
3+
mkdirSync,
4+
rmSync,
5+
unlinkSync,
6+
writeFileSync,
7+
} from "node:fs";
8+
9+
const path = require("path");
10+
11+
const componentName = process.argv[2];
12+
const isDelete = process.argv[3] === "-d"; // 这里判断是否删除
13+
14+
if (!componentName) {
15+
console.error("请提供组件名称,例如: pnpm cp test");
16+
process.exit(1);
17+
}
18+
19+
// 处理大驼峰命名
20+
const componentNameUpper = componentName
21+
.split("-")
22+
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
23+
.join("");
24+
25+
// 路径配置
26+
const paths = {
27+
components: {
28+
root: path.join(process.cwd(), "packages/components", componentName),
29+
src: path.join(process.cwd(), "packages/components", componentName, "src"),
30+
index: path.join(
31+
process.cwd(),
32+
"packages/components",
33+
componentName,
34+
"index.ts"
35+
),
36+
vue: path.join(
37+
process.cwd(),
38+
"packages/components",
39+
componentName,
40+
"src",
41+
`${componentName}.vue`
42+
),
43+
ts: path.join(
44+
process.cwd(),
45+
"packages/components",
46+
componentName,
47+
"src",
48+
`${componentName}.ts`
49+
),
50+
},
51+
theme: {
52+
scss: path.join(
53+
process.cwd(),
54+
"packages/theme-chalk/src",
55+
`${componentName}.scss`
56+
),
57+
},
58+
};
59+
60+
// 创建目录
61+
const createDir = (dirPath: string) => {
62+
if (!existsSync(dirPath)) {
63+
mkdirSync(dirPath, { recursive: true });
64+
}
65+
};
66+
67+
// 创建文件
68+
const createFile = (
69+
filePath: string,
70+
content: string | NodeJS.ArrayBufferView<ArrayBufferLike>
71+
) => {
72+
if (existsSync(filePath)) {
73+
console.error(`文件已存在: ${filePath}`);
74+
return false;
75+
}
76+
writeFileSync(filePath, content);
77+
return true;
78+
};
79+
80+
// 删除文件
81+
const deleteFile = (filePath: string) => {
82+
if (!existsSync(filePath)) {
83+
return false;
84+
}
85+
unlinkSync(filePath);
86+
return true;
87+
};
88+
// 删除目录
89+
const deleteDir = (dirPath: string) => {
90+
if (!existsSync(dirPath)) {
91+
return false;
92+
}
93+
rmSync(dirPath, { recursive: true });
94+
return true;
95+
};
96+
// 文件模板
97+
const templates = {
98+
index: `import { withInstall } from "@licht-ui/utils";
99+
import _${componentNameUpper} from "./src/${componentName}.vue";
100+
import "@licht-ui/theme-chalk/src/${componentName}.scss";
101+
export * from "./src/${componentName}";
102+
103+
export default withInstall(_${componentNameUpper});
104+
105+
declare module "vue" {
106+
export interface GlobalComponents {
107+
Li${componentNameUpper}: typeof _${componentNameUpper};
108+
}
109+
}`,
110+
111+
vue: `
112+
<template>
113+
</template>
114+
<script setup lang="ts">
115+
import { createNamespace } from "@licht-ui/utils";
116+
import { ${componentName}Type } from "./${componentName}";
117+
defineOptions({
118+
name: 'Li${componentNameUpper}'
119+
});
120+
defineProps(${componentName}Type);
121+
const bem = createNamespace("${componentName}");
122+
</script>
123+
`,
124+
125+
ts: `import { ExtractPropTypes } from "vue";
126+
export const ${componentName}Type = {} as const;
127+
export type ${componentNameUpper}Type = ExtractPropTypes<typeof ${componentName}Type & HTMLAnchorElement>;`,
128+
129+
scss: `@use "./common/var.scss" as *;
130+
@use "./mixins/mixins.scss" as *;
131+
132+
@include b(${componentName}) {
133+
134+
}`,
135+
};
136+
137+
try {
138+
if (isDelete) {
139+
//————————
140+
deleteDir(paths.components.root);
141+
//————————
142+
deleteFile(paths.theme.scss);
143+
console.log(`\x1b[32m组件 ${componentName} 删除成功!\x1b[0m`);
144+
console.log(`组件路径: ${paths.components.root}`);
145+
console.log(`样式路径: ${paths.theme.scss}`);
146+
} else {
147+
//————————
148+
createDir(paths.components.src);
149+
//————————
150+
createFile(paths.components.index, templates.index);
151+
createFile(paths.components.vue, templates.vue);
152+
createFile(paths.components.ts, templates.ts);
153+
154+
createFile(paths.theme.scss, templates.scss);
155+
156+
console.log(`\x1b[32m组件 ${componentName} 创建成功!\x1b[0m`);
157+
console.log(`组件路径: ${paths.components.root}`);
158+
console.log(`样式路径: ${paths.theme.scss}`);
159+
}
160+
} catch (error) {
161+
console.error("\x1b[31m创建组件失败:\x1b[0m", error);
162+
process.exit(1);
163+
}

script/gen-comp-types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Project } from "ts-morph";
44
const build = process.argv[3] || "dist";
55
const project = new Project();
66
console.time("types");
7-
console.log("\x1B[36m%s\x1B[0m", "正在生成类型中...");
7+
console.log("\x1B[36m%s\x1B[0m", "正在生成类型中...\n");
88

99
// todo 自动生成类型文件
1010
project.addSourceFilesAtPaths(`./${build}/components/*/*.d.ts`);
@@ -23,7 +23,7 @@ for (let index = 0; index < sourceFiles.length; index++) {
2323
if (getExoports.has("default") && module) {
2424
const o = module.getInterface("GlobalComponents")!.getChildren()[3];
2525
const name = o.getFirstChild()?.getChildren()[0].getText();
26-
text += " ";
26+
text += "\t".repeat(2);
2727
text += `${name}: typeof import("licht-ui/components/${baseName}")["default"];\n`;
2828
}
2929
}
@@ -32,7 +32,7 @@ const code = `
3232
// 自动生成类型文件 ${new Date().toLocaleString()}
3333
declare module "vue" {
3434
export interface GlobalComponents {
35-
${text}
35+
${text.replace(/\n$/, "")}
3636
}
3737
}
3838
export {};

0 commit comments

Comments
 (0)