@zhin.js/scaffold-wizard 1.1.1 → 1.1.2
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 +5 -0
- package/lib/apply.d.ts +1 -1
- package/lib/apply.d.ts.map +1 -1
- package/lib/apply.js +7 -0
- package/lib/apply.js.map +1 -1
- package/lib/database-definitions.d.ts +28 -0
- package/lib/database-definitions.d.ts.map +1 -0
- package/lib/database-definitions.js +290 -0
- package/lib/database-definitions.js.map +1 -0
- package/lib/database.d.ts.map +1 -1
- package/lib/database.js +69 -51
- package/lib/database.js.map +1 -1
- package/lib/env.d.ts +6 -0
- package/lib/env.d.ts.map +1 -1
- package/lib/env.js +51 -0
- package/lib/env.js.map +1 -1
- package/lib/index.d.ts +5 -3
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +4 -3
- package/lib/index.js.map +1 -1
- package/lib/types.d.ts +3 -2
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js +6 -8
- package/lib/types.js.map +1 -1
- package/lib/zhin-stack-deps.d.ts +7 -6
- package/lib/zhin-stack-deps.d.ts.map +1 -1
- package/lib/zhin-stack-deps.js +14 -9
- package/lib/zhin-stack-deps.js.map +1 -1
- package/package.json +2 -2
- package/src/apply.ts +8 -1
- package/src/database-definitions.ts +336 -0
- package/src/database.ts +77 -52
- package/src/env.ts +52 -0
- package/src/index.ts +16 -3
- package/src/types.ts +12 -9
- package/src/zhin-stack-deps.ts +14 -9
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zhin-stack-deps.js","sourceRoot":"","sources":["../src/zhin-stack-deps.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,MAAM,UAAU,CAAC;AAC1B,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"zhin-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ared interactive scaffold wizard for create-zhin-app and @zhin.js/cli",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"inquirer": "^14.0.2",
|
|
23
23
|
"qrcode": "^1.5.4",
|
|
24
24
|
"yaml": "^2.9.0",
|
|
25
|
-
"@zhin.js/plugin-runtime": "1.1.
|
|
25
|
+
"@zhin.js/plugin-runtime": "1.1.11"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@types/fs-extra": "^11.0.4",
|
package/src/apply.ts
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
} from './adapter.js';
|
|
12
12
|
import { generateAIEnvVars, materializeAIConfig } from './ai.js';
|
|
13
13
|
import { generateDatabaseEnvVars, mergeEnvText } from './env.js';
|
|
14
|
+
import { getDatabaseDialectDefinition } from './database-definitions.js';
|
|
14
15
|
import { ensureDatabaseForAdapters, ensureDatabaseForAI, getAIDependencies } from './project-deps.js';
|
|
15
16
|
import { DEFAULT_CREATE_BOT_HTTP_PORT, ZHIN_STACK_VERSIONS } from './zhin-stack-deps.js';
|
|
16
17
|
|
|
@@ -141,10 +142,14 @@ export async function appendWizardEnvVars(
|
|
|
141
142
|
}
|
|
142
143
|
|
|
143
144
|
export function collectWizardDependencies(
|
|
144
|
-
options: Pick<InitOptions, 'adapters' | 'ai'>,
|
|
145
|
+
options: Pick<InitOptions, 'adapters' | 'ai' | 'database'>,
|
|
145
146
|
): Record<string, string> {
|
|
147
|
+
const databaseDriver = options.database
|
|
148
|
+
? getDatabaseDialectDefinition(options.database.dialect)?.driver
|
|
149
|
+
: undefined;
|
|
146
150
|
return {
|
|
147
151
|
...(options.adapters ? getAdapterDependencies(options.adapters) : {}),
|
|
152
|
+
...(databaseDriver ? { [databaseDriver.package]: databaseDriver.version } : {}),
|
|
148
153
|
...getAIDependencies(options.ai),
|
|
149
154
|
// AI authoring conventions(与 create-zhin 路径对齐)
|
|
150
155
|
...(options.ai?.enabled ? {
|
|
@@ -260,6 +265,8 @@ export function materializeDatabaseConfig(config: DatabaseConfig): Record<string
|
|
|
260
265
|
password: '${REDIS_PASSWORD}',
|
|
261
266
|
database: '${REDIS_DB}',
|
|
262
267
|
};
|
|
268
|
+
case 'memory':
|
|
269
|
+
return { dialect: 'memory' };
|
|
263
270
|
case 'sqlite':
|
|
264
271
|
default: {
|
|
265
272
|
const sqlite: Record<string, unknown> = { dialect: 'sqlite' };
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
import type { DatabaseConfig } from './types.js';
|
|
2
|
+
|
|
3
|
+
export type DatabaseDialect = DatabaseConfig['dialect'];
|
|
4
|
+
|
|
5
|
+
export interface DatabaseDriverDefinition {
|
|
6
|
+
readonly package: string;
|
|
7
|
+
readonly version: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface DatabaseDialectDefinition {
|
|
11
|
+
readonly dialect: DatabaseDialect;
|
|
12
|
+
readonly name: string;
|
|
13
|
+
readonly description: string;
|
|
14
|
+
/** Concrete service version covered by the repository's live acceptance tests. */
|
|
15
|
+
readonly verifiedServer: string;
|
|
16
|
+
readonly driver?: DatabaseDriverDefinition;
|
|
17
|
+
readonly schema: Readonly<Record<string, unknown>>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type SchemaIssueKind = 'env_missing' | 'invalid';
|
|
21
|
+
|
|
22
|
+
export interface DatabaseSchemaIssue {
|
|
23
|
+
readonly kind: SchemaIssueKind;
|
|
24
|
+
readonly path: string;
|
|
25
|
+
readonly message: string;
|
|
26
|
+
readonly envKey?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const text = (defaultValue?: string, extras: Record<string, unknown> = {}) => ({
|
|
30
|
+
type: 'string',
|
|
31
|
+
minLength: 1,
|
|
32
|
+
...(defaultValue === undefined ? {} : { default: defaultValue }),
|
|
33
|
+
...extras,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const port = (defaultValue: number) => ({
|
|
37
|
+
type: 'integer',
|
|
38
|
+
minimum: 1,
|
|
39
|
+
maximum: 65535,
|
|
40
|
+
default: defaultValue,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
export const DATABASE_DIALECT_DEFINITIONS: readonly DatabaseDialectDefinition[] = Object.freeze([
|
|
44
|
+
{
|
|
45
|
+
dialect: 'sqlite',
|
|
46
|
+
name: 'SQLite',
|
|
47
|
+
description: '推荐,零配置,适合单机 Bot',
|
|
48
|
+
verifiedServer: 'Node.js 内置 node:sqlite(Node.js 22.5+)',
|
|
49
|
+
schema: {
|
|
50
|
+
type: 'object',
|
|
51
|
+
additionalProperties: true,
|
|
52
|
+
required: ['dialect'],
|
|
53
|
+
properties: {
|
|
54
|
+
dialect: { const: 'sqlite' },
|
|
55
|
+
filename: text('./data/bot.db'),
|
|
56
|
+
mode: { type: 'string', enum: ['wal', 'delete', 'memory'], default: 'wal' },
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
dialect: 'mysql',
|
|
62
|
+
name: 'MySQL',
|
|
63
|
+
description: '关系型数据库',
|
|
64
|
+
verifiedServer: 'MySQL 8.4 LTS',
|
|
65
|
+
driver: { package: 'mysql2', version: '^3.23.2' },
|
|
66
|
+
schema: {
|
|
67
|
+
type: 'object',
|
|
68
|
+
additionalProperties: true,
|
|
69
|
+
required: ['dialect'],
|
|
70
|
+
properties: {
|
|
71
|
+
dialect: { const: 'mysql' },
|
|
72
|
+
host: text('127.0.0.1'),
|
|
73
|
+
port: port(3306),
|
|
74
|
+
user: text('root'),
|
|
75
|
+
password: { type: 'string' },
|
|
76
|
+
database: text('zhin_bot'),
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
dialect: 'pg',
|
|
82
|
+
name: 'PostgreSQL',
|
|
83
|
+
description: '关系型数据库,方言名为 pg',
|
|
84
|
+
verifiedServer: 'PostgreSQL 17',
|
|
85
|
+
driver: { package: 'pg', version: '^8.22.0' },
|
|
86
|
+
schema: {
|
|
87
|
+
type: 'object',
|
|
88
|
+
additionalProperties: true,
|
|
89
|
+
required: ['dialect'],
|
|
90
|
+
properties: {
|
|
91
|
+
dialect: { const: 'pg' },
|
|
92
|
+
host: text('127.0.0.1'),
|
|
93
|
+
port: port(5432),
|
|
94
|
+
user: text('postgres'),
|
|
95
|
+
password: { type: 'string' },
|
|
96
|
+
database: text('zhin_bot'),
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
dialect: 'mongodb',
|
|
102
|
+
name: 'MongoDB',
|
|
103
|
+
description: '文档数据库',
|
|
104
|
+
verifiedServer: 'MongoDB 8',
|
|
105
|
+
driver: { package: 'mongodb', version: '^7.5.0' },
|
|
106
|
+
schema: {
|
|
107
|
+
type: 'object',
|
|
108
|
+
additionalProperties: true,
|
|
109
|
+
required: ['dialect', 'url', 'dbName'],
|
|
110
|
+
properties: {
|
|
111
|
+
dialect: { const: 'mongodb' },
|
|
112
|
+
url: text('mongodb://127.0.0.1:27017', { pattern: '^mongodb(?:\\+srv)?://' }),
|
|
113
|
+
dbName: text('zhin_bot'),
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
dialect: 'redis',
|
|
119
|
+
name: 'Redis',
|
|
120
|
+
description: '键值数据库',
|
|
121
|
+
verifiedServer: 'Redis 8',
|
|
122
|
+
driver: { package: 'redis', version: '^6.1.0' },
|
|
123
|
+
schema: {
|
|
124
|
+
type: 'object',
|
|
125
|
+
additionalProperties: true,
|
|
126
|
+
required: ['dialect'],
|
|
127
|
+
properties: {
|
|
128
|
+
dialect: { const: 'redis' },
|
|
129
|
+
socket: {
|
|
130
|
+
type: 'object',
|
|
131
|
+
additionalProperties: true,
|
|
132
|
+
required: ['host', 'port'],
|
|
133
|
+
properties: {
|
|
134
|
+
host: text('127.0.0.1'),
|
|
135
|
+
port: port(6379),
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
password: { type: 'string' },
|
|
139
|
+
database: { type: 'integer', minimum: 0, default: 0 },
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
dialect: 'memory',
|
|
145
|
+
name: 'Memory',
|
|
146
|
+
description: '仅进程内,适合测试,不持久化',
|
|
147
|
+
verifiedServer: 'Zhin 内置实现',
|
|
148
|
+
schema: {
|
|
149
|
+
type: 'object',
|
|
150
|
+
additionalProperties: false,
|
|
151
|
+
required: ['dialect'],
|
|
152
|
+
properties: { dialect: { const: 'memory' } },
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
]);
|
|
156
|
+
|
|
157
|
+
const definitions = new Map(DATABASE_DIALECT_DEFINITIONS.map((definition) => [definition.dialect, definition]));
|
|
158
|
+
|
|
159
|
+
export function getDatabaseDialectDefinition(dialect: string): DatabaseDialectDefinition | undefined {
|
|
160
|
+
return definitions.get(dialect as DatabaseDialect);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function databaseChoiceLabel(definition: DatabaseDialectDefinition): string {
|
|
164
|
+
const driver = definition.driver
|
|
165
|
+
? `;驱动 ${definition.driver.package} ${definition.driver.version}`
|
|
166
|
+
: '';
|
|
167
|
+
return `${definition.name}(${definition.description};当前支持/实机验证 ${definition.verifiedServer}${driver})`;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function expandEnvReferences(
|
|
171
|
+
input: string,
|
|
172
|
+
path: string,
|
|
173
|
+
env: Readonly<Record<string, string | undefined>>,
|
|
174
|
+
issues: DatabaseSchemaIssue[],
|
|
175
|
+
): string {
|
|
176
|
+
let output = '';
|
|
177
|
+
let index = 0;
|
|
178
|
+
while (index < input.length) {
|
|
179
|
+
const start = input.indexOf('${', index);
|
|
180
|
+
if (start < 0) break;
|
|
181
|
+
let cursor = start + 2;
|
|
182
|
+
const first = input[cursor];
|
|
183
|
+
if (first === undefined || !isEnvKeyStart(first)) {
|
|
184
|
+
output += input.slice(index, start + 1);
|
|
185
|
+
index = start + 1;
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
cursor += 1;
|
|
189
|
+
while (cursor < input.length && isEnvKeyCharacter(input[cursor]!)) cursor += 1;
|
|
190
|
+
const key = input.slice(start + 2, cursor);
|
|
191
|
+
let fallback: string | undefined;
|
|
192
|
+
if (input[cursor] === ':' && (input[cursor + 1] === '-' || input[cursor + 1] === '=')) {
|
|
193
|
+
const close = input.indexOf('}', cursor + 2);
|
|
194
|
+
if (close < 0) {
|
|
195
|
+
output += input.slice(index, start + 1);
|
|
196
|
+
index = start + 1;
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
fallback = input.slice(cursor + 2, close);
|
|
200
|
+
cursor = close;
|
|
201
|
+
}
|
|
202
|
+
if (input[cursor] !== '}') {
|
|
203
|
+
output += input.slice(index, start + 1);
|
|
204
|
+
index = start + 1;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
output += input.slice(index, start);
|
|
208
|
+
const resolved = env[key];
|
|
209
|
+
if ((resolved === undefined || resolved === '') && fallback !== undefined) {
|
|
210
|
+
output += fallback;
|
|
211
|
+
} else if (resolved === undefined) {
|
|
212
|
+
issues.push({
|
|
213
|
+
kind: 'env_missing',
|
|
214
|
+
path,
|
|
215
|
+
envKey: key,
|
|
216
|
+
message: `环境变量 ${key} 未设置(${path})`,
|
|
217
|
+
});
|
|
218
|
+
} else {
|
|
219
|
+
output += resolved;
|
|
220
|
+
}
|
|
221
|
+
index = cursor + 1;
|
|
222
|
+
}
|
|
223
|
+
return output + input.slice(index);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function isEnvKeyStart(character: string): boolean {
|
|
227
|
+
return (character >= 'A' && character <= 'Z')
|
|
228
|
+
|| (character >= 'a' && character <= 'z')
|
|
229
|
+
|| character === '_';
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function isEnvKeyCharacter(character: string): boolean {
|
|
233
|
+
return isEnvKeyStart(character) || (character >= '0' && character <= '9');
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function validateValue(
|
|
237
|
+
schema: Record<string, unknown>,
|
|
238
|
+
rawValue: unknown,
|
|
239
|
+
path: string,
|
|
240
|
+
env: Readonly<Record<string, string | undefined>>,
|
|
241
|
+
issues: DatabaseSchemaIssue[],
|
|
242
|
+
): void {
|
|
243
|
+
let value = rawValue;
|
|
244
|
+
if (typeof value === 'string') {
|
|
245
|
+
const before = issues.length;
|
|
246
|
+
value = expandEnvReferences(value, path, env, issues);
|
|
247
|
+
if (issues.length > before) return;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if ('const' in schema && value !== schema.const) {
|
|
251
|
+
issues.push({ kind: 'invalid', path, message: `${path} 必须是 ${JSON.stringify(schema.const)}` });
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
const type = schema.type;
|
|
255
|
+
if (type === 'object') {
|
|
256
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
257
|
+
issues.push({ kind: 'invalid', path, message: `${path} 必须是对象` });
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
const record = value as Record<string, unknown>;
|
|
261
|
+
const properties = (schema.properties ?? {}) as Record<string, Record<string, unknown>>;
|
|
262
|
+
if (schema.additionalProperties === false) {
|
|
263
|
+
for (const key of Object.keys(record)) {
|
|
264
|
+
if (!(key in properties)) {
|
|
265
|
+
issues.push({ kind: 'invalid', path: `${path}.${key}`, message: `${path}.${key} 不是受支持的配置项` });
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
for (const required of (schema.required ?? []) as string[]) {
|
|
270
|
+
if (!(required in record) || record[required] === undefined || record[required] === null) {
|
|
271
|
+
issues.push({ kind: 'invalid', path: `${path}.${required}`, message: `${path}.${required} 不能为空` });
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
for (const [key, propertySchema] of Object.entries(properties)) {
|
|
275
|
+
if (record[key] !== undefined) validateValue(propertySchema, record[key], `${path}.${key}`, env, issues);
|
|
276
|
+
}
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
if (type === 'string') {
|
|
280
|
+
if (typeof value !== 'string') {
|
|
281
|
+
issues.push({ kind: 'invalid', path, message: `${path} 必须是字符串` });
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (typeof schema.minLength === 'number' && value.length < schema.minLength) {
|
|
285
|
+
issues.push({ kind: 'invalid', path, message: `${path} 不能为空` });
|
|
286
|
+
}
|
|
287
|
+
if (typeof schema.pattern === 'string' && !new RegExp(schema.pattern, 'u').test(value)) {
|
|
288
|
+
issues.push({ kind: 'invalid', path, message: `${path} 格式无效` });
|
|
289
|
+
}
|
|
290
|
+
if (Array.isArray(schema.enum) && !schema.enum.includes(value)) {
|
|
291
|
+
issues.push({ kind: 'invalid', path, message: `${path} 必须是 ${schema.enum.join('、')} 之一` });
|
|
292
|
+
}
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
if (type === 'integer') {
|
|
296
|
+
const numeric = typeof value === 'number'
|
|
297
|
+
? value
|
|
298
|
+
: typeof value === 'string' && /^\d+$/u.test(value.trim())
|
|
299
|
+
? Number(value.trim())
|
|
300
|
+
: Number.NaN;
|
|
301
|
+
if (!Number.isInteger(numeric)) {
|
|
302
|
+
issues.push({ kind: 'invalid', path, message: `${path} 必须是整数` });
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
if (typeof schema.minimum === 'number' && numeric < schema.minimum) {
|
|
306
|
+
issues.push({ kind: 'invalid', path, message: `${path} 不能小于 ${schema.minimum}` });
|
|
307
|
+
}
|
|
308
|
+
if (typeof schema.maximum === 'number' && numeric > schema.maximum) {
|
|
309
|
+
issues.push({ kind: 'invalid', path, message: `${path} 不能大于 ${schema.maximum}` });
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export function validateDatabaseConfig(
|
|
315
|
+
config: unknown,
|
|
316
|
+
env: Readonly<Record<string, string | undefined>> = {},
|
|
317
|
+
): readonly DatabaseSchemaIssue[] {
|
|
318
|
+
if (!config || typeof config !== 'object' || Array.isArray(config)) {
|
|
319
|
+
return [{ kind: 'invalid', path: 'database', message: 'database 必须是对象' }];
|
|
320
|
+
}
|
|
321
|
+
const dialect = (config as Record<string, unknown>).dialect;
|
|
322
|
+
if (typeof dialect !== 'string') {
|
|
323
|
+
return [{ kind: 'invalid', path: 'database.dialect', message: 'database.dialect 不能为空' }];
|
|
324
|
+
}
|
|
325
|
+
const definition = getDatabaseDialectDefinition(dialect);
|
|
326
|
+
if (!definition) {
|
|
327
|
+
return [{
|
|
328
|
+
kind: 'invalid',
|
|
329
|
+
path: 'database.dialect',
|
|
330
|
+
message: `不支持的数据库方言 ${JSON.stringify(dialect)}`,
|
|
331
|
+
}];
|
|
332
|
+
}
|
|
333
|
+
const issues: DatabaseSchemaIssue[] = [];
|
|
334
|
+
validateValue(definition.schema as Record<string, unknown>, config, 'database', env, issues);
|
|
335
|
+
return Object.freeze(issues);
|
|
336
|
+
}
|