create-nserve 1.0.4 → 1.0.8

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 CHANGED
@@ -1,14 +1,10 @@
1
- # @aicblock/nserve
1
+ # create-nserve 1.0.8
2
2
 
3
3
  ![npm](https://img.shields.io/npm/v/@aicblock/nserve)
4
4
  ![npm](https://img.shields.io/npm/dm/@aicblock/nserve)
5
5
  ![NPM](https://img.shields.io/npm/l/@aicblock/nserve)
6
6
 
7
- #### Description
8
-
9
- 🛠️ Description NodeJS Server-side development CLI
10
-
11
- #### 快速搭建第一个 Nserve 项目
7
+ ## 创建项目
12
8
 
13
9
  #### 使用 NPM:
14
10
 
@@ -25,5 +21,16 @@ $ yarn create nserve@latest
25
21
  #### 使用 PNPM:
26
22
 
27
23
  ```sh
28
- $ pnpm create nserve@latest
24
+ $ pnpm create nserve@latest
25
+ ```
26
+
27
+ 选择 `Blank template` 或 `MySQL ORM`,输入新项目目录名,然后执行:
28
+
29
+ ```bash
30
+ cd <项目目录>
31
+ npm install
32
+ npm run gen
33
+ npm run dev
29
34
  ```
35
+
36
+ MySQL 模板还需要先在 `nserve.config.ts` 中填写 `yamlConfig.MYSQL_CONF` 并准备数据库。模板安装的 `@aicblock/nserve` 版本与脚手架版本一致(当前为 1.0.8)。
package/bin/index.js CHANGED
@@ -26,19 +26,8 @@ async function init () {
26
26
  initial: 0
27
27
  }
28
28
  ])
29
- // 2. 定义路径
30
- const templateDir = path.join(__dirname, '../template',response.template)
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
- init().catch(console.error)
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
  {
2
2
  "name": "create-nserve",
3
- "version": "1.0.4",
3
+ "version": "1.0.8",
4
4
  "main": "bin/index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -1,17 +1,9 @@
1
- # @aicblock/nserve
1
+ # NServe 1.0.8 空白模板
2
2
 
3
- ![npm](https://img.shields.io/npm/v/@aicblock/nserve)
4
- ![npm](https://img.shields.io/npm/dm/@aicblock/nserve)
5
- ![NPM](https://img.shields.io/npm/l/@aicblock/nserve)
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
3
+ ```bash
4
+ npm install
5
+ npm run gen
6
+ npm run dev
17
7
  ```
8
+
9
+ 服务默认监听 `http://localhost:3000`,示例接口为 `/api/user/v1/list`,健康检查为 `/api/health`。可在 `nserve.config.ts` 的 `yamlConfig.RUN_PORT` 修改端口;修改路由描述文件后再次运行 `npm run gen`。生产构建使用 `npm run build`。
@@ -7,7 +7,9 @@
7
7
  "build": "nserve build",
8
8
  "gen": "nserve gen"
9
9
  },
10
- "dependencies": {},
10
+ "dependencies": {
11
+ "express": "5.2.1"
12
+ },
11
13
  "devDependencies": {
12
14
  "@aicblock/nserve": "{{nserveVersion}}",
13
15
  "@types/express": "^5.0.6",
@@ -40,7 +40,7 @@ const run = async () => {
40
40
  message: '接口不存在'
41
41
  })
42
42
  })
43
- const RUN_PORT = Number(process.env.RUN_PORT || 3000)
43
+ const RUN_PORT = Number(process.env.RUN_PORT || global.RUN_PORT || 3000)
44
44
  // 启动服务器
45
45
  app.listen(RUN_PORT, () => {
46
46
  console.log(`🚀 服务器运行在端口 ${RUN_PORT}`)
@@ -60,6 +60,7 @@ const run = async () => {
60
60
  })
61
61
  } catch (error) {
62
62
  console.error('初始化数据失败:', error)
63
+ process.exitCode = 1
63
64
  }
64
65
  }
65
66
 
@@ -0,0 +1,5 @@
1
+ declare global {
2
+ var RUN_PORT: number
3
+ }
4
+
5
+ export {}
@@ -1,17 +1,11 @@
1
- # @aicblock/nserve
1
+ # NServe 1.0.8 MySQL 模板
2
2
 
3
- ![npm](https://img.shields.io/npm/v/@aicblock/nserve)
4
- ![npm](https://img.shields.io/npm/dm/@aicblock/nserve)
5
- ![NPM](https://img.shields.io/npm/l/@aicblock/nserve)
3
+ 先在 `nserve.config.ts` 中填写 `yamlConfig.MYSQL_CONF` 的 `host`、`user`、`password`、`database`(需要时填写 `port`),并准备可连接的 MySQL 数据库。启动时会初始化连接,并根据 `nserve-config/model/*.sql` 创建表。
6
4
 
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
5
+ ```bash
6
+ npm install
7
+ npm run gen
8
+ npm run dev
17
9
  ```
10
+
11
+ 服务默认监听 `http://localhost:3000`,健康检查为 `/api/health`。可在 `nserve.config.ts` 的 `yamlConfig.RUN_PORT` 修改端口;修改 SQL 或路由描述文件后再次运行 `npm run gen`。生产构建使用 `npm run build`。
@@ -7,7 +7,10 @@
7
7
  "build": "nserve build",
8
8
  "gen": "nserve gen"
9
9
  },
10
- "dependencies": {},
10
+ "dependencies": {
11
+ "@aicblock/esql": "2.0.2",
12
+ "express": "5.2.1"
13
+ },
11
14
  "devDependencies": {
12
15
  "@aicblock/nserve": "{{nserveVersion}}",
13
16
  "@types/express": "^5.0.6",
@@ -71,7 +71,7 @@ const run = async () => {
71
71
  message: '接口不存在'
72
72
  })
73
73
  })
74
- const RUN_PORT = Number(process.env.RUN_PORT || 3000)
74
+ const RUN_PORT = Number(process.env.RUN_PORT || global.RUN_PORT || 3000)
75
75
  // 启动服务器
76
76
  app.listen(RUN_PORT, () => {
77
77
  console.log(`🚀 服务器运行在端口 ${RUN_PORT}`)
@@ -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
 
@@ -1,6 +1,7 @@
1
1
  /* eslint-disable no-var */
2
2
  import esql from '@aicblock/esql'
3
3
  declare global {
4
+ var RUN_PORT: number
4
5
  /**
5
6
  * mysql 服务
6
7
  */