create-nserve 1.0.0
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/README.md +29 -0
- package/bin/index.js +36 -0
- package/package.json +31 -0
- package/template/README.md +17 -0
- package/template/nserve-config/model/t_user.sql +14 -0
- package/template/nserve-config/router/user.ts +13 -0
- package/template/nserve.config.ts +12 -0
- package/template/package.json +16 -0
- package/template/src/app.ts +80 -0
- package/template/src/handler/router.ts +1 -0
- package/template/src/types/index.d.ts +17 -0
- package/template/tsconfig.json +24 -0
package/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# @aicblock/nserve
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
#### Description
|
|
8
|
+
|
|
9
|
+
🛠️ Description NodeJS Server-side development CLI
|
|
10
|
+
|
|
11
|
+
#### 快速搭建第一个 Nserve 项目
|
|
12
|
+
|
|
13
|
+
#### 使用 NPM:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
$ npm create nserve@latest
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
#### 使用 Yarn:
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
$ yarn create nserve@latest
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
#### 使用 PNPM:
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
$ pnpm create nserve@latest
|
|
29
|
+
```
|
package/bin/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const fs = require('fs-extra')
|
|
4
|
+
const path = require('path')
|
|
5
|
+
const prompts = require('prompts')
|
|
6
|
+
const { version: nserveVersion } = require('../package.json')
|
|
7
|
+
|
|
8
|
+
async function init() {
|
|
9
|
+
// 1. 获取用户输入
|
|
10
|
+
const response = await prompts([
|
|
11
|
+
{
|
|
12
|
+
type: 'text',
|
|
13
|
+
name: 'projectName',
|
|
14
|
+
message: '项目名称?',
|
|
15
|
+
initial: 'my-project'
|
|
16
|
+
}
|
|
17
|
+
])
|
|
18
|
+
|
|
19
|
+
// 2. 定义路径
|
|
20
|
+
const templateDir = path.join(__dirname, '../template')
|
|
21
|
+
const targetDir = path.join(process.cwd(), response.projectName)
|
|
22
|
+
|
|
23
|
+
// 3. 复制模板文件
|
|
24
|
+
await fs.copy(templateDir, targetDir)
|
|
25
|
+
|
|
26
|
+
// 4. 修改 package.json 中的项目名
|
|
27
|
+
const pkgPath = path.join(targetDir, 'package.json')
|
|
28
|
+
const pkg = require(pkgPath)
|
|
29
|
+
pkg.name = response.projectName
|
|
30
|
+
pkg.devDependencies['@aicblock/nserve'] = nserveVersion
|
|
31
|
+
await fs.writeJson(pkgPath, pkg, { spaces: 2 })
|
|
32
|
+
|
|
33
|
+
console.log('项目创建成功!')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
init().catch(console.error)
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "create-nserve",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "bin/index.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
7
|
+
},
|
|
8
|
+
"keywords": [
|
|
9
|
+
"nserve",
|
|
10
|
+
"create-app",
|
|
11
|
+
"aicblock"
|
|
12
|
+
],
|
|
13
|
+
"bin": {
|
|
14
|
+
"create-nserve": "./bin/index.js"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://gitee.com/aiclouddigit/nserve.git"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"bin",
|
|
22
|
+
"template"
|
|
23
|
+
],
|
|
24
|
+
"author": "jieyang;damon yang",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"description": "NodeJS Server-side development CLI template",
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"fs-extra": "^11.3.3",
|
|
29
|
+
"prompts": "^2.4.2"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -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,14 @@
|
|
|
1
|
+
CREATE TABLE `t_users` (
|
|
2
|
+
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键ID',
|
|
3
|
+
`unionid` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin NOT NULL COMMENT '第三方用户唯一识别码',
|
|
4
|
+
`email` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin NULL DEFAULT NULL COMMENT '邮箱地址',
|
|
5
|
+
`real_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin NOT NULL COMMENT '真实姓名',
|
|
6
|
+
`nickname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin NULL DEFAULT NULL COMMENT '昵称',
|
|
7
|
+
`username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin NULL DEFAULT NULL COMMENT '用户名',
|
|
8
|
+
`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin NULL DEFAULT NULL COMMENT '登陆密码',
|
|
9
|
+
`type` int NULL DEFAULT 2 COMMENT '用户类型',
|
|
10
|
+
`status` int NULL DEFAULT 1 COMMENT '用户状态',
|
|
11
|
+
`update_at` datetime NOT NULL COMMENT '更新时间',
|
|
12
|
+
PRIMARY KEY (`id`) USING BTREE,
|
|
13
|
+
UNIQUE INDEX `unionid`(`unionid` ASC) USING BTREE
|
|
14
|
+
) ENGINE = InnoDB AUTO_INCREMENT = 518060 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_bin ROW_FORMAT = Dynamic;
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import express from 'express'
|
|
2
|
+
import routers from './handler/router'
|
|
3
|
+
import emysql from '@dpapejs/emysql'
|
|
4
|
+
import { LoadConfig } from '@aicblock/nserve/lib/tools'
|
|
5
|
+
import { join } from 'path'
|
|
6
|
+
const app = express()
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 初始化mysql数据库
|
|
10
|
+
*/
|
|
11
|
+
const initDatabase = async () => {
|
|
12
|
+
// 判断数据库名是否为空
|
|
13
|
+
const { database, user, password, port, host } = global.MYSQL_CONF || {}
|
|
14
|
+
if (!database) {
|
|
15
|
+
throw Error('Failed to read the database configuration file')
|
|
16
|
+
}
|
|
17
|
+
try {
|
|
18
|
+
global.mysql = new emysql({
|
|
19
|
+
database,
|
|
20
|
+
user,
|
|
21
|
+
password,
|
|
22
|
+
port,
|
|
23
|
+
host,
|
|
24
|
+
log: 'debug'
|
|
25
|
+
})
|
|
26
|
+
const connection = await mysql.pool?.getConnection()
|
|
27
|
+
connection?.release()
|
|
28
|
+
} catch (error) {
|
|
29
|
+
console.error('❌ 数据库连接失败:', error)
|
|
30
|
+
return Promise.reject(error)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// 启动应用
|
|
35
|
+
const run = async () => {
|
|
36
|
+
try {
|
|
37
|
+
LoadConfig(join(__dirname, 'config.yaml'))
|
|
38
|
+
await initDatabase()
|
|
39
|
+
routers.length > 0 && app.use('/api', routers)
|
|
40
|
+
|
|
41
|
+
// 健康检查
|
|
42
|
+
app.get('/api/health', (_, res) => {
|
|
43
|
+
res.json({
|
|
44
|
+
status: 'OK',
|
|
45
|
+
timestamp: new Date().toISOString(),
|
|
46
|
+
version: process.env.VERSION
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
// 404处理
|
|
51
|
+
app.use(/.*/, (_, res) => {
|
|
52
|
+
res.status(404).json({
|
|
53
|
+
success: false,
|
|
54
|
+
message: '接口不存在'
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
// 启动服务器
|
|
59
|
+
app.listen(3000, () => {
|
|
60
|
+
console.log(`🚀 服务器运行在端口 3000`)
|
|
61
|
+
console.log(`📊 环境: ${process.env.NODE_ENV || 'development'}`)
|
|
62
|
+
console.log(`🔗 API地址: http://localhost:3000/api`)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
// 优雅关闭
|
|
66
|
+
process.on('SIGTERM', () => {
|
|
67
|
+
console.log('收到SIGTERM信号,正在关闭服务器...')
|
|
68
|
+
process.exit(0)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
process.on('SIGINT', () => {
|
|
72
|
+
console.log('收到SIGINT信号,正在关闭服务器...')
|
|
73
|
+
process.exit(0)
|
|
74
|
+
})
|
|
75
|
+
} catch (error) {
|
|
76
|
+
console.error('初始化数据失败:', error)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
run()
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default []
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/* eslint-disable no-var */
|
|
2
|
+
import emysql from '@dpapejs/emysql'
|
|
3
|
+
import eredis from '@/common/redis'
|
|
4
|
+
declare global {
|
|
5
|
+
/**
|
|
6
|
+
* mysql 服务
|
|
7
|
+
*/
|
|
8
|
+
var mysql: emysql
|
|
9
|
+
// 数据库配置信息
|
|
10
|
+
var MYSQL_CONF: {
|
|
11
|
+
password: string
|
|
12
|
+
user: string
|
|
13
|
+
database: string
|
|
14
|
+
port?: number
|
|
15
|
+
host?: string
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -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
|
+
}
|