@jayfong/x-server 2.50.1 → 2.51.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/lib/_cjs/cli/build_util.js +7 -0
- package/lib/_cjs/cli/cli.js +9 -1
- package/lib/_cjs/core/server.js +31 -0
- package/lib/cli/build_util.js +7 -0
- package/lib/cli/cli.js +9 -1
- package/lib/core/server.d.ts +1 -0
- package/lib/core/server.js +31 -0
- package/package.json +1 -1
|
@@ -141,6 +141,13 @@ class BuildUtil {
|
|
|
141
141
|
const distPrismaSchemaFile = _nodePath.default.join(distDir, _nodePath.default.basename(prismaSchemaFile));
|
|
142
142
|
await _fsExtra.default.copyFile(prismaSchemaFile, distPrismaSchemaFile);
|
|
143
143
|
|
|
144
|
+
// 多 schema 文件兼容
|
|
145
|
+
const multipleSchemaDir = _nodePath.default.join(options.cwd, 'src/db/schema');
|
|
146
|
+
if (await _fsExtra.default.pathExists(multipleSchemaDir)) {
|
|
147
|
+
const distMultipleSchemaDir = _nodePath.default.join(distDir, _nodePath.default.basename(multipleSchemaDir));
|
|
148
|
+
await _fsExtra.default.copy(multipleSchemaDir, distMultipleSchemaDir);
|
|
149
|
+
}
|
|
150
|
+
|
|
144
151
|
// 复制查询引擎
|
|
145
152
|
const libqueryEngineFiles = await (0, _globby.default)('libquery_engine-*', {
|
|
146
153
|
cwd: _nodePath.default.join(options.cwd, 'node_modules/.prisma/client'),
|
package/lib/_cjs/cli/cli.js
CHANGED
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
"use strict";
|
|
3
3
|
|
|
4
4
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
5
|
+
var _path = _interopRequireDefault(require("path"));
|
|
5
6
|
var _chokidar = _interopRequireDefault(require("chokidar"));
|
|
6
7
|
var _execa = _interopRequireDefault(require("execa"));
|
|
8
|
+
var _fsExtra = _interopRequireDefault(require("fs-extra"));
|
|
7
9
|
var _vscodeGenerateIndexStandalone = require("vscode-generate-index-standalone");
|
|
8
10
|
var _vtils = require("vtils");
|
|
9
11
|
var _yargs = _interopRequireDefault(require("yargs"));
|
|
@@ -142,7 +144,13 @@ _yargs.default.command('dev', '开始开发', _ => _.positional('index', {
|
|
|
142
144
|
// 之所以能成功是因为 Prisma 用的 dotenv 默认不会覆盖已经设置的环境变量
|
|
143
145
|
// https://github.com/motdotla/dotenv#override
|
|
144
146
|
process.env.DATABASE_URL = process.env.DATABASE_ACTION_URL || process.env.DATABASE_URL;
|
|
145
|
-
|
|
147
|
+
|
|
148
|
+
// 多 schema 文件兼容
|
|
149
|
+
// https://www.prisma.io/docs/orm/prisma-schema/overview/location#how-to-use-existing-prisma-cli-commands-with-multiple-prisma-schema-files
|
|
150
|
+
const schemaDir = _path.default.join(process.cwd(), 'src/db/schema');
|
|
151
|
+
const isMultipleFiles = await _fsExtra.default.pathExists(schemaDir);
|
|
152
|
+
const schemaArg = isMultipleFiles ? 'src/db' : 'src/db/schema.prisma';
|
|
153
|
+
await (0, _execa.default)('tnpx', ['prisma', ...argv._.slice(1), '--schema', schemaArg], {
|
|
146
154
|
cwd: process.cwd(),
|
|
147
155
|
stdio: 'inherit'
|
|
148
156
|
});
|
package/lib/_cjs/core/server.js
CHANGED
|
@@ -139,6 +139,9 @@ class Server {
|
|
|
139
139
|
});
|
|
140
140
|
}
|
|
141
141
|
|
|
142
|
+
// 静态文件服务
|
|
143
|
+
this.applyStatic();
|
|
144
|
+
|
|
142
145
|
// 接口统一入口
|
|
143
146
|
fastify.route({
|
|
144
147
|
method: 'POST',
|
|
@@ -170,12 +173,40 @@ class Server {
|
|
|
170
173
|
return 'ping:success';
|
|
171
174
|
} else if (payload.type === 'updateEnv') {
|
|
172
175
|
Object.assign(_x.x.env, payload.data);
|
|
176
|
+
|
|
177
|
+
// 更新静态文件服务
|
|
178
|
+
this.applyStatic();
|
|
173
179
|
}
|
|
174
180
|
}
|
|
175
181
|
}
|
|
176
182
|
});
|
|
177
183
|
});
|
|
178
184
|
}
|
|
185
|
+
async applyStatic() {
|
|
186
|
+
if (Array.isArray(_x.x.env.APP_STATIC_CONTENT)) {
|
|
187
|
+
const staticContent = _x.x.env.APP_STATIC_CONTENT;
|
|
188
|
+
for (const item of staticContent) {
|
|
189
|
+
if (!item.method) {
|
|
190
|
+
item.method = 'GET';
|
|
191
|
+
}
|
|
192
|
+
if (!this.fastify.hasRoute({
|
|
193
|
+
method: item.method,
|
|
194
|
+
url: item.path
|
|
195
|
+
})) {
|
|
196
|
+
this.fastify.route({
|
|
197
|
+
method: item.method,
|
|
198
|
+
url: item.path,
|
|
199
|
+
handler: async (_req, res) => {
|
|
200
|
+
if (!item.contentType && typeof item.content === 'string') {
|
|
201
|
+
res.header('Content-Type', 'plain/text');
|
|
202
|
+
}
|
|
203
|
+
return item.content;
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
179
210
|
async startCrons() {
|
|
180
211
|
// @ts-ignore
|
|
181
212
|
await Promise.resolve().then(() => (0, _interopRequireWildcard2.default)(require('.x/crons')));
|
package/lib/cli/build_util.js
CHANGED
|
@@ -135,6 +135,13 @@ export class BuildUtil {
|
|
|
135
135
|
const distPrismaSchemaFile = path.join(distDir, path.basename(prismaSchemaFile));
|
|
136
136
|
await fs.copyFile(prismaSchemaFile, distPrismaSchemaFile);
|
|
137
137
|
|
|
138
|
+
// 多 schema 文件兼容
|
|
139
|
+
const multipleSchemaDir = path.join(options.cwd, 'src/db/schema');
|
|
140
|
+
if (await fs.pathExists(multipleSchemaDir)) {
|
|
141
|
+
const distMultipleSchemaDir = path.join(distDir, path.basename(multipleSchemaDir));
|
|
142
|
+
await fs.copy(multipleSchemaDir, distMultipleSchemaDir);
|
|
143
|
+
}
|
|
144
|
+
|
|
138
145
|
// 复制查询引擎
|
|
139
146
|
const libqueryEngineFiles = await globby('libquery_engine-*', {
|
|
140
147
|
cwd: path.join(options.cwd, 'node_modules/.prisma/client'),
|
package/lib/cli/cli.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import path from 'path';
|
|
2
3
|
import chokidar from 'chokidar';
|
|
3
4
|
import execa from 'execa';
|
|
5
|
+
import fs from 'fs-extra';
|
|
4
6
|
import { generateManyIndex } from 'vscode-generate-index-standalone';
|
|
5
7
|
import { castArray, debounce } from 'vtils';
|
|
6
8
|
import yargs from 'yargs';
|
|
@@ -139,7 +141,13 @@ yargs.command('dev', '开始开发', _ => _.positional('index', {
|
|
|
139
141
|
// 之所以能成功是因为 Prisma 用的 dotenv 默认不会覆盖已经设置的环境变量
|
|
140
142
|
// https://github.com/motdotla/dotenv#override
|
|
141
143
|
process.env.DATABASE_URL = process.env.DATABASE_ACTION_URL || process.env.DATABASE_URL;
|
|
142
|
-
|
|
144
|
+
|
|
145
|
+
// 多 schema 文件兼容
|
|
146
|
+
// https://www.prisma.io/docs/orm/prisma-schema/overview/location#how-to-use-existing-prisma-cli-commands-with-multiple-prisma-schema-files
|
|
147
|
+
const schemaDir = path.join(process.cwd(), 'src/db/schema');
|
|
148
|
+
const isMultipleFiles = await fs.pathExists(schemaDir);
|
|
149
|
+
const schemaArg = isMultipleFiles ? 'src/db' : 'src/db/schema.prisma';
|
|
150
|
+
await execa('tnpx', ['prisma', ...argv._.slice(1), '--schema', schemaArg], {
|
|
143
151
|
cwd: process.cwd(),
|
|
144
152
|
stdio: 'inherit'
|
|
145
153
|
});
|
package/lib/core/server.d.ts
CHANGED
package/lib/core/server.js
CHANGED
|
@@ -133,6 +133,9 @@ export class Server {
|
|
|
133
133
|
});
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
+
// 静态文件服务
|
|
137
|
+
this.applyStatic();
|
|
138
|
+
|
|
136
139
|
// 接口统一入口
|
|
137
140
|
fastify.route({
|
|
138
141
|
method: 'POST',
|
|
@@ -164,12 +167,40 @@ export class Server {
|
|
|
164
167
|
return 'ping:success';
|
|
165
168
|
} else if (payload.type === 'updateEnv') {
|
|
166
169
|
Object.assign(x.env, payload.data);
|
|
170
|
+
|
|
171
|
+
// 更新静态文件服务
|
|
172
|
+
this.applyStatic();
|
|
167
173
|
}
|
|
168
174
|
}
|
|
169
175
|
}
|
|
170
176
|
});
|
|
171
177
|
});
|
|
172
178
|
}
|
|
179
|
+
async applyStatic() {
|
|
180
|
+
if (Array.isArray(x.env.APP_STATIC_CONTENT)) {
|
|
181
|
+
const staticContent = x.env.APP_STATIC_CONTENT;
|
|
182
|
+
for (const item of staticContent) {
|
|
183
|
+
if (!item.method) {
|
|
184
|
+
item.method = 'GET';
|
|
185
|
+
}
|
|
186
|
+
if (!this.fastify.hasRoute({
|
|
187
|
+
method: item.method,
|
|
188
|
+
url: item.path
|
|
189
|
+
})) {
|
|
190
|
+
this.fastify.route({
|
|
191
|
+
method: item.method,
|
|
192
|
+
url: item.path,
|
|
193
|
+
handler: async (_req, res) => {
|
|
194
|
+
if (!item.contentType && typeof item.content === 'string') {
|
|
195
|
+
res.header('Content-Type', 'plain/text');
|
|
196
|
+
}
|
|
197
|
+
return item.content;
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
173
204
|
async startCrons() {
|
|
174
205
|
// @ts-ignore
|
|
175
206
|
await import('.x/crons');
|