create-nserve 1.0.0 → 1.0.1
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 +19 -6
- package/package.json +2 -2
- package/template/blank/nserve.config.ts +7 -0
- package/template/blank/src/app.ts +52 -0
- package/template/mysql/README.md +17 -0
- package/template/mysql/nserve-config/router/user.ts +13 -0
- package/template/{nserve.config.ts → mysql/nserve.config.ts} +1 -0
- package/template/mysql/package.json +16 -0
- package/template/{src → mysql/src}/app.ts +6 -4
- package/template/mysql/src/handler/router.ts +1 -0
- package/template/mysql/src/model/index.ts +1 -0
- package/template/mysql/tsconfig.json +24 -0
- /package/template/{README.md → blank/README.md} +0 -0
- /package/template/{nserve-config → blank/nserve-config}/router/user.ts +0 -0
- /package/template/{package.json → blank/package.json} +0 -0
- /package/template/{src → blank/src}/handler/router.ts +0 -0
- /package/template/{tsconfig.json → blank/tsconfig.json} +0 -0
- /package/template/{nserve-config → mysql/nserve-config}/model/t_user.sql +0 -0
- /package/template/{src → mysql/src}/types/index.d.ts +0 -0
package/bin/index.js
CHANGED
|
@@ -5,19 +5,29 @@ const path = require('path')
|
|
|
5
5
|
const prompts = require('prompts')
|
|
6
6
|
const { version: nserveVersion } = require('../package.json')
|
|
7
7
|
|
|
8
|
-
async function init() {
|
|
8
|
+
async function init () {
|
|
9
9
|
// 1. 获取用户输入
|
|
10
10
|
const response = await prompts([
|
|
11
11
|
{
|
|
12
12
|
type: 'text',
|
|
13
13
|
name: 'projectName',
|
|
14
|
-
message: '
|
|
15
|
-
initial: '
|
|
14
|
+
message: 'Project name?',
|
|
15
|
+
initial: 'nserve-project',
|
|
16
|
+
validate: (name) => /^[a-zA-Z0-9-]+$/.test(name) || '项目名只能包含字母、数字和短横线'
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
type: 'select',
|
|
20
|
+
name: 'template',
|
|
21
|
+
message: 'Pick a template type?',
|
|
22
|
+
choices: [
|
|
23
|
+
{ title: 'MySQL ORM', description: 'Generate MySQL ORM files', value: 'mysql' },
|
|
24
|
+
{ title: 'Blank template', description: 'Blank template', value: 'blank' },
|
|
25
|
+
],
|
|
26
|
+
initial: 0
|
|
16
27
|
}
|
|
17
28
|
])
|
|
18
|
-
|
|
19
29
|
// 2. 定义路径
|
|
20
|
-
const templateDir = path.join(__dirname,
|
|
30
|
+
const templateDir = path.join(__dirname, '../template',response.template)
|
|
21
31
|
const targetDir = path.join(process.cwd(), response.projectName)
|
|
22
32
|
|
|
23
33
|
// 3. 复制模板文件
|
|
@@ -30,7 +40,10 @@ async function init() {
|
|
|
30
40
|
pkg.devDependencies['@aicblock/nserve'] = nserveVersion
|
|
31
41
|
await fs.writeJson(pkgPath, pkg, { spaces: 2 })
|
|
32
42
|
|
|
33
|
-
console.log(
|
|
43
|
+
console.log(`项目 ${response.projectName} 创建成功! 可执行以下命令:`)
|
|
44
|
+
console.log(` cd ${response.projectName}`)
|
|
45
|
+
console.log(' npm install')
|
|
46
|
+
console.log(' npm run dev')
|
|
34
47
|
}
|
|
35
48
|
|
|
36
49
|
init().catch(console.error)
|
package/package.json
CHANGED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import express from 'express'
|
|
2
|
+
import { LoadConfig } from '@aicblock/nserve/lib/tools'
|
|
3
|
+
import routers from '@/handler/router'
|
|
4
|
+
import { join } from 'path'
|
|
5
|
+
const app = express()
|
|
6
|
+
|
|
7
|
+
// 启动应用
|
|
8
|
+
const run = async () => {
|
|
9
|
+
try {
|
|
10
|
+
LoadConfig(join(__dirname, 'config.yaml'))
|
|
11
|
+
routers.length > 0 && app.use('/api', routers)
|
|
12
|
+
|
|
13
|
+
// 健康检查
|
|
14
|
+
app.get('/api/health', (_, res) => {
|
|
15
|
+
res.json({
|
|
16
|
+
status: 'OK',
|
|
17
|
+
timestamp: new Date().toISOString(),
|
|
18
|
+
version: process.env.VERSION
|
|
19
|
+
})
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
// 404处理
|
|
23
|
+
app.use(/.*/, (_, res) => {
|
|
24
|
+
res.status(404).json({
|
|
25
|
+
success: false,
|
|
26
|
+
message: '接口不存在'
|
|
27
|
+
})
|
|
28
|
+
})
|
|
29
|
+
const RUN_PORT = Number(process.env.RUN_PORT || 3000)
|
|
30
|
+
// 启动服务器
|
|
31
|
+
app.listen(RUN_PORT, () => {
|
|
32
|
+
console.log(`🚀 服务器运行在端口 ${RUN_PORT}`)
|
|
33
|
+
console.log(`📊 环境: ${process.env.NODE_ENV || 'development'}`)
|
|
34
|
+
console.log(`🔗 API地址: http://localhost:${RUN_PORT}/api`)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
// 优雅关闭
|
|
38
|
+
process.on('SIGTERM', () => {
|
|
39
|
+
console.log('收到SIGTERM信号,正在关闭服务器...')
|
|
40
|
+
process.exit(0)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
process.on('SIGINT', () => {
|
|
44
|
+
console.log('收到SIGINT信号,正在关闭服务器...')
|
|
45
|
+
process.exit(0)
|
|
46
|
+
})
|
|
47
|
+
} catch (error) {
|
|
48
|
+
console.error('初始化数据失败:', error)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
run()
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# @aicblock/nserve
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
#### Description
|
|
8
|
+
|
|
9
|
+
🛠️ Description NodeJS Server-side development CLI
|
|
10
|
+
|
|
11
|
+
#### Installation
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm i @aicblock/nserve@latest -g
|
|
15
|
+
# OR
|
|
16
|
+
npm i @aicblock/nserve@latest -D
|
|
17
|
+
```
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "{{name}}",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "NodeJS Server-side development CLI template",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"dev": "nserve serve",
|
|
7
|
+
"build": "nserve build",
|
|
8
|
+
"gen": "nserve gen"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@aicblock/nserve": "{{nserveVersion}}",
|
|
13
|
+
"@types/express": "^5.0.6",
|
|
14
|
+
"@types/node": "^24.0.4"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -3,6 +3,7 @@ import routers from './handler/router'
|
|
|
3
3
|
import emysql from '@dpapejs/emysql'
|
|
4
4
|
import { LoadConfig } from '@aicblock/nserve/lib/tools'
|
|
5
5
|
import { join } from 'path'
|
|
6
|
+
import models from '@/model'
|
|
6
7
|
const app = express()
|
|
7
8
|
|
|
8
9
|
/**
|
|
@@ -25,6 +26,7 @@ const initDatabase = async () => {
|
|
|
25
26
|
})
|
|
26
27
|
const connection = await mysql.pool?.getConnection()
|
|
27
28
|
connection?.release()
|
|
29
|
+
models.length > 0 && (await mysql.table.create(models))
|
|
28
30
|
} catch (error) {
|
|
29
31
|
console.error('❌ 数据库连接失败:', error)
|
|
30
32
|
return Promise.reject(error)
|
|
@@ -54,12 +56,12 @@ const run = async () => {
|
|
|
54
56
|
message: '接口不存在'
|
|
55
57
|
})
|
|
56
58
|
})
|
|
57
|
-
|
|
59
|
+
const RUN_PORT = Number(process.env.RUN_PORT || 3000)
|
|
58
60
|
// 启动服务器
|
|
59
|
-
app.listen(
|
|
60
|
-
console.log(`🚀 服务器运行在端口
|
|
61
|
+
app.listen(RUN_PORT, () => {
|
|
62
|
+
console.log(`🚀 服务器运行在端口 ${RUN_PORT}`)
|
|
61
63
|
console.log(`📊 环境: ${process.env.NODE_ENV || 'development'}`)
|
|
62
|
-
console.log(`🔗 API地址: http://localhost
|
|
64
|
+
console.log(`🔗 API地址: http://localhost:${RUN_PORT}/api`)
|
|
63
65
|
})
|
|
64
66
|
|
|
65
67
|
// 优雅关闭
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default []
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default []
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"module": "commonjs",
|
|
4
|
+
"target": "ES2015",
|
|
5
|
+
"outDir": "./lib",
|
|
6
|
+
"lib": ["esnext"],
|
|
7
|
+
"esModuleInterop": true,
|
|
8
|
+
"removeComments": false,
|
|
9
|
+
"noEmitOnError": true,
|
|
10
|
+
"isolatedModules": true,
|
|
11
|
+
"experimentalDecorators": true,
|
|
12
|
+
"emitDecoratorMetadata": true,
|
|
13
|
+
"strictNullChecks": true,
|
|
14
|
+
"strict": true,
|
|
15
|
+
"baseUrl": "./",
|
|
16
|
+
"skipLibCheck": true,
|
|
17
|
+
"paths": {
|
|
18
|
+
"@/*": ["src/*"]
|
|
19
|
+
},
|
|
20
|
+
"types": ["@types/node"]
|
|
21
|
+
},
|
|
22
|
+
"include": ["src/**/*"],
|
|
23
|
+
"exclude": ["node_modules"]
|
|
24
|
+
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|