Skip to content

Commit 94b378a

Browse files
committed
feat: Add a new package for creating component template
1 parent c93e3f4 commit 94b378a

File tree

9 files changed

+1523
-4
lines changed

9 files changed

+1523
-4
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
"lint": "eslint . --cache",
1313
"lint:fix": "eslint . --cache --fix",
1414
"bumpp": "bumpp -r",
15+
"nvc": "nextui-vue-create",
1516
"pub:view": "pnpm publish -r --dry-run --registry=https://registry.npmjs.org/"
1617
},
1718
"devDependencies": {
1819
"@antfu/eslint-config": "^2.24.1",
1920
"bumpp": "^9.10.0",
2021
"eslint": "9.5.0",
22+
"nextui-vue-create": "workspace: *",
2123
"pnpm": "^9.8.0",
2224
"rimraf": "^6.0.1"
2325
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
import './dist/index.mjs'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "nextui-vue-create",
3+
"type": "module",
4+
"private": true,
5+
"exports": {
6+
".": "./dist/index.mjs"
7+
},
8+
"main": "./dist/index.mjs",
9+
"bin": {
10+
"nextui-vue-create": "index.js"
11+
},
12+
"files": ["template"],
13+
"scripts": {
14+
"dev": "unbuild --stub",
15+
"build": "unbuild"
16+
},
17+
"devDependencies": {
18+
"@types/minimist": "^1.2.5",
19+
"@types/node": "^22.10.5",
20+
"@types/prompts": "^2.4.9",
21+
"minimist": "^1.2.8",
22+
"prompts": "^2.4.2",
23+
"unbuild": "^3.3.1"
24+
}
25+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import process from 'node:process'
2+
import fs from 'node:fs/promises'
3+
import fsSync from 'node:fs'
4+
import path from 'node:path'
5+
import { fileURLToPath } from 'node:url'
6+
import minimist from 'minimist'
7+
import prompts, { type PromptObject } from 'prompts'
8+
9+
const cwd = process.cwd()
10+
11+
const questions: PromptObject[] = [
12+
{
13+
type: 'text',
14+
name: 'name',
15+
message: '为哪个组件创建模版?',
16+
validate: input => /^\w+$/.test(input) ? true : `至少输入一个字符, 且只能是字母、数字、下划线`,
17+
},
18+
{
19+
type: (prev) => {
20+
const targetDir = path.join(cwd, 'packages/components', prev)
21+
return !isEmptyDir(targetDir) ? 'confirm' : false
22+
},
23+
name: 'override',
24+
message: prev => `${prev}已存在, 要覆盖它吗?`,
25+
},
26+
]
27+
28+
const response = (await prompts(questions)) as { name: string, override?: boolean }
29+
30+
// const arv = minimist<{
31+
// name: string
32+
// }>(process.argv.splice(2), {
33+
// alias: {
34+
// name: 'n',
35+
// },
36+
// })
37+
38+
const templateDir = path.join(fileURLToPath(import.meta.url), '../..', 'template')
39+
const targetDir = path.join(cwd, 'packages/components', response.name)
40+
41+
const renamedFiles = {
42+
_gitignore: '.gitignore',
43+
}
44+
45+
function isEmptyDir(path) {
46+
if (!fsSync.existsSync(path)) {
47+
return true
48+
}
49+
50+
const files = fsSync.readdirSync(path)
51+
return !files || files?.length === 0
52+
}
53+
54+
async function copyDir(source: string, target: string) {
55+
await fs.mkdir(target, { recursive: true })
56+
57+
const files = await fs.readdir(source)
58+
for (const file of files) {
59+
const sourcePath = path.join(source, file)
60+
const targetPath = path.join(target, renamedFiles[file] || file)
61+
const stat = await fs.stat(sourcePath)
62+
63+
if (stat.isDirectory()) {
64+
copyDir(sourcePath, targetPath)
65+
}
66+
else {
67+
await fs.copyFile(sourcePath, targetPath)
68+
}
69+
}
70+
}
71+
72+
export async function main() {
73+
if (!Object.hasOwn(response, 'override') || response.override) {
74+
await copyDir(templateDir, targetDir)
75+
console.log(`创建成功`)
76+
}
77+
else {
78+
console.log('无事发生, 退出')
79+
}
80+
}
81+
82+
main()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
!.vscode/settings.json
19+
.idea
20+
.DS_Store
21+
*.suo
22+
*.ntvs*
23+
*.njsproj
24+
*.sln
25+
*.sw?
26+
docs/.vitepress/dist
27+
docs/.vitepress/cache

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

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"rootDir": ".",
4+
"module": "NodeNext",
5+
"moduleResolution": "nodenext"
6+
},
7+
"include": ["**/*.ts"]
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineBuildConfig } from 'unbuild'
2+
3+
export default defineBuildConfig({
4+
entries: ['./src/index.ts'],
5+
outDir: 'dist',
6+
failOnWarn: false,
7+
})

0 commit comments

Comments
 (0)