create-pangza 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/README.md +2 -1
- package/lib/modules.js +32 -42
- package/lib/scaffold.js +2 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,7 +10,8 @@ npx create-pangza init my-app
|
|
|
10
10
|
npm create pangza@latest -- init my-app
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
交互勾选模块:`orm` / `auth` / `redis` / `log` / `schedule` / `encrypt` / `validate`。
|
|
13
|
+
交互勾选模块:`orm` / `auth` / `redis` / `log` / `schedule` / `encrypt` / `validate`。
|
|
14
|
+
勾选 `orm` / `redis` 时会写入 `EnableOrm()` / `EnableRedis()`;未勾选则启动不连库、不因缺配置报错。不生成 `.gitkeep`。
|
|
14
15
|
|
|
15
16
|
```bash
|
|
16
17
|
npx create-pangza init my-app --modules=orm,auth,redis --db=mysql
|
package/lib/modules.js
CHANGED
|
@@ -81,7 +81,7 @@ const DB_ENV = {
|
|
|
81
81
|
/** @param {string[]} moduleIds @param {string} [dbType] */
|
|
82
82
|
function resolveDependencies(moduleIds, dbType = "mysql") {
|
|
83
83
|
const deps = {
|
|
84
|
-
"@pangza/express": "^1.0.
|
|
84
|
+
"@pangza/express": "^1.0.1",
|
|
85
85
|
cors: "^2.8.5",
|
|
86
86
|
express: "^5.2.1",
|
|
87
87
|
inversify: "^8.2.3",
|
|
@@ -143,35 +143,25 @@ function resolveDirs(moduleIds) {
|
|
|
143
143
|
}
|
|
144
144
|
|
|
145
145
|
/**
|
|
146
|
-
* 生成空项目入口:只起 HTTP
|
|
146
|
+
* 生成空项目入口:只起 HTTP;选了 redis/orm 才 Enable*,否则启动不连库。
|
|
147
147
|
* @param {string[]} moduleIds
|
|
148
148
|
*/
|
|
149
149
|
function generateMainTs(moduleIds) {
|
|
150
150
|
const set = new Set(moduleIds);
|
|
151
|
+
const named = ["Application", "Logger"];
|
|
152
|
+
if (set.has("orm")) named.push("EnableOrm");
|
|
153
|
+
if (set.has("redis") || set.has("auth")) named.push("EnableRedis");
|
|
154
|
+
if (set.has("auth")) named.push("Auth", "Redis");
|
|
155
|
+
if (set.has("log")) named.push("Log");
|
|
156
|
+
|
|
151
157
|
const imports = [
|
|
152
158
|
'import "reflect-metadata";',
|
|
153
159
|
'import cors from "cors";',
|
|
154
160
|
'import { json } from "express";',
|
|
155
|
-
|
|
161
|
+
`import { ${named.join(", ")} } from "@pangza/express";`,
|
|
156
162
|
];
|
|
157
163
|
|
|
158
164
|
const notes = [];
|
|
159
|
-
if (set.has("orm")) {
|
|
160
|
-
notes.push(
|
|
161
|
-
"// ORM: 配置 .env 中的数据库变量后即可使用 @Mapper / QueryWrapper",
|
|
162
|
-
);
|
|
163
|
-
}
|
|
164
|
-
if (set.has("redis") || set.has("auth")) {
|
|
165
|
-
notes.push("// Redis: 配置 REDIS_* 后由框架自动连接(见文档)");
|
|
166
|
-
}
|
|
167
|
-
if (set.has("auth")) {
|
|
168
|
-
notes.push(
|
|
169
|
-
'// Auth: Application.registry(Auth({ ignore: ["/api/pub/**"] }))',
|
|
170
|
-
);
|
|
171
|
-
}
|
|
172
|
-
if (set.has("log")) {
|
|
173
|
-
notes.push("// Log: configureLog({ writer }) 后配合 @Log 装饰器");
|
|
174
|
-
}
|
|
175
165
|
if (set.has("schedule")) {
|
|
176
166
|
notes.push("// Schedule: 在 src/schedule 编写任务并由扫描加载");
|
|
177
167
|
}
|
|
@@ -182,34 +172,35 @@ function generateMainTs(moduleIds) {
|
|
|
182
172
|
notes.push("// Validate: 在 Controller 参数上使用校验装饰器");
|
|
183
173
|
}
|
|
184
174
|
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
"",
|
|
188
|
-
...(notes.length ? [...notes, ""] : []),
|
|
189
|
-
"Application.registry(Logger({ level: \"info\" }))",
|
|
175
|
+
const chain = [
|
|
176
|
+
'Application.registry(Logger({ level: "info" }))',
|
|
190
177
|
" .registry(cors())",
|
|
191
178
|
" .registry(json())",
|
|
192
|
-
' .scan("src")',
|
|
193
|
-
" .start();",
|
|
194
|
-
"",
|
|
195
179
|
];
|
|
196
|
-
return body.join("\n");
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function generateGitkeepReadme(moduleIds) {
|
|
200
|
-
const set = new Set(moduleIds);
|
|
201
|
-
const files = {
|
|
202
|
-
"src/controller/.gitkeep": "",
|
|
203
|
-
};
|
|
204
180
|
if (set.has("orm")) {
|
|
205
|
-
|
|
206
|
-
files["src/mapper/.gitkeep"] = "";
|
|
207
|
-
files["src/service/.gitkeep"] = "";
|
|
181
|
+
chain.push(" .registry(EnableOrm())");
|
|
208
182
|
}
|
|
209
|
-
if (set.has("
|
|
210
|
-
|
|
183
|
+
if (set.has("redis") || set.has("auth")) {
|
|
184
|
+
chain.push(" .registry(EnableRedis())");
|
|
185
|
+
}
|
|
186
|
+
if (set.has("auth")) {
|
|
187
|
+
chain.push(
|
|
188
|
+
' .registry(Auth({ ignore: ["/api/pub/**"], redis: Redis }))',
|
|
189
|
+
);
|
|
211
190
|
}
|
|
212
|
-
|
|
191
|
+
if (set.has("log")) {
|
|
192
|
+
chain.push(" .registry(Log())");
|
|
193
|
+
}
|
|
194
|
+
chain.push(' .scan("src")');
|
|
195
|
+
chain.push(" .start();");
|
|
196
|
+
|
|
197
|
+
return [
|
|
198
|
+
...imports,
|
|
199
|
+
"",
|
|
200
|
+
...(notes.length ? [...notes, ""] : []),
|
|
201
|
+
...chain,
|
|
202
|
+
"",
|
|
203
|
+
].join("\n");
|
|
213
204
|
}
|
|
214
205
|
|
|
215
206
|
module.exports = {
|
|
@@ -219,5 +210,4 @@ module.exports = {
|
|
|
219
210
|
resolveEnvLines,
|
|
220
211
|
resolveDirs,
|
|
221
212
|
generateMainTs,
|
|
222
|
-
generateGitkeepReadme,
|
|
223
213
|
};
|
package/lib/scaffold.js
CHANGED
|
@@ -7,7 +7,6 @@ const {
|
|
|
7
7
|
resolveEnvLines,
|
|
8
8
|
resolveDirs,
|
|
9
9
|
generateMainTs,
|
|
10
|
-
generateGitkeepReadme,
|
|
11
10
|
} = require("./modules");
|
|
12
11
|
|
|
13
12
|
const ROOT = path.join(__dirname, "..");
|
|
@@ -45,7 +44,6 @@ function scaffold(opts) {
|
|
|
45
44
|
|
|
46
45
|
ensureDir(targetDir);
|
|
47
46
|
|
|
48
|
-
// base static files
|
|
49
47
|
copyFile(
|
|
50
48
|
path.join(BASE_TEMPLATE, "tsconfig.json"),
|
|
51
49
|
path.join(targetDir, "tsconfig.json"),
|
|
@@ -90,11 +88,6 @@ function scaffold(opts) {
|
|
|
90
88
|
);
|
|
91
89
|
writeFile(path.join(targetDir, "src", "main.ts"), generateMainTs(modules));
|
|
92
90
|
|
|
93
|
-
const extras = generateGitkeepReadme(modules);
|
|
94
|
-
for (const [rel, content] of Object.entries(extras)) {
|
|
95
|
-
writeFile(path.join(targetDir, rel), content);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
91
|
writeFile(
|
|
99
92
|
path.join(targetDir, "README.md"),
|
|
100
93
|
`# ${projectName}
|
|
@@ -113,6 +106,8 @@ cp .env.example .env
|
|
|
113
106
|
npm install
|
|
114
107
|
npm run dev
|
|
115
108
|
\`\`\`
|
|
109
|
+
|
|
110
|
+
需要 Redis / 数据库时,请在入口 \`registry(EnableRedis())\` / \`registry(EnableOrm())\`(脚手架勾选对应模块时已自动写入)。
|
|
116
111
|
`,
|
|
117
112
|
);
|
|
118
113
|
}
|