|
| 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 | +} |
0 commit comments