Skip to content

Commit 303a5ac

Browse files
committed
feat(bin): Improve the bin file for creating new template component and the required template files
1 parent 9d96b79 commit 303a5ac

File tree

11 files changed

+292
-68
lines changed

11 files changed

+292
-68
lines changed

packages/bin/create-component-template/src/index.ts

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ const questions: PromptObject[] = [
2525
},
2626
]
2727

28-
const response = (await prompts(questions)) as { name: string, override?: boolean }
28+
const answers = (await prompts(questions)) as { name: string, override?: boolean }
29+
30+
const response = { ...answers, stdName: Array.from(answers.name)
31+
.map((char, index) => index === 0 ? char.toLocaleUpperCase() : char)
32+
.join('') }
2933

3034
// const arv = minimist<{
3135
// name: string
@@ -38,11 +42,21 @@ const response = (await prompts(questions)) as { name: string, override?: boolea
3842
const templateDir = path.join(fileURLToPath(import.meta.url), '../..', 'template')
3943
const targetDir = path.join(cwd, 'packages/components', response.name)
4044

45+
const placeholderSymbol = '$NAME'
46+
4147
const renamedFiles = {
4248
_gitignore: '.gitignore',
49+
Template_vue: `${response.stdName}.vue`,
4350
}
4451

45-
function isEmptyDir(path) {
52+
const placeholderFiles = {
53+
'package.json': (content: string) => content.replaceAll(placeholderSymbol, response.name),
54+
'index.ts': (content: string) => content.replaceAll(placeholderSymbol, response.stdName),
55+
} as const
56+
57+
type PlaceholderFile = keyof typeof placeholderFiles
58+
59+
function isEmptyDir(path: string) {
4660
if (!fsSync.existsSync(path)) {
4761
return true
4862
}
@@ -51,20 +65,45 @@ function isEmptyDir(path) {
5165
return !files || files?.length === 0
5266
}
5367

68+
async function getPlaceholderFileContent(filepath: string) {
69+
const file = Object.keys(placeholderFiles).find((placeholderFile) => {
70+
return path.resolve(templateDir, placeholderFile) === filepath
71+
})
72+
73+
if (!file) {
74+
return
75+
}
76+
77+
const fullFilepath = path.resolve(templateDir, file)
78+
const content = await fs.readFile(fullFilepath, { encoding: 'utf-8' })
79+
return placeholderFiles[file as PlaceholderFile]?.(content)
80+
}
81+
82+
async function copyFile(sourcePath: string, destPath: string) {
83+
const content = await getPlaceholderFileContent(sourcePath)
84+
if (content) {
85+
await fs.writeFile(destPath, content, { encoding: 'utf-8' })
86+
}
87+
else {
88+
await fs.copyFile(sourcePath, destPath)
89+
}
90+
}
91+
5492
async function copyDir(source: string, target: string) {
5593
await fs.mkdir(target, { recursive: true })
5694

5795
const files = await fs.readdir(source)
5896
for (const file of files) {
97+
const renamedFile = renamedFiles[file] instanceof Function ? renamedFiles[file]() : renamedFiles[file]
5998
const sourcePath = path.join(source, file)
60-
const targetPath = path.join(target, renamedFiles[file] || file)
99+
const targetPath = path.join(target, renamedFile || file)
61100
const stat = await fs.stat(sourcePath)
62101

63102
if (stat.isDirectory()) {
64-
copyDir(sourcePath, targetPath)
103+
await copyDir(sourcePath, targetPath)
65104
}
66105
else {
67-
await fs.copyFile(sourcePath, targetPath)
106+
await copyFile(sourcePath, targetPath)
68107
}
69108
}
70109
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 hotdogc1017
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as $NAME } from './src/$NAME.vue'
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "@vue-nextui/$NAME",
3+
"type": "module",
4+
"version": "0.0.0-beta.1",
5+
"author": {
6+
"name": "hotdog",
7+
"email": "hotdogc1017@gmail.com",
8+
"url": "https://github.com/hotdogc1017"
9+
},
10+
"publishConfig": {
11+
"access": "public",
12+
"registry": "https://registry.npmjs.org/"
13+
},
14+
"license": "MIT",
15+
"repository": {
16+
"url": "git+https://github.com/hotdogc1017/nextui-vue.git"
17+
},
18+
"bugs": {
19+
"url": "https://github.com/hotdogc1017/nextui-vue/issues"
20+
},
21+
"keywords": [
22+
"nextui",
23+
"vue",
24+
"ui framework",
25+
"tailwindcss",
26+
"$NAME component"
27+
],
28+
"exports": {
29+
".": "./dist/index.js"
30+
},
31+
"files": ["dist"],
32+
"scripts": {
33+
"build": "vite build"
34+
},
35+
"dependencies": {
36+
"@vue-nextui/shared": "workspace: *"
37+
},
38+
"devDependencies": {
39+
"@nextui-org/theme": "^2.2.9",
40+
"@types/node": "^22.10.5",
41+
"@vitejs/plugin-vue": "^5.0.5",
42+
"@vue/tsconfig": "^0.5.1",
43+
"vite": "^5.3.4",
44+
"vite-plugin-dts": "^4.5.0",
45+
"vue": "^3.5.0"
46+
}
47+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script lang="ts" setup>
2+
import { ref } from 'vue'
3+
</script>
4+
5+
<template>
6+
7+
</template>
8+
9+
<style scoped>
10+
11+
</style>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "@vue/tsconfig/tsconfig.dom.json",
3+
"compilerOptions": {
4+
"baseUrl": ".",
5+
},
6+
"files": ["./index.ts"],
7+
"include": ["./src/**/*", "./src/**/*.vue"]
8+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { resolve } from 'node:path'
2+
import { defineConfig } from 'vite'
3+
import vue from '@vitejs/plugin-vue'
4+
import dts from 'vite-plugin-dts'
5+
6+
export default defineConfig({
7+
plugins: [vue(), dts({ tsconfigPath: './tsconfig.json', rollupTypes: true })],
8+
build: {
9+
lib: {
10+
formats: ['es'],
11+
entry: resolve(__dirname, './index.ts'),
12+
fileName: 'index',
13+
},
14+
rollupOptions: {
15+
external: ['vue', '@nextui-org/theme'],
16+
},
17+
},
18+
})

packages/bin/create-component-template/unbuild.config.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/components/button/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"extends": "@vue/tsconfig/tsconfig.dom.json",
33
"compilerOptions": {
4-
"baseUrl": ".",
4+
"baseUrl": "."
55
},
66
"files": ["./index.ts"],
77
"include": ["./src/**/*", "./src/**/*.vue"]

0 commit comments

Comments
 (0)