create-nserve 1.0.3 → 1.0.5
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/index.js
CHANGED
|
@@ -26,19 +26,8 @@ async function init () {
|
|
|
26
26
|
initial: 0
|
|
27
27
|
}
|
|
28
28
|
])
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const targetDir = path.join(process.cwd(), response.projectName)
|
|
32
|
-
|
|
33
|
-
// 3. 复制模板文件
|
|
34
|
-
await fs.copy(templateDir, targetDir)
|
|
35
|
-
|
|
36
|
-
// 4. 修改 package.json 中的项目名
|
|
37
|
-
const pkgPath = path.join(targetDir, 'package.json')
|
|
38
|
-
const pkg = require(pkgPath)
|
|
39
|
-
pkg.name = response.projectName
|
|
40
|
-
pkg.devDependencies['@aicblock/nserve'] = nserveVersion
|
|
41
|
-
await fs.writeJson(pkgPath, pkg, { spaces: 2 })
|
|
29
|
+
if (!response.projectName || !response.template) return
|
|
30
|
+
await createProject(response.projectName, response.template)
|
|
42
31
|
|
|
43
32
|
console.log(`项目 ${response.projectName} 创建成功! 可执行以下命令:`)
|
|
44
33
|
console.log(` cd ${response.projectName}`)
|
|
@@ -46,4 +35,37 @@ async function init () {
|
|
|
46
35
|
console.log(' npm run dev')
|
|
47
36
|
}
|
|
48
37
|
|
|
49
|
-
|
|
38
|
+
async function createProject (projectName, template, cwd = process.cwd()) {
|
|
39
|
+
if (!/^[a-zA-Z0-9-]+$/.test(projectName)) {
|
|
40
|
+
throw new Error('项目名只能包含字母、数字和短横线')
|
|
41
|
+
}
|
|
42
|
+
if (!['mysql', 'blank'].includes(template)) {
|
|
43
|
+
throw new Error(`无效的项目模板: ${template}`)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const templateDir = path.join(__dirname, '../template', template)
|
|
47
|
+
const targetDir = path.join(cwd, projectName)
|
|
48
|
+
try {
|
|
49
|
+
await fs.mkdir(targetDir)
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (error.code === 'EEXIST') throw new Error(`目标目录已存在: ${targetDir}`)
|
|
52
|
+
throw error
|
|
53
|
+
}
|
|
54
|
+
await fs.copy(templateDir, targetDir, { overwrite: false, errorOnExist: true })
|
|
55
|
+
|
|
56
|
+
const pkgPath = path.join(targetDir, 'package.json')
|
|
57
|
+
const pkg = await fs.readJson(pkgPath)
|
|
58
|
+
pkg.name = projectName
|
|
59
|
+
pkg.devDependencies['@aicblock/nserve'] = nserveVersion
|
|
60
|
+
await fs.writeJson(pkgPath, pkg, { spaces: 2 })
|
|
61
|
+
return targetDir
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
module.exports = { createProject }
|
|
65
|
+
|
|
66
|
+
if (require.main === module) {
|
|
67
|
+
init().catch((error) => {
|
|
68
|
+
console.error(error)
|
|
69
|
+
process.exitCode = 1
|
|
70
|
+
})
|
|
71
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import express from 'express'
|
|
2
2
|
import routers from './handler/router'
|
|
3
|
-
import
|
|
3
|
+
import esql from '@aicblock/esql'
|
|
4
4
|
import { LoadConfig } from '@aicblock/nserve/lib/tools'
|
|
5
5
|
import { join } from 'path'
|
|
6
6
|
import models from '@/model'
|
|
@@ -16,7 +16,7 @@ const initDatabase = async () => {
|
|
|
16
16
|
throw Error('Failed to read the database configuration file')
|
|
17
17
|
}
|
|
18
18
|
try {
|
|
19
|
-
global.mysql = new
|
|
19
|
+
global.mysql = new esql({
|
|
20
20
|
connConfig: {
|
|
21
21
|
database,
|
|
22
22
|
user,
|
|
@@ -91,6 +91,7 @@ const run = async () => {
|
|
|
91
91
|
})
|
|
92
92
|
} catch (error) {
|
|
93
93
|
console.error('初始化数据失败:', error)
|
|
94
|
+
process.exitCode = 1
|
|
94
95
|
}
|
|
95
96
|
}
|
|
96
97
|
|