create-jnrs-template-vue 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/create.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { fileURLToPath } from 'url'
|
|
2
2
|
import { dirname, join, relative } from 'path'
|
|
3
|
-
import { promises as fs } from 'fs'
|
|
3
|
+
import { promises as fs, existsSync } from 'fs' // ✅ 同时导入 promises 和 existsSync
|
|
4
4
|
import { execSync, spawnSync } from 'child_process'
|
|
5
5
|
import minimist from 'minimist'
|
|
6
6
|
import prompts from 'prompts'
|
|
@@ -10,7 +10,6 @@ const __dirname = dirname(__filename)
|
|
|
10
10
|
|
|
11
11
|
// 检测系统中可用的包管理器
|
|
12
12
|
function detectPackageManager() {
|
|
13
|
-
// 按优先级:pnpm > yarn > npm
|
|
14
13
|
try {
|
|
15
14
|
const pnpmVersion = spawnSync('pnpm', ['--version'], { stdio: 'ignore' })
|
|
16
15
|
if (pnpmVersion.status === 0) return 'pnpm'
|
|
@@ -24,7 +23,6 @@ function detectPackageManager() {
|
|
|
24
23
|
return 'npm'
|
|
25
24
|
}
|
|
26
25
|
|
|
27
|
-
// 获取安装命令
|
|
28
26
|
function getInstallCommand(packageManager) {
|
|
29
27
|
switch (packageManager) {
|
|
30
28
|
case 'pnpm':
|
|
@@ -36,7 +34,6 @@ function getInstallCommand(packageManager) {
|
|
|
36
34
|
}
|
|
37
35
|
}
|
|
38
36
|
|
|
39
|
-
// 获取运行命令(用于提示)
|
|
40
37
|
function getRunCommand(packageManager, script) {
|
|
41
38
|
switch (packageManager) {
|
|
42
39
|
case 'pnpm':
|
|
@@ -66,6 +63,7 @@ async function main() {
|
|
|
66
63
|
const argv = minimist(process.argv.slice(2), { string: ['_'] })
|
|
67
64
|
let targetDir = argv._[0]
|
|
68
65
|
|
|
66
|
+
// 交互式输入项目名
|
|
69
67
|
if (!targetDir) {
|
|
70
68
|
const { name } = await prompts({
|
|
71
69
|
type: 'text',
|
|
@@ -74,7 +72,7 @@ async function main() {
|
|
|
74
72
|
initial: 'my-vue-app',
|
|
75
73
|
validate: (input) => {
|
|
76
74
|
if (!input) return 'Project name is required.'
|
|
77
|
-
if (
|
|
75
|
+
if (existsSync(input)) return 'Directory already exists.' // ✅ 使用 existsSync
|
|
78
76
|
if (!isValidPackageName(input)) {
|
|
79
77
|
return 'Invalid package name. Use lowercase, hyphens, and no special characters.'
|
|
80
78
|
}
|
|
@@ -84,42 +82,40 @@ async function main() {
|
|
|
84
82
|
if (!name) process.exit(1)
|
|
85
83
|
targetDir = name
|
|
86
84
|
} else {
|
|
85
|
+
// 非交互模式:校验包名和目录
|
|
87
86
|
if (!isValidPackageName(targetDir)) {
|
|
88
87
|
console.error('❌ Invalid project name:', targetDir)
|
|
89
88
|
console.error(' Package name must be valid (lowercase, no spaces, etc.)')
|
|
90
89
|
process.exit(1)
|
|
91
90
|
}
|
|
91
|
+
if (existsSync(join(process.cwd(), targetDir))) {
|
|
92
|
+
// ✅ 同步检查
|
|
93
|
+
console.error(`❌ Directory "${targetDir}" already exists.`)
|
|
94
|
+
process.exit(1)
|
|
95
|
+
}
|
|
92
96
|
}
|
|
93
97
|
|
|
94
98
|
const root = join(process.cwd(), targetDir.trim())
|
|
95
99
|
|
|
96
|
-
try {
|
|
97
|
-
await fs.access(root)
|
|
98
|
-
console.error(`❌ Directory "${targetDir}" already exists.`)
|
|
99
|
-
process.exit(1)
|
|
100
|
-
} catch {}
|
|
101
|
-
|
|
102
100
|
// 复制模板
|
|
103
101
|
const templateDir = join(__dirname, '..', 'jnrs-template-vue')
|
|
104
102
|
await fs.cp(templateDir, root, { recursive: true })
|
|
105
103
|
|
|
106
|
-
// 更新 package.json
|
|
104
|
+
// 更新 package.json
|
|
107
105
|
const pkgPath = join(root, 'package.json')
|
|
108
106
|
const pkg = JSON.parse(await fs.readFile(pkgPath, 'utf8'))
|
|
109
107
|
pkg.name = toValidPackageName(targetDir)
|
|
110
108
|
await fs.writeFile(pkgPath, JSON.stringify(pkg, null, 2))
|
|
111
109
|
|
|
112
|
-
//
|
|
113
|
-
|
|
114
|
-
await fs.writeFile(npmrcPath, `# Config for pnpm\nshamefully-hoist=true\n`)
|
|
110
|
+
// 写入 .npmrc(适配 pnpm)
|
|
111
|
+
await fs.writeFile(join(root, '.npmrc'), '# Config for pnpm\nshamefully-hoist=true\n')
|
|
115
112
|
|
|
116
|
-
//
|
|
113
|
+
// 安装依赖
|
|
117
114
|
const packageManager = detectPackageManager()
|
|
118
115
|
console.log(`\n📦 Detected package manager: ${packageManager}`)
|
|
119
116
|
console.log(`Installing dependencies with ${packageManager}...\n`)
|
|
120
117
|
|
|
121
118
|
const [cmd, ...args] = getInstallCommand(packageManager)
|
|
122
|
-
|
|
123
119
|
try {
|
|
124
120
|
execSync(`${cmd} ${args.join(' ')}`, {
|
|
125
121
|
cwd: root,
|
|
@@ -128,12 +124,12 @@ async function main() {
|
|
|
128
124
|
})
|
|
129
125
|
} catch (e) {
|
|
130
126
|
console.error(
|
|
131
|
-
`\n⚠️ Failed to install dependencies. Run manually:\n ${cmd} ${args.join(' ')}`
|
|
127
|
+
`\n⚠️ Failed to install dependencies. Run manually:\n cd ${relative(process.cwd(), root)} && ${cmd} ${args.join(' ')}`
|
|
132
128
|
)
|
|
133
129
|
process.exit(1)
|
|
134
130
|
}
|
|
135
131
|
|
|
136
|
-
//
|
|
132
|
+
// 成功提示
|
|
137
133
|
const relativePath = relative(process.cwd(), root)
|
|
138
134
|
const devCmd = getRunCommand(packageManager, 'dev')
|
|
139
135
|
|
|
@@ -144,5 +140,6 @@ async function main() {
|
|
|
144
140
|
|
|
145
141
|
main().catch((err) => {
|
|
146
142
|
console.error(`\n🚨 Error: ${err.message}`)
|
|
143
|
+
console.trace(err) // 可选:开发时显示堆栈
|
|
147
144
|
process.exit(1)
|
|
148
145
|
})
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
export {};
|
|
3
3
|
|
|
4
4
|
; declare global {
|
|
5
|
+
var __VLS_PROPS_FALLBACK: Record<string, unknown>;
|
|
6
|
+
|
|
5
7
|
const __VLS_directiveBindingRestFields: { instance: null, oldValue: null, modifiers: any, dir: any };
|
|
6
8
|
const __VLS_unref: typeof import('vue').unref;
|
|
7
9
|
const __VLS_placeholder: any;
|
|
@@ -35,7 +37,7 @@ export {};
|
|
|
35
37
|
attrs?: any;
|
|
36
38
|
slots?: T extends { $slots: infer Slots } ? Slots : Record<string, any>;
|
|
37
39
|
emit?: T extends { $emit: infer Emit } ? Emit : {};
|
|
38
|
-
props?:
|
|
40
|
+
props?: typeof props;
|
|
39
41
|
expose?: (exposed: T) => void;
|
|
40
42
|
};
|
|
41
43
|
};
|