silgi 0.3.13 → 0.4.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 +1 -31
- package/cli.d.ts +1 -0
- package/config.d.ts +1 -0
- package/core.d.ts +1 -0
- package/dist/_chunks/index.mjs +233 -0
- package/dist/cli/compatibility.mjs +42 -0
- package/dist/cli/index.mjs +3 -184
- package/dist/cli/prepare.mjs +1346 -0
- package/dist/config/index.d.mts +5 -0
- package/dist/config/index.d.ts +5 -0
- package/dist/core/index.d.mts +136 -0
- package/dist/core/index.d.ts +136 -0
- package/dist/core/index.mjs +1444 -0
- package/dist/ecosystem/nitro/index.mjs +21 -29
- package/dist/ecosystem/nuxt/module.mjs +4 -25
- package/dist/kit/index.d.mts +90 -0
- package/dist/kit/index.d.ts +90 -0
- package/dist/kit/index.mjs +316 -0
- package/dist/meta/index.d.mts +3 -0
- package/dist/meta/index.d.ts +3 -0
- package/dist/meta/index.mjs +1 -0
- package/dist/presets/_all.gen.d.ts +2 -0
- package/dist/presets/_all.gen.mjs +8 -0
- package/dist/presets/_resolve.d.ts +8 -0
- package/dist/presets/_resolve.mjs +58 -0
- package/dist/presets/_types.gen.d.ts +5 -0
- package/dist/presets/_types.gen.mjs +1 -0
- package/dist/presets/h3/preset.d.ts +2 -0
- package/dist/presets/h3/preset.mjs +22 -0
- package/dist/presets/index.d.mts +1 -0
- package/dist/presets/index.d.ts +2 -0
- package/dist/presets/index.mjs +1 -0
- package/dist/presets/nitro/preset.d.ts +2 -0
- package/dist/presets/nitro/preset.mjs +26 -0
- package/dist/presets/npmpackage/preset.d.ts +2 -0
- package/dist/presets/npmpackage/preset.mjs +23 -0
- package/dist/runtime/index.d.ts +1 -0
- package/dist/runtime/index.mjs +1 -0
- package/dist/runtime/internal/debug.d.ts +2 -0
- package/dist/runtime/internal/debug.mjs +5 -0
- package/dist/runtime/internal/nitro.d.ts +2 -0
- package/dist/runtime/internal/nitro.mjs +45 -0
- package/dist/runtime/internal/plugin.d.ts +3 -0
- package/dist/runtime/internal/plugin.mjs +4 -0
- package/dist/shared/silgi.40ZJYm8F.d.mts +11 -0
- package/dist/shared/silgi.40ZJYm8F.d.ts +11 -0
- package/dist/shared/{silgi.ClpvycKI.d.mts → silgi.D8h2AAVk.d.ts} +451 -437
- package/dist/shared/{silgi.ClpvycKI.d.ts → silgi.DSHNePNA.d.mts} +451 -437
- package/dist/types/index.d.mts +42 -0
- package/dist/types/index.d.ts +42 -0
- package/dist/types/index.mjs +1 -0
- package/kit.d.ts +1 -0
- package/meta.d.ts +1 -0
- package/package.json +89 -39
- package/presets.d.ts +1 -0
- package/runtime-meta.d.ts +4 -0
- package/runtime-meta.mjs +32 -0
- package/runtime.d.ts +1 -0
- package/types.d.ts +1 -0
- package/bin/silgi.mjs +0 -3
- package/dist/chunks/generate.mjs +0 -1257
- package/dist/cli/config.d.mts +0 -1633
- package/dist/cli/config.d.ts +0 -1633
- package/dist/index.d.mts +0 -198
- package/dist/index.d.ts +0 -198
- package/dist/index.mjs +0 -503
- package/dist/shared/silgi.D2yb1XAa.mjs +0 -842
- /package/dist/{chunks → cli}/init.mjs +0 -0
- /package/dist/{cli/config.mjs → config/index.mjs} +0 -0
|
@@ -0,0 +1,1346 @@
|
|
|
1
|
+
import { defineCommand } from 'citty';
|
|
2
|
+
import { join, resolve, isAbsolute, relative, dirname, basename, extname } from 'pathe';
|
|
3
|
+
import { version } from 'silgi/meta';
|
|
4
|
+
import { runtimeDir } from 'silgi/runtime/meta';
|
|
5
|
+
import fsp, { readdir } from 'node:fs/promises';
|
|
6
|
+
import { promises, existsSync, readFileSync } from 'node:fs';
|
|
7
|
+
import { createJiti } from 'jiti';
|
|
8
|
+
import { resolvePath, parseNodeModulePath, lookupNodeModuleSubpath, resolve as resolve$1 } from 'mlly';
|
|
9
|
+
import { resolveAlias } from 'pathe/utils';
|
|
10
|
+
import { silgiGenerateType, SchemaParser, loadOptions } from 'silgi/core';
|
|
11
|
+
import { relativeWithDot, isDirectory, writeFile, resolveAlias as resolveAlias$1, resolvePath as resolvePath$1 } from 'silgi/kit';
|
|
12
|
+
import { toExports, createUnimport } from 'unimport';
|
|
13
|
+
import { readPackageJSON } from 'pkg-types';
|
|
14
|
+
import { hash } from 'ohash';
|
|
15
|
+
import { consola } from 'consola';
|
|
16
|
+
import { createHooks, createDebugger } from 'hookable';
|
|
17
|
+
import { join as join$1 } from 'node:path';
|
|
18
|
+
import { makeExecutableSchema, mergeSchemas } from '@graphql-tools/schema';
|
|
19
|
+
import { printSchemaWithDirectives } from '@graphql-tools/utils';
|
|
20
|
+
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
|
|
21
|
+
import { loadSchemaSync } from '@graphql-tools/load';
|
|
22
|
+
import { pascalCase } from 'scule';
|
|
23
|
+
import { h as hasInstalledModule, u as useSilgiCLI, s as silgiCLICtx } from './compatibility.mjs';
|
|
24
|
+
import { pathToFileURL, fileURLToPath } from 'node:url';
|
|
25
|
+
import { isRelative, withTrailingSlash } from 'ufo';
|
|
26
|
+
import { globby } from 'globby';
|
|
27
|
+
import ignore from 'ignore';
|
|
28
|
+
import { klona } from 'klona';
|
|
29
|
+
import { createStorage, builtinDrivers } from 'unstorage';
|
|
30
|
+
import 'semver/functions/satisfies.js';
|
|
31
|
+
import 'unctx';
|
|
32
|
+
|
|
33
|
+
async function h3Framework(silgi, skip = false) {
|
|
34
|
+
if (silgi.options.preset !== "h3" && skip === false)
|
|
35
|
+
return;
|
|
36
|
+
if (silgi.options.preset === "h3") {
|
|
37
|
+
silgi.hook("prepare:schema.ts", (data) => {
|
|
38
|
+
data.importItems.nitropack = {
|
|
39
|
+
import: [
|
|
40
|
+
{ name: "NitroApp", type: true }
|
|
41
|
+
],
|
|
42
|
+
from: "nitropack/types"
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
silgi.hook("after:prepare:schema.ts", (data) => {
|
|
46
|
+
data.unshift("type FrameworkContextExtends = NitroApp");
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
silgi.hook("prepare:createDTSFramework", (data) => {
|
|
50
|
+
data.importItems["silgi/types"] = {
|
|
51
|
+
import: [
|
|
52
|
+
{ name: "SilgiModuleContext", type: true }
|
|
53
|
+
],
|
|
54
|
+
from: "silgi/types"
|
|
55
|
+
};
|
|
56
|
+
data.customContent?.push(
|
|
57
|
+
"",
|
|
58
|
+
'declare module "h3" {',
|
|
59
|
+
" interface H3EventContext extends SilgiModuleContext {}",
|
|
60
|
+
"}",
|
|
61
|
+
""
|
|
62
|
+
);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function nitroFramework(silgi, skip = false) {
|
|
67
|
+
if (silgi.options.preset !== "nitro" && skip === false)
|
|
68
|
+
return;
|
|
69
|
+
silgi.hook("prepare:schema.ts", (data) => {
|
|
70
|
+
data.importItems.nitropack = {
|
|
71
|
+
import: [
|
|
72
|
+
{ name: "NitroApp", type: true }
|
|
73
|
+
],
|
|
74
|
+
from: "nitropack/types"
|
|
75
|
+
};
|
|
76
|
+
});
|
|
77
|
+
silgi.hook("after:prepare:schema.ts", (data) => {
|
|
78
|
+
data.unshift("type FrameworkContextExtends = NitroApp");
|
|
79
|
+
});
|
|
80
|
+
silgi.options.plugins.push({
|
|
81
|
+
packageImport: "silgi/runtime/internal/nitro",
|
|
82
|
+
path: join(runtimeDir, "internal/nitro")
|
|
83
|
+
});
|
|
84
|
+
await h3Framework(silgi, true);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const frameworkSetup = [h3Framework, nitroFramework];
|
|
88
|
+
|
|
89
|
+
async function prepare$1(silgi) {
|
|
90
|
+
await prepareDir(silgi.options.output.dir);
|
|
91
|
+
if (!silgi.options.static) {
|
|
92
|
+
await prepareDir(silgi.options.output.serverDir);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async function prepareDir(dir) {
|
|
96
|
+
await fsp.rm(dir, { recursive: true, force: true });
|
|
97
|
+
await fsp.mkdir(dir, { recursive: true });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function readCoreFile(silgi) {
|
|
101
|
+
const path = resolve(silgi.options.silgi.serverDir, "core.ts");
|
|
102
|
+
const jiti = createJiti(silgi.options.rootDir, {
|
|
103
|
+
fsCache: false,
|
|
104
|
+
interopDefault: true,
|
|
105
|
+
moduleCache: false,
|
|
106
|
+
alias: silgi.options.alias
|
|
107
|
+
});
|
|
108
|
+
const resolvedPath = jiti.esmResolve(path);
|
|
109
|
+
const context = await promises.readFile(path, { encoding: "utf-8" });
|
|
110
|
+
const coreFile = await jiti.import(
|
|
111
|
+
resolvedPath,
|
|
112
|
+
{
|
|
113
|
+
conditions: silgi.options.conditions
|
|
114
|
+
}
|
|
115
|
+
);
|
|
116
|
+
silgi.uris = coreFile.uris;
|
|
117
|
+
silgi.schemas = coreFile.schemas;
|
|
118
|
+
silgi.services = coreFile.services;
|
|
119
|
+
silgi.shareds = coreFile.shareds;
|
|
120
|
+
silgi.modulesURIs = coreFile.modulesURIs;
|
|
121
|
+
return {
|
|
122
|
+
context,
|
|
123
|
+
object: {
|
|
124
|
+
schemas: coreFile.schemas,
|
|
125
|
+
uris: coreFile.uris,
|
|
126
|
+
services: coreFile.services,
|
|
127
|
+
shareds: coreFile.shareds,
|
|
128
|
+
modulesURIs: coreFile.modulesURIs
|
|
129
|
+
},
|
|
130
|
+
path
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function traverseObject(silgi, obj, currentPath = []) {
|
|
135
|
+
const uriMap = /* @__PURE__ */ new Map();
|
|
136
|
+
function traverse(node, path = []) {
|
|
137
|
+
if (!node || typeof node !== "object")
|
|
138
|
+
return;
|
|
139
|
+
if (path.length === 4) {
|
|
140
|
+
const basePath = path.join("/");
|
|
141
|
+
let paramString = "";
|
|
142
|
+
if (node.router) {
|
|
143
|
+
let params = null;
|
|
144
|
+
if (node.router?._def?.typeName !== undefined) {
|
|
145
|
+
try {
|
|
146
|
+
const shape = node.router?.shape?.params?.shape;
|
|
147
|
+
params = shape ? Object.keys(shape) : null;
|
|
148
|
+
} catch {
|
|
149
|
+
params = null;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (params?.length) {
|
|
153
|
+
paramString = params.map((p) => `:${p}`).join("/");
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
uriMap.set(basePath, paramString);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
for (const key in node) {
|
|
160
|
+
if (!["_type", "fields"].includes(key)) {
|
|
161
|
+
traverse(node[key], [...path, key]);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
traverse(obj, currentPath);
|
|
166
|
+
return uriMap;
|
|
167
|
+
}
|
|
168
|
+
function scanActionModulesUris(silgi, obj, currentPath = []) {
|
|
169
|
+
const uriMap = {};
|
|
170
|
+
function traverse(node, path = []) {
|
|
171
|
+
if (!node || typeof node !== "object")
|
|
172
|
+
return;
|
|
173
|
+
if (path.length === 4) {
|
|
174
|
+
const basePath = path.join("/");
|
|
175
|
+
let moduleName = "";
|
|
176
|
+
if (node.modules?.graphqlyoga) {
|
|
177
|
+
let rootFieldName = null;
|
|
178
|
+
if (node.modules?.graphqlyoga?.rootFieldName) {
|
|
179
|
+
moduleName = "graphqlyoga";
|
|
180
|
+
rootFieldName = node.modules?.graphqlyoga?.rootFieldName;
|
|
181
|
+
}
|
|
182
|
+
if (!rootFieldName) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
uriMap[moduleName] ??= {};
|
|
186
|
+
uriMap[moduleName].rootFieldName ??= {};
|
|
187
|
+
uriMap[moduleName].rootFieldName[rootFieldName] = basePath;
|
|
188
|
+
}
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
for (const key in node) {
|
|
192
|
+
if (!["_type", "fields"].includes(key)) {
|
|
193
|
+
traverse(node[key], [...path, key]);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
traverse(obj, currentPath);
|
|
198
|
+
return uriMap;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async function scanUris(silgi) {
|
|
202
|
+
const { context, object, path } = await readCoreFile(silgi);
|
|
203
|
+
const uriMap = traverseObject(silgi, object.schemas, []);
|
|
204
|
+
const modulesURIs = scanActionModulesUris(silgi, object.services, []);
|
|
205
|
+
const uriContent = Array.from(uriMap.entries()).map(([uri, params]) => ` '${uri}': '${params}',`).join("\n");
|
|
206
|
+
let newContext = "";
|
|
207
|
+
if (uriMap.size > 0) {
|
|
208
|
+
newContext = context.replace(
|
|
209
|
+
/export const uris = \{[^}]*\}/,
|
|
210
|
+
`export const uris = {
|
|
211
|
+
${uriContent}
|
|
212
|
+
}`
|
|
213
|
+
).replace(
|
|
214
|
+
/export const modulesURIs = \{[^}]*\}/,
|
|
215
|
+
`export const modulesURIs = ${JSON.stringify(modulesURIs, null, 2)}`
|
|
216
|
+
);
|
|
217
|
+
} else {
|
|
218
|
+
newContext = context;
|
|
219
|
+
}
|
|
220
|
+
await promises.writeFile(path, newContext);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async function generateSilgiStorageBaseType(silgi) {
|
|
224
|
+
silgi.hook("prepare:schema.ts", async (options) => {
|
|
225
|
+
if (silgi.options.storage) {
|
|
226
|
+
for (const [key, _value] of Object.entries(silgi.options.storage)) {
|
|
227
|
+
options.storeBase.push(key);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
async function generateRouterDTS(silgi) {
|
|
234
|
+
silgi.hook("read:core.ts", async (data) => {
|
|
235
|
+
silgi.hook("close", async () => {
|
|
236
|
+
const { object } = await data();
|
|
237
|
+
const uris = object.uris;
|
|
238
|
+
const tag = "srn";
|
|
239
|
+
const groupedRoutes = Object.entries(uris || {}).reduce((acc, [key, _value]) => {
|
|
240
|
+
const [service, resource, method, action] = key.split("/");
|
|
241
|
+
const routePath = `${tag}/${service}/${resource}/${action}`;
|
|
242
|
+
if (!acc[routePath]) {
|
|
243
|
+
acc[routePath] = {};
|
|
244
|
+
}
|
|
245
|
+
acc[routePath][method] = {
|
|
246
|
+
input: `ExtractInputFromURI<'${key}'>`,
|
|
247
|
+
output: `ExtractOutputFromURI<'${key}'>`,
|
|
248
|
+
params: `ExtractRouterParamsFromURI<'${key}'>['params']`
|
|
249
|
+
};
|
|
250
|
+
return acc;
|
|
251
|
+
}, {});
|
|
252
|
+
const routerTypes = Object.entries(groupedRoutes).map(([path, methods]) => {
|
|
253
|
+
const methodEntries = Object.entries(methods).map(([method, { input, output, params }]) => {
|
|
254
|
+
return ` '${method}': {
|
|
255
|
+
input: ${input},
|
|
256
|
+
output: ${output}
|
|
257
|
+
params: ${params}
|
|
258
|
+
}`;
|
|
259
|
+
}).join(",\n");
|
|
260
|
+
return ` '/${path}': {
|
|
261
|
+
${methodEntries}
|
|
262
|
+
}`;
|
|
263
|
+
});
|
|
264
|
+
const nitro = [
|
|
265
|
+
"declare module 'nitropack/types' {",
|
|
266
|
+
" interface InternalApi extends RouterTypes {}",
|
|
267
|
+
"}"
|
|
268
|
+
];
|
|
269
|
+
const context = [
|
|
270
|
+
"import type { ExtractInputFromURI, ExtractOutputFromURI, ExtractRouterParamsFromURI } from 'silgi/types'",
|
|
271
|
+
"",
|
|
272
|
+
"export interface RouterTypes {",
|
|
273
|
+
routerTypes.join(",\n"),
|
|
274
|
+
"}",
|
|
275
|
+
"",
|
|
276
|
+
"declare module 'silgi/types' {",
|
|
277
|
+
" interface SilgiRouterTypes extends RouterTypes {",
|
|
278
|
+
" }",
|
|
279
|
+
"}",
|
|
280
|
+
"",
|
|
281
|
+
silgi.options.preset === "h3" || silgi.options.preset === "nitro" ? nitro.join("\n") : "",
|
|
282
|
+
"",
|
|
283
|
+
"export {}"
|
|
284
|
+
].join("\n");
|
|
285
|
+
const outputPath = resolve(silgi.options.build.typesDir, "silgi-routes.d.ts");
|
|
286
|
+
await promises.writeFile(outputPath, context);
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
async function createCoreFramework(silgi) {
|
|
292
|
+
const relativeRootDir = relativeWithDot(silgi.options.rootDir, silgi.options.serverDir);
|
|
293
|
+
if (silgi.options.preset !== "nitro" && silgi.options.preset !== "h3")
|
|
294
|
+
return;
|
|
295
|
+
const importItems = {
|
|
296
|
+
"silgi": {
|
|
297
|
+
import: [
|
|
298
|
+
{ name: "useSilgi" }
|
|
299
|
+
],
|
|
300
|
+
from: "silgi"
|
|
301
|
+
},
|
|
302
|
+
"silgi/types": {
|
|
303
|
+
import: [
|
|
304
|
+
{ name: "SilgiModuleContext", type: true }
|
|
305
|
+
],
|
|
306
|
+
from: "silgi/types"
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
await Promise.all([...silgi.options.modules, ...silgi.options._modules].map(async (id) => {
|
|
310
|
+
if (typeof id !== "string") {
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
const pkg = await readPackageJSON(id, { url: silgi.options.modulesDir }).catch(() => null);
|
|
314
|
+
if (!pkg?.name) {
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
if (importItems[pkg.name]) {
|
|
318
|
+
importItems[pkg.name].from = isAbsolute(id) ? relativeWithDot(relativeRootDir, id) : id;
|
|
319
|
+
}
|
|
320
|
+
}));
|
|
321
|
+
const customImports = [];
|
|
322
|
+
const functions = [];
|
|
323
|
+
await silgi.callHook("prepare:createCoreFramework", {
|
|
324
|
+
importItems,
|
|
325
|
+
customImports,
|
|
326
|
+
functions
|
|
327
|
+
});
|
|
328
|
+
const content = [
|
|
329
|
+
...functions.map((f) => ` ${f}(framework)`)
|
|
330
|
+
];
|
|
331
|
+
return {
|
|
332
|
+
content,
|
|
333
|
+
importItems
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
async function createDTSFramework(silgi) {
|
|
337
|
+
const relativeRootDir = relativeWithDot(silgi.options.rootDir, silgi.options.serverDir);
|
|
338
|
+
const importItems = {
|
|
339
|
+
"silgi/types": {
|
|
340
|
+
import: [
|
|
341
|
+
{ name: "SilgiModuleContext", type: true }
|
|
342
|
+
],
|
|
343
|
+
from: "silgi/types"
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
await Promise.all([...silgi.options.modules, ...silgi.options._modules].map(async (id) => {
|
|
347
|
+
if (typeof id !== "string") {
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
const pkg = await readPackageJSON(id, { url: silgi.options.modulesDir }).catch(() => null);
|
|
351
|
+
if (!pkg?.name) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
if (importItems[pkg.name]) {
|
|
355
|
+
importItems[pkg.name].from = isAbsolute(id) ? relativeWithDot(relativeRootDir, id) : id;
|
|
356
|
+
}
|
|
357
|
+
}));
|
|
358
|
+
const customImports = [];
|
|
359
|
+
const customContent = [];
|
|
360
|
+
await silgi.callHook("prepare:createDTSFramework", {
|
|
361
|
+
importItems,
|
|
362
|
+
customImports,
|
|
363
|
+
customContent
|
|
364
|
+
});
|
|
365
|
+
const content = [
|
|
366
|
+
...Object.entries(importItems).map(([_name, { from, import: imports }]) => {
|
|
367
|
+
const path = isAbsolute(from) ? relativeWithDot(silgi.options.build.typesDir, from) : from;
|
|
368
|
+
return `import { ${imports.map(({ type, name }) => type ? `type ${name}` : name).join(", ")} } from '${path}'`;
|
|
369
|
+
}),
|
|
370
|
+
"",
|
|
371
|
+
...customImports,
|
|
372
|
+
"",
|
|
373
|
+
...customContent,
|
|
374
|
+
""
|
|
375
|
+
];
|
|
376
|
+
return {
|
|
377
|
+
content,
|
|
378
|
+
importItems
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
async function schemaTemplate(silgi) {
|
|
383
|
+
const relativeRootDir = relativeWithDot(silgi.options.rootDir, silgi.options.serverDir);
|
|
384
|
+
const importItems = {
|
|
385
|
+
"silgi/types": {
|
|
386
|
+
import: [
|
|
387
|
+
{ name: "URIsTypes", type: true },
|
|
388
|
+
{ name: "Namespaces", type: true },
|
|
389
|
+
{ name: "SilgiModuleContext", type: true }
|
|
390
|
+
],
|
|
391
|
+
from: "silgi/types"
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
await Promise.all([...silgi.options.modules, ...silgi.options._modules].map(async (id) => {
|
|
395
|
+
if (typeof id !== "string") {
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
const pkg = await readPackageJSON(id, { url: silgi.options.modulesDir }).catch(() => null);
|
|
399
|
+
if (!pkg?.name) {
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
if (importItems[pkg.name]) {
|
|
403
|
+
importItems[pkg.name].from = isAbsolute(id) ? relativeWithDot(relativeRootDir, id) : id;
|
|
404
|
+
}
|
|
405
|
+
}));
|
|
406
|
+
const customImports = [];
|
|
407
|
+
const configs = [];
|
|
408
|
+
const contexts = [];
|
|
409
|
+
const methods = [];
|
|
410
|
+
const shareds = [];
|
|
411
|
+
const events = [];
|
|
412
|
+
const storeBase = [];
|
|
413
|
+
await silgi.callHook("prepare:schema.ts", {
|
|
414
|
+
importItems,
|
|
415
|
+
customImports,
|
|
416
|
+
configs,
|
|
417
|
+
contexts,
|
|
418
|
+
methods,
|
|
419
|
+
shareds,
|
|
420
|
+
storeBase,
|
|
421
|
+
events
|
|
422
|
+
});
|
|
423
|
+
const silgiExport = relativeWithDot(silgi.options.build.typesDir, `${silgi.options.silgi.serverDir}/core.ts`);
|
|
424
|
+
let addSilgiContext = false;
|
|
425
|
+
const importsContent = [
|
|
426
|
+
...Object.entries(importItems).map(([_name, { from, import: imports }]) => {
|
|
427
|
+
const path = isAbsolute(from) ? relativeWithDot(silgi.options.build.typesDir, from) : from;
|
|
428
|
+
return `import { ${imports.map(({ type, name }) => type ? `type ${name}` : name).join(", ")} } from '${path}'`;
|
|
429
|
+
}),
|
|
430
|
+
"",
|
|
431
|
+
...customImports,
|
|
432
|
+
""
|
|
433
|
+
];
|
|
434
|
+
const importData = [
|
|
435
|
+
"interface InferredNamespaces {",
|
|
436
|
+
...(silgi.options.namespaces || []).map((key) => ` ${key}: string,`),
|
|
437
|
+
"}",
|
|
438
|
+
"",
|
|
439
|
+
`type SchemaExtends = Namespaces<typeof import('${silgiExport}')['schemas']>`,
|
|
440
|
+
"",
|
|
441
|
+
`type SilgiURIsMerge = URIsTypes<typeof import('${silgiExport}')['uris']>`,
|
|
442
|
+
"",
|
|
443
|
+
contexts.length ? `type SilgiModuleContextExtends = ${contexts.map(({ value }) => value).join(" & ")}` : "type SilgiModuleContextExtends = {}",
|
|
444
|
+
"",
|
|
445
|
+
events.length ? `interface SilgiModuleEventsExtends extends ${events.map((item) => item.extends ? item.value : "").join(", ")} {
|
|
446
|
+
${events.map((item) => {
|
|
447
|
+
if (item.isSilgiContext) {
|
|
448
|
+
addSilgiContext = true;
|
|
449
|
+
}
|
|
450
|
+
return !item.extends && !addSilgiContext ? ` ${item.key}: ${item.value}` : item.isSilgiContext ? " context: SilgiModuleContext" : "";
|
|
451
|
+
}).join(",\n")}
|
|
452
|
+
}` : "interface SilgiModuleEventsExtends {}",
|
|
453
|
+
"",
|
|
454
|
+
shareds.length ? `type SilgiModuleSharedExtends = ${shareds.map(({ value }) => value).join(" & ")}` : "type SilgiModuleSharedExtends = {}",
|
|
455
|
+
"",
|
|
456
|
+
"interface SilgiModuleMethodsExtends {",
|
|
457
|
+
...(methods).map(({ key, value }) => ` ${key}: ${value},`),
|
|
458
|
+
"}",
|
|
459
|
+
"",
|
|
460
|
+
"interface SilgiModuleOptionsExtends {",
|
|
461
|
+
...(configs).map(({ key, value }) => ` ${key}: ${value},`),
|
|
462
|
+
"}",
|
|
463
|
+
"",
|
|
464
|
+
"interface SilgiStorageBaseExtends {",
|
|
465
|
+
...(storeBase).map((value) => ` ${value}: ''`),
|
|
466
|
+
"}",
|
|
467
|
+
"",
|
|
468
|
+
"",
|
|
469
|
+
"declare module 'silgi/types' {",
|
|
470
|
+
" interface FrameworkContext extends FrameworkContextExtends {}",
|
|
471
|
+
" interface SilgiSchema extends SchemaExtends {}",
|
|
472
|
+
" interface SilgiNamespaces extends InferredNamespaces {}",
|
|
473
|
+
" interface SilgiStorageBase extends SilgiStorageBaseExtends {}",
|
|
474
|
+
" interface SilgiModules extends SilgiModuleOptionsExtends {}",
|
|
475
|
+
" interface SilgiURIs extends SilgiURIsMerge {}",
|
|
476
|
+
" interface SilgiModuleContext extends SilgiModuleContextExtends {}",
|
|
477
|
+
" interface SilgiEvent extends SilgiModuleEventsExtends {}",
|
|
478
|
+
" interface SilgiDefaultShared extends SilgiModuleSharedExtends {}",
|
|
479
|
+
" interface SilgiModuleMethods extends SilgiModuleMethodsExtends {}",
|
|
480
|
+
" interface SilgiModuleOptions extends SilgiModuleOptionsExtends {}",
|
|
481
|
+
"}",
|
|
482
|
+
"",
|
|
483
|
+
"export {}"
|
|
484
|
+
];
|
|
485
|
+
await silgi.callHook("after:prepare:schema.ts", importData);
|
|
486
|
+
importData.unshift(...importsContent);
|
|
487
|
+
return importData;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
async function silgiCoreFile(_importItems, frameworkContext, silgi) {
|
|
491
|
+
const relativeRootDir = relativeWithDot(silgi.options.rootDir, silgi.options.serverDir);
|
|
492
|
+
const importItems = {
|
|
493
|
+
"silgi/core": {
|
|
494
|
+
import: [
|
|
495
|
+
{ name: "createSilgi" }
|
|
496
|
+
],
|
|
497
|
+
from: "silgi/core"
|
|
498
|
+
},
|
|
499
|
+
"silgi/types": {
|
|
500
|
+
import: [
|
|
501
|
+
{ name: "SilgiModuleOptions", type: true },
|
|
502
|
+
{ name: "SilgiOptions", type: true },
|
|
503
|
+
{ name: "FrameworkContext", type: true }
|
|
504
|
+
],
|
|
505
|
+
from: "silgi/types"
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
_importItems = { ..._importItems, ...importItems };
|
|
509
|
+
await Promise.all([...silgi.options.modules, ...silgi.options._modules].map(async (id) => {
|
|
510
|
+
if (typeof id !== "string") {
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
const pkg = await readPackageJSON(id, { url: silgi.options.modulesDir }).catch(() => null);
|
|
514
|
+
if (!pkg?.name) {
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
if (importItems[pkg.name]) {
|
|
518
|
+
importItems[pkg.name].from = isAbsolute(id) ? relativeWithDot(relativeRootDir, id) : id;
|
|
519
|
+
}
|
|
520
|
+
}));
|
|
521
|
+
const customImports = [];
|
|
522
|
+
const uris = [];
|
|
523
|
+
const services = [];
|
|
524
|
+
const shareds = [];
|
|
525
|
+
const schemas = [];
|
|
526
|
+
const buildSilgiExtraContent = [];
|
|
527
|
+
await silgi.callHook("prepare:core.ts", {
|
|
528
|
+
importItems,
|
|
529
|
+
customImports,
|
|
530
|
+
uris,
|
|
531
|
+
services,
|
|
532
|
+
shareds,
|
|
533
|
+
schemas,
|
|
534
|
+
buildSilgiExtraContent
|
|
535
|
+
});
|
|
536
|
+
if (services.length > 0) {
|
|
537
|
+
importItems["silgi/core"].import.push({ name: "mergeServices" });
|
|
538
|
+
}
|
|
539
|
+
if (shareds.length > 0) {
|
|
540
|
+
importItems["silgi/core"].import.push({ name: "mergeShared" });
|
|
541
|
+
}
|
|
542
|
+
if (schemas.length > 0) {
|
|
543
|
+
importItems["silgi/core"].import.push({ name: "mergeSchemas" });
|
|
544
|
+
}
|
|
545
|
+
const plugins = [];
|
|
546
|
+
for (const plugin of silgi.options.plugins) {
|
|
547
|
+
const pluginImportName = `_${hash(plugin.packageImport)}`;
|
|
548
|
+
customImports.push(`import ${pluginImportName} from '${plugin.packageImport}';`);
|
|
549
|
+
plugins.push(pluginImportName);
|
|
550
|
+
}
|
|
551
|
+
const importsContent = [
|
|
552
|
+
...Object.entries(importItems).map(([_name, { from, import: imports }]) => {
|
|
553
|
+
return `import { ${imports.map(({ type, name }) => type ? `type ${name}` : name).join(", ")} } from '${from}'`;
|
|
554
|
+
}),
|
|
555
|
+
...customImports,
|
|
556
|
+
""
|
|
557
|
+
];
|
|
558
|
+
const importData = [
|
|
559
|
+
"export const uris = {}",
|
|
560
|
+
"",
|
|
561
|
+
"export const modulesURIs = {}",
|
|
562
|
+
"",
|
|
563
|
+
schemas.length > 0 ? "export const schemas = mergeSchemas([" : "export const schemas = {",
|
|
564
|
+
...schemas.map((name) => {
|
|
565
|
+
return ` ${name},`;
|
|
566
|
+
}),
|
|
567
|
+
schemas.length > 0 ? "])" : "}",
|
|
568
|
+
"",
|
|
569
|
+
services.length > 0 ? "export const services = mergeServices([" : "export const services = {",
|
|
570
|
+
...services.map((name) => {
|
|
571
|
+
return ` ${name},`;
|
|
572
|
+
}),
|
|
573
|
+
services.length > 0 ? "])" : "}",
|
|
574
|
+
"",
|
|
575
|
+
shareds.length > 0 ? "export const shareds = mergeShared([" : "export const shareds = {",
|
|
576
|
+
...shareds.map((name) => {
|
|
577
|
+
return ` ${name},`;
|
|
578
|
+
}),
|
|
579
|
+
shareds.length > 0 ? "])" : "}",
|
|
580
|
+
"",
|
|
581
|
+
"export const silgiOptions: Partial<SilgiOptions> = {}",
|
|
582
|
+
"",
|
|
583
|
+
"export async function buildSilgi(framework: FrameworkContext, options?: Partial<SilgiModuleOptions>) {",
|
|
584
|
+
" const silgi = await createSilgi({",
|
|
585
|
+
" framework,",
|
|
586
|
+
" shared: shareds as any,",
|
|
587
|
+
" services: services as any,",
|
|
588
|
+
" schemas: schemas as any,",
|
|
589
|
+
" uris,",
|
|
590
|
+
" modulesURIs,",
|
|
591
|
+
` plugins: [${plugins.join(", ")}],`,
|
|
592
|
+
"",
|
|
593
|
+
" options: {",
|
|
594
|
+
" ...silgiOptions,",
|
|
595
|
+
" ...options,",
|
|
596
|
+
" },",
|
|
597
|
+
" })",
|
|
598
|
+
"",
|
|
599
|
+
...frameworkContext,
|
|
600
|
+
"",
|
|
601
|
+
...buildSilgiExtraContent,
|
|
602
|
+
"",
|
|
603
|
+
" return silgi",
|
|
604
|
+
"}",
|
|
605
|
+
""
|
|
606
|
+
];
|
|
607
|
+
await silgi.callHook("after:prepare:core.ts", importData);
|
|
608
|
+
importData.unshift(...importsContent);
|
|
609
|
+
return importData;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
async function writeTypesAndFiles(silgi) {
|
|
613
|
+
const data = await createCoreFramework(silgi);
|
|
614
|
+
await generateSilgiStorageBaseType(silgi);
|
|
615
|
+
await generateRouterDTS(silgi);
|
|
616
|
+
silgi.hook("prepare:types", (opts) => {
|
|
617
|
+
opts.references.push({ path: "./schema.d.ts" });
|
|
618
|
+
opts.references.push({ path: "./silgi-routes.d.ts" });
|
|
619
|
+
opts.references.push({ path: "./framework.d.ts" });
|
|
620
|
+
});
|
|
621
|
+
const schemaContent = await schemaTemplate(silgi);
|
|
622
|
+
const coreContent = await silgiCoreFile(data?.importItems ?? {}, data?.content ?? [], silgi);
|
|
623
|
+
const frameworkDTS = await createDTSFramework(silgi);
|
|
624
|
+
const { declarations, tsConfig } = await silgiGenerateType(silgi);
|
|
625
|
+
const tsConfigPath = resolve(
|
|
626
|
+
silgi.options.typescript.generateTsConfig ? silgi.options.build.dir : silgi.options.rootDir,
|
|
627
|
+
silgi.options.typescript.tsconfigPath
|
|
628
|
+
);
|
|
629
|
+
const typesDir = resolve(silgi.options.build.typesDir);
|
|
630
|
+
const silgiDir = resolve(silgi.options.silgi.serverDir);
|
|
631
|
+
let autoImportedTypes = [];
|
|
632
|
+
let autoImportExports = "";
|
|
633
|
+
if (silgi.unimport) {
|
|
634
|
+
await silgi.unimport.init();
|
|
635
|
+
const allImports = await silgi.unimport.getImports();
|
|
636
|
+
autoImportExports = toExports(allImports).replace(
|
|
637
|
+
/#internal\/nitro/g,
|
|
638
|
+
relative(typesDir, runtimeDir)
|
|
639
|
+
);
|
|
640
|
+
const resolvedImportPathMap = /* @__PURE__ */ new Map();
|
|
641
|
+
for (const i of allImports.filter((i2) => !i2.type)) {
|
|
642
|
+
if (resolvedImportPathMap.has(i.from)) {
|
|
643
|
+
continue;
|
|
644
|
+
}
|
|
645
|
+
let path = resolveAlias(i.from, silgi.options.alias);
|
|
646
|
+
if (isAbsolute(path)) {
|
|
647
|
+
const resolvedPath = await resolvePath(i.from, {
|
|
648
|
+
url: silgi.options.nodeModulesDirs
|
|
649
|
+
}).catch(() => null);
|
|
650
|
+
if (resolvedPath) {
|
|
651
|
+
const { dir, name } = parseNodeModulePath(resolvedPath);
|
|
652
|
+
if (!dir || !name) {
|
|
653
|
+
path = resolvedPath;
|
|
654
|
+
} else {
|
|
655
|
+
const subpath = await lookupNodeModuleSubpath(resolvedPath);
|
|
656
|
+
path = join(dir, name, subpath || "");
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
if (existsSync(path) && !await isDirectory(path)) {
|
|
661
|
+
path = path.replace(/\.[a-z]+$/, "");
|
|
662
|
+
}
|
|
663
|
+
if (isAbsolute(path)) {
|
|
664
|
+
path = relative(typesDir, path);
|
|
665
|
+
}
|
|
666
|
+
resolvedImportPathMap.set(i.from, path);
|
|
667
|
+
}
|
|
668
|
+
autoImportedTypes = [
|
|
669
|
+
silgi.options.imports && silgi.options.imports.autoImport !== false ? (await silgi.unimport.generateTypeDeclarations({
|
|
670
|
+
exportHelper: false,
|
|
671
|
+
resolvePath: (i) => resolvedImportPathMap.get(i.from) ?? i.from
|
|
672
|
+
})).trim() : ""
|
|
673
|
+
];
|
|
674
|
+
}
|
|
675
|
+
const buildFiles = [];
|
|
676
|
+
buildFiles.push({
|
|
677
|
+
path: join(typesDir, "silgi-routes.d.ts"),
|
|
678
|
+
// contents: uris.join('\n'),
|
|
679
|
+
contents: ""
|
|
680
|
+
});
|
|
681
|
+
buildFiles.push({
|
|
682
|
+
path: join(silgiDir, "core.ts"),
|
|
683
|
+
contents: coreContent.join("\n")
|
|
684
|
+
});
|
|
685
|
+
buildFiles.push({
|
|
686
|
+
path: join(typesDir, "schema.d.ts"),
|
|
687
|
+
contents: schemaContent.join("\n")
|
|
688
|
+
});
|
|
689
|
+
buildFiles.push({
|
|
690
|
+
path: join(typesDir, "silgi-imports.d.ts"),
|
|
691
|
+
contents: [...autoImportedTypes, autoImportExports || "export {}"].join(
|
|
692
|
+
"\n"
|
|
693
|
+
)
|
|
694
|
+
});
|
|
695
|
+
buildFiles.push({
|
|
696
|
+
path: join(typesDir, "silgi.d.ts"),
|
|
697
|
+
contents: declarations.join("\n")
|
|
698
|
+
});
|
|
699
|
+
buildFiles.push({
|
|
700
|
+
path: tsConfigPath,
|
|
701
|
+
contents: JSON.stringify(tsConfig, null, 2)
|
|
702
|
+
});
|
|
703
|
+
buildFiles.push({
|
|
704
|
+
path: join(typesDir, "framework.d.ts"),
|
|
705
|
+
contents: frameworkDTS.content.join("\n")
|
|
706
|
+
});
|
|
707
|
+
await Promise.all(
|
|
708
|
+
buildFiles.map(async (file) => {
|
|
709
|
+
await writeFile(
|
|
710
|
+
resolve(silgi.options.build.dir, file.path),
|
|
711
|
+
file.contents
|
|
712
|
+
);
|
|
713
|
+
})
|
|
714
|
+
);
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
const commonArgs = {
|
|
718
|
+
dir: {
|
|
719
|
+
type: "string",
|
|
720
|
+
description: "project root directory"
|
|
721
|
+
},
|
|
722
|
+
_dir: {
|
|
723
|
+
type: "positional",
|
|
724
|
+
default: ".",
|
|
725
|
+
description: "project root directory (prefer using `--dir`)"
|
|
726
|
+
}
|
|
727
|
+
};
|
|
728
|
+
|
|
729
|
+
async function graphQLLoadSchemaSync(schemaPointers, data = {}) {
|
|
730
|
+
let result;
|
|
731
|
+
try {
|
|
732
|
+
result = loadSchemaSync(schemaPointers, {
|
|
733
|
+
...data,
|
|
734
|
+
loaders: [
|
|
735
|
+
new GraphQLFileLoader(),
|
|
736
|
+
...data.loaders || []
|
|
737
|
+
]
|
|
738
|
+
});
|
|
739
|
+
} catch (e) {
|
|
740
|
+
if (
|
|
741
|
+
// https://www.graphql-tools.com/docs/documents-loading#no-files-found
|
|
742
|
+
(e.message || "").includes(
|
|
743
|
+
"Unable to find any GraphQL type definitions for the following pointers:"
|
|
744
|
+
)
|
|
745
|
+
) ; else {
|
|
746
|
+
throw e;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
return result;
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
async function graphqlLoadSync(silgi) {
|
|
753
|
+
const typeDefs = [];
|
|
754
|
+
const resolvers = [];
|
|
755
|
+
await silgi.callHook("before:graphql:schema", {
|
|
756
|
+
typeDefs,
|
|
757
|
+
resolvers
|
|
758
|
+
});
|
|
759
|
+
const schema = await graphQLLoadSchemaSync(silgi.options.serverDir, {
|
|
760
|
+
cwd: silgi.options.rootDir
|
|
761
|
+
});
|
|
762
|
+
const makeSchema = makeExecutableSchema({
|
|
763
|
+
typeDefs,
|
|
764
|
+
resolvers
|
|
765
|
+
});
|
|
766
|
+
const merge = mergeSchemas({
|
|
767
|
+
schemas: schema ? [makeSchema, schema] : [makeSchema]
|
|
768
|
+
});
|
|
769
|
+
if (merge) {
|
|
770
|
+
const schemaString = printSchemaWithDirectives(merge);
|
|
771
|
+
const context = [
|
|
772
|
+
"// THIS FILE IS GENERATED, DO NOT EDIT!",
|
|
773
|
+
"/* eslint-disable eslint-comments/no-unlimited-disable */",
|
|
774
|
+
"/* tslint:disable */",
|
|
775
|
+
"/* eslint-disable */",
|
|
776
|
+
"/* prettier-ignore */",
|
|
777
|
+
"/* silgi */",
|
|
778
|
+
"export const schema = `",
|
|
779
|
+
"",
|
|
780
|
+
schemaString,
|
|
781
|
+
"",
|
|
782
|
+
"`"
|
|
783
|
+
].join("\n");
|
|
784
|
+
const fsPath = join$1(silgi.options.silgi.serverDir, "generated/graphql.schema.ts");
|
|
785
|
+
await promises.mkdir(dirname(fsPath), { recursive: true });
|
|
786
|
+
await promises.writeFile(fsPath, context);
|
|
787
|
+
}
|
|
788
|
+
return {
|
|
789
|
+
schema
|
|
790
|
+
};
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
function moduleScanHooks(silgi) {
|
|
794
|
+
silgi.hook("prepare:schema.ts", async (options) => {
|
|
795
|
+
for (const module of silgi.scanModules) {
|
|
796
|
+
const exports = module.meta.exports;
|
|
797
|
+
if (exports?.interface) {
|
|
798
|
+
let importName;
|
|
799
|
+
options.importItems[module.meta.configKey] = {
|
|
800
|
+
import: [],
|
|
801
|
+
from: module.meta._packageName
|
|
802
|
+
};
|
|
803
|
+
if (exports.interface.config) {
|
|
804
|
+
importName = pascalCase(`${module.meta.configKey}Config`);
|
|
805
|
+
options.importItems[module.meta.configKey].import.push({
|
|
806
|
+
name: `SilgiModuleOptions as ${importName}`,
|
|
807
|
+
type: true
|
|
808
|
+
});
|
|
809
|
+
options.configs.push({
|
|
810
|
+
key: module.meta.configKey,
|
|
811
|
+
value: importName
|
|
812
|
+
});
|
|
813
|
+
}
|
|
814
|
+
if (exports.interface.context) {
|
|
815
|
+
importName = pascalCase(`${module.meta.configKey}Context`);
|
|
816
|
+
options.importItems[module.meta.configKey].import.push({
|
|
817
|
+
name: `SilgiModuleContext as ${importName}`,
|
|
818
|
+
type: true
|
|
819
|
+
});
|
|
820
|
+
options.contexts.push({
|
|
821
|
+
key: module.meta.configKey,
|
|
822
|
+
value: importName
|
|
823
|
+
});
|
|
824
|
+
}
|
|
825
|
+
if (exports.interface.method) {
|
|
826
|
+
importName = pascalCase(`${module.meta.configKey}Method`);
|
|
827
|
+
options.importItems[module.meta.configKey].import.push({
|
|
828
|
+
name: `SilgiModuleMethods as ${importName}`,
|
|
829
|
+
type: true
|
|
830
|
+
});
|
|
831
|
+
options.methods.push({
|
|
832
|
+
key: module.meta.configKey,
|
|
833
|
+
value: importName
|
|
834
|
+
});
|
|
835
|
+
}
|
|
836
|
+
if (exports.interface.shared) {
|
|
837
|
+
importName = pascalCase(`${module.meta.configKey}Shared`);
|
|
838
|
+
options.importItems[module.meta.configKey].import.push({
|
|
839
|
+
name: `SilgiModuleShared as ${importName}`,
|
|
840
|
+
type: true
|
|
841
|
+
});
|
|
842
|
+
options.shareds.push({
|
|
843
|
+
key: module.meta.configKey,
|
|
844
|
+
value: importName
|
|
845
|
+
});
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
});
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
async function afterModuleScan(silgi) {
|
|
853
|
+
await moduleScanHooks(silgi);
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
async function loadSilgiModuleInstance(silgiModule) {
|
|
857
|
+
if (typeof silgiModule === "string") {
|
|
858
|
+
throw new TypeError(`Could not load \`${silgiModule}\`. Is it installed?`);
|
|
859
|
+
}
|
|
860
|
+
if (typeof silgiModule !== "function") {
|
|
861
|
+
throw new TypeError(`Nuxt module should be a function: ${silgiModule}`);
|
|
862
|
+
}
|
|
863
|
+
return { silgiModule };
|
|
864
|
+
}
|
|
865
|
+
async function installModules(silgi) {
|
|
866
|
+
const jiti = createJiti(silgi.options.rootDir, {
|
|
867
|
+
alias: silgi.options.alias
|
|
868
|
+
});
|
|
869
|
+
for (const module of silgi.scanModules) {
|
|
870
|
+
if (hasInstalledModule(module.meta.configKey)) {
|
|
871
|
+
silgi.logger.info(`Module ${module.meta.configKey} installed`);
|
|
872
|
+
}
|
|
873
|
+
try {
|
|
874
|
+
const silgiModule = await jiti.import(module.entryPath, {
|
|
875
|
+
default: true,
|
|
876
|
+
conditions: silgi.options.conditions
|
|
877
|
+
});
|
|
878
|
+
await installModule(silgiModule, silgi);
|
|
879
|
+
} catch (err) {
|
|
880
|
+
silgi.logger.error(err);
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
async function installModule(moduleToInstall, silgi = useSilgiCLI(), inlineOptions) {
|
|
885
|
+
const { silgiModule } = await loadSilgiModuleInstance(moduleToInstall);
|
|
886
|
+
const res = await silgiModule({}, silgi) ?? {};
|
|
887
|
+
if (res === false) {
|
|
888
|
+
return false;
|
|
889
|
+
}
|
|
890
|
+
const metaData = await silgiModule.getMeta?.();
|
|
891
|
+
const installedModule = silgi.scanModules.find((m) => m.meta.configKey === metaData?.configKey);
|
|
892
|
+
if (installedModule) {
|
|
893
|
+
installedModule.installed = true;
|
|
894
|
+
} else {
|
|
895
|
+
throw new Error(`Module ${metaData?.name} not found`);
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
const logger = consola;
|
|
900
|
+
async function _resolveSilgiModule(mod, silgi) {
|
|
901
|
+
let _url;
|
|
902
|
+
let buildTimeModuleMeta = {};
|
|
903
|
+
const jiti = createJiti(silgi.options.rootDir, {
|
|
904
|
+
alias: silgi.options.alias
|
|
905
|
+
});
|
|
906
|
+
if (typeof mod === "string") {
|
|
907
|
+
const paths = /* @__PURE__ */ new Set();
|
|
908
|
+
mod = resolveAlias$1(mod, silgi.options.alias);
|
|
909
|
+
if (isRelative(mod)) {
|
|
910
|
+
mod = resolve(silgi.options.rootDir, mod);
|
|
911
|
+
}
|
|
912
|
+
paths.add(join(mod, "silgi"));
|
|
913
|
+
paths.add(join(mod, "module"));
|
|
914
|
+
paths.add(mod);
|
|
915
|
+
for (const path of paths) {
|
|
916
|
+
try {
|
|
917
|
+
const src = isAbsolute(path) ? pathToFileURL(await resolvePath$1(path, { fallbackToOriginal: false, extensions: silgi.options.extensions })).href : await resolve$1(path, { url: silgi.options.modulesDir.map((m) => pathToFileURL(m.replace(/\/node_modules\/?$/, ""))), extensions: silgi.options.extensions });
|
|
918
|
+
mod = await jiti.import(src, {
|
|
919
|
+
default: true,
|
|
920
|
+
conditions: silgi.options.conditions
|
|
921
|
+
});
|
|
922
|
+
_url = fileURLToPath(new URL(src));
|
|
923
|
+
const moduleMetadataPath = new URL("module.json", src);
|
|
924
|
+
if (existsSync(moduleMetadataPath)) {
|
|
925
|
+
buildTimeModuleMeta = JSON.parse(await promises.readFile(moduleMetadataPath, "utf-8"));
|
|
926
|
+
} else {
|
|
927
|
+
if (typeof mod === "function") {
|
|
928
|
+
const meta = await mod.getMeta?.();
|
|
929
|
+
buildTimeModuleMeta = {
|
|
930
|
+
...meta
|
|
931
|
+
};
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
break;
|
|
935
|
+
} catch (error) {
|
|
936
|
+
const code = error.code;
|
|
937
|
+
if (code === "MODULE_NOT_FOUND" || code === "ERR_PACKAGE_PATH_NOT_EXPORTED" || code === "ERR_MODULE_NOT_FOUND" || code === "ERR_UNSUPPORTED_DIR_IMPORT" || code === "ENOTDIR") {
|
|
938
|
+
continue;
|
|
939
|
+
}
|
|
940
|
+
logger.error(`Error while importing module \`${mod}\`: ${error}`);
|
|
941
|
+
throw error;
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
if (!buildTimeModuleMeta) {
|
|
946
|
+
throw new Error(`Module ${mod} is not a valid Silgi module`);
|
|
947
|
+
}
|
|
948
|
+
if (typeof mod === "function") {
|
|
949
|
+
silgi.scanModules.push({
|
|
950
|
+
meta: buildTimeModuleMeta,
|
|
951
|
+
entryPath: _url,
|
|
952
|
+
installed: false
|
|
953
|
+
});
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
async function scanModules$1(silgi) {
|
|
957
|
+
const _modules = [
|
|
958
|
+
...silgi.options._modules,
|
|
959
|
+
...silgi.options.modules
|
|
960
|
+
];
|
|
961
|
+
for await (const mod of _modules) {
|
|
962
|
+
await _resolveSilgiModule(mod, silgi);
|
|
963
|
+
}
|
|
964
|
+
const moduleMap = new Map(
|
|
965
|
+
silgi.scanModules.map((m) => [m.meta?.configKey, m])
|
|
966
|
+
);
|
|
967
|
+
const graph = createDependencyGraph(silgi.scanModules);
|
|
968
|
+
const sortedKeys = topologicalSort(graph);
|
|
969
|
+
const modules = sortedKeys.map((key) => moduleMap.get(key)).filter((module) => Boolean(module));
|
|
970
|
+
silgi.scanModules = modules;
|
|
971
|
+
}
|
|
972
|
+
function createDependencyGraph(modules) {
|
|
973
|
+
const graph = /* @__PURE__ */ new Map();
|
|
974
|
+
modules.forEach((module) => {
|
|
975
|
+
const key = module.meta?.configKey;
|
|
976
|
+
if (key) {
|
|
977
|
+
graph.set(key, /* @__PURE__ */ new Set());
|
|
978
|
+
}
|
|
979
|
+
});
|
|
980
|
+
modules.forEach((module) => {
|
|
981
|
+
const key = module.meta?.configKey;
|
|
982
|
+
const deps = module.meta?.dependencies || [];
|
|
983
|
+
if (key && deps.length > 0) {
|
|
984
|
+
const modulesDeps = graph.get(key) || /* @__PURE__ */ new Set();
|
|
985
|
+
deps.forEach((dep) => {
|
|
986
|
+
if (graph.has(dep)) {
|
|
987
|
+
modulesDeps.add(dep);
|
|
988
|
+
}
|
|
989
|
+
});
|
|
990
|
+
graph.set(key, modulesDeps);
|
|
991
|
+
}
|
|
992
|
+
});
|
|
993
|
+
return graph;
|
|
994
|
+
}
|
|
995
|
+
function topologicalSort(graph) {
|
|
996
|
+
const visited = /* @__PURE__ */ new Set();
|
|
997
|
+
const temp = /* @__PURE__ */ new Set();
|
|
998
|
+
const order = [];
|
|
999
|
+
function visit(node) {
|
|
1000
|
+
if (temp.has(node)) {
|
|
1001
|
+
throw new Error(`D\xF6ng\xFCsel ba\u011F\u0131ml\u0131l\u0131k tespit edildi: ${node}`);
|
|
1002
|
+
}
|
|
1003
|
+
if (visited.has(node))
|
|
1004
|
+
return;
|
|
1005
|
+
temp.add(node);
|
|
1006
|
+
const deps = graph.get(node) || /* @__PURE__ */ new Set();
|
|
1007
|
+
for (const dep of deps) {
|
|
1008
|
+
visit(dep);
|
|
1009
|
+
}
|
|
1010
|
+
temp.delete(node);
|
|
1011
|
+
visited.add(node);
|
|
1012
|
+
order.push(node);
|
|
1013
|
+
}
|
|
1014
|
+
for (const [node, deps] of graph.entries()) {
|
|
1015
|
+
if (deps.size === 0 && !visited.has(node)) {
|
|
1016
|
+
visit(node);
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
for (const node of graph.keys()) {
|
|
1020
|
+
if (!visited.has(node)) {
|
|
1021
|
+
visit(node);
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
return order;
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
function resolveIgnorePatterns(silgi, relativePath) {
|
|
1028
|
+
if (!silgi) {
|
|
1029
|
+
return [];
|
|
1030
|
+
}
|
|
1031
|
+
const ignorePatterns = silgi.options.ignore.flatMap((s) => resolveGroupSyntax(s));
|
|
1032
|
+
const nuxtignoreFile = join(silgi.options.rootDir, ".nuxtignore");
|
|
1033
|
+
if (existsSync(nuxtignoreFile)) {
|
|
1034
|
+
const contents = readFileSync(nuxtignoreFile, "utf-8");
|
|
1035
|
+
ignorePatterns.push(...contents.trim().split(/\r?\n/));
|
|
1036
|
+
}
|
|
1037
|
+
return ignorePatterns;
|
|
1038
|
+
}
|
|
1039
|
+
function isIgnored(pathname, silgi, _stats) {
|
|
1040
|
+
if (!silgi) {
|
|
1041
|
+
return false;
|
|
1042
|
+
}
|
|
1043
|
+
if (!silgi._ignore) {
|
|
1044
|
+
silgi._ignore = ignore(silgi.options.ignoreOptions);
|
|
1045
|
+
silgi._ignore.add(resolveIgnorePatterns(silgi));
|
|
1046
|
+
}
|
|
1047
|
+
const relativePath = relative(silgi.options.rootDir, pathname);
|
|
1048
|
+
if (relativePath[0] === "." && relativePath[1] === ".") {
|
|
1049
|
+
return false;
|
|
1050
|
+
}
|
|
1051
|
+
return !!(relativePath && silgi._ignore.ignores(relativePath));
|
|
1052
|
+
}
|
|
1053
|
+
function resolveGroupSyntax(group) {
|
|
1054
|
+
let groups = [group];
|
|
1055
|
+
while (groups.some((group2) => group2.includes("{"))) {
|
|
1056
|
+
groups = groups.flatMap((group2) => {
|
|
1057
|
+
const [head, ...tail] = group2.split("{");
|
|
1058
|
+
if (tail.length) {
|
|
1059
|
+
const [body = "", ...rest] = tail.join("{").split("}");
|
|
1060
|
+
return body.split(",").map((part) => `${head}${part}${rest.join("")}`);
|
|
1061
|
+
}
|
|
1062
|
+
return group2;
|
|
1063
|
+
});
|
|
1064
|
+
}
|
|
1065
|
+
return groups;
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
async function scanFiles$1(silgi) {
|
|
1069
|
+
const filePaths = /* @__PURE__ */ new Set();
|
|
1070
|
+
const scannedPaths = [];
|
|
1071
|
+
const dir = silgi.options.serverDir;
|
|
1072
|
+
const files = (await globby(dir, { cwd: silgi.options.rootDir, ignore: silgi.options.ignore })).sort();
|
|
1073
|
+
if (files.length) {
|
|
1074
|
+
const siblings = await readdir(dirname(dir)).catch(() => []);
|
|
1075
|
+
const directory = basename(dir);
|
|
1076
|
+
if (!siblings.includes(directory)) {
|
|
1077
|
+
const directoryLowerCase = directory.toLowerCase();
|
|
1078
|
+
const caseCorrected = siblings.find((sibling) => sibling.toLowerCase() === directoryLowerCase);
|
|
1079
|
+
if (caseCorrected) {
|
|
1080
|
+
const original = relative(silgi.options.serverDir, dir);
|
|
1081
|
+
const corrected = relative(silgi.options.serverDir, join(dirname(dir), caseCorrected));
|
|
1082
|
+
consola.warn(`Components not scanned from \`~/${corrected}\`. Did you mean to name the directory \`~/${original}\` instead?`);
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
for (const _file of files) {
|
|
1087
|
+
const filePath = resolve(dir, _file);
|
|
1088
|
+
if (scannedPaths.find((d) => filePath.startsWith(withTrailingSlash(d))) || isIgnored(filePath, silgi)) {
|
|
1089
|
+
continue;
|
|
1090
|
+
}
|
|
1091
|
+
if (filePaths.has(filePath)) {
|
|
1092
|
+
continue;
|
|
1093
|
+
}
|
|
1094
|
+
filePaths.add(filePath);
|
|
1095
|
+
if (silgi.options.extensions.includes(extname(filePath))) {
|
|
1096
|
+
const parser = new SchemaParser({
|
|
1097
|
+
debug: false
|
|
1098
|
+
});
|
|
1099
|
+
const readfile = readFileSync(filePath, "utf-8");
|
|
1100
|
+
const { exportVariables, parseInterfaceDeclarations } = parser.parseExports(readfile, filePath);
|
|
1101
|
+
const createServices = exportVariables("createService", filePath);
|
|
1102
|
+
if (createServices.length > 0) {
|
|
1103
|
+
for (const createService of createServices) {
|
|
1104
|
+
const { exportName, path } = createService;
|
|
1105
|
+
silgi.hook("prepare:core.ts", (options) => {
|
|
1106
|
+
options.services.push(exportName);
|
|
1107
|
+
});
|
|
1108
|
+
silgi.hook("prepare:core.ts", (options) => {
|
|
1109
|
+
options.importItems[path] ??= {
|
|
1110
|
+
import: [],
|
|
1111
|
+
from: relativeWithDot(silgi.options.silgi.serverDir, path)
|
|
1112
|
+
};
|
|
1113
|
+
options.importItems[path].import.push({
|
|
1114
|
+
name: exportName
|
|
1115
|
+
});
|
|
1116
|
+
});
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
const createSchemas = exportVariables("createSchema", filePath);
|
|
1120
|
+
if (createSchemas.length > 0) {
|
|
1121
|
+
for (const createSchema of createSchemas) {
|
|
1122
|
+
const { exportName, path } = createSchema;
|
|
1123
|
+
silgi.hook("prepare:core.ts", (options) => {
|
|
1124
|
+
options.schemas.push(exportName);
|
|
1125
|
+
});
|
|
1126
|
+
silgi.hook("prepare:core.ts", (options) => {
|
|
1127
|
+
options.importItems[path] ??= {
|
|
1128
|
+
import: [],
|
|
1129
|
+
from: relativeWithDot(silgi.options.silgi.serverDir, path)
|
|
1130
|
+
};
|
|
1131
|
+
options.importItems[path].import.push({
|
|
1132
|
+
name: exportName
|
|
1133
|
+
});
|
|
1134
|
+
});
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
const createShareds = exportVariables("createShared", filePath);
|
|
1138
|
+
if (createShareds.length > 0) {
|
|
1139
|
+
for (const createShared of createShareds) {
|
|
1140
|
+
const { exportName, path } = createShared;
|
|
1141
|
+
silgi.hook("prepare:core.ts", (options) => {
|
|
1142
|
+
options.shareds.push(exportName);
|
|
1143
|
+
});
|
|
1144
|
+
silgi.hook("prepare:core.ts", (options) => {
|
|
1145
|
+
options.importItems[path] ??= {
|
|
1146
|
+
import: [],
|
|
1147
|
+
// Relative path kaldirmamiz gerekiyor bunlar hooklarin bittigi yerde karar verilmeli.
|
|
1148
|
+
from: relativeWithDot(silgi.options.silgi.serverDir, path)
|
|
1149
|
+
};
|
|
1150
|
+
options.importItems[path].import.push({
|
|
1151
|
+
name: exportName
|
|
1152
|
+
});
|
|
1153
|
+
});
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
const sharedsTypes = parseInterfaceDeclarations("ExtendShared", filePath);
|
|
1157
|
+
if (sharedsTypes.length > 0) {
|
|
1158
|
+
for (const sharedType of sharedsTypes) {
|
|
1159
|
+
const { exportName, path } = sharedType;
|
|
1160
|
+
silgi.hook("prepare:schema.ts", (options) => {
|
|
1161
|
+
options.shareds.push({
|
|
1162
|
+
key: exportName,
|
|
1163
|
+
value: exportName
|
|
1164
|
+
});
|
|
1165
|
+
});
|
|
1166
|
+
silgi.hook("prepare:schema.ts", (options) => {
|
|
1167
|
+
options.importItems[path] ??= {
|
|
1168
|
+
import: [],
|
|
1169
|
+
from: path
|
|
1170
|
+
};
|
|
1171
|
+
options.importItems[path].import.push({
|
|
1172
|
+
name: exportName,
|
|
1173
|
+
type: true
|
|
1174
|
+
});
|
|
1175
|
+
});
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
const contextTypes = parseInterfaceDeclarations("ExtendContext", filePath);
|
|
1179
|
+
if (contextTypes.length > 0) {
|
|
1180
|
+
for (const contextType of contextTypes) {
|
|
1181
|
+
const { exportName, path } = contextType;
|
|
1182
|
+
silgi.hook("prepare:schema.ts", (options) => {
|
|
1183
|
+
options.contexts.push({
|
|
1184
|
+
key: exportName,
|
|
1185
|
+
value: exportName
|
|
1186
|
+
});
|
|
1187
|
+
});
|
|
1188
|
+
silgi.hook("prepare:schema.ts", (options) => {
|
|
1189
|
+
options.importItems[path] ??= {
|
|
1190
|
+
import: [],
|
|
1191
|
+
from: path
|
|
1192
|
+
};
|
|
1193
|
+
options.importItems[path].import.push({
|
|
1194
|
+
name: exportName,
|
|
1195
|
+
type: true
|
|
1196
|
+
});
|
|
1197
|
+
});
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
async function createStorageCLI(silgi) {
|
|
1205
|
+
const storage = createStorage();
|
|
1206
|
+
const mounts = klona({
|
|
1207
|
+
...silgi.options.storage,
|
|
1208
|
+
...silgi.options.devStorage
|
|
1209
|
+
});
|
|
1210
|
+
for (const [path, opts] of Object.entries(mounts)) {
|
|
1211
|
+
if (opts.driver) {
|
|
1212
|
+
const driver = await import(builtinDrivers[opts.driver] || opts.driver).then((r) => r.default || r);
|
|
1213
|
+
storage.mount(path, driver(opts));
|
|
1214
|
+
} else {
|
|
1215
|
+
silgi.logger.warn(`No \`driver\` set for storage mount point "${path}".`);
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
return storage;
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
const GLOB_SCAN_PATTERN = "**/*.{js,mjs,cjs,ts,mts,cts,tsx,jsx}";
|
|
1222
|
+
async function scanAndSyncOptions(silgi) {
|
|
1223
|
+
const scannedModules = await scanModules(silgi);
|
|
1224
|
+
silgi.options.modules = silgi.options.modules || [];
|
|
1225
|
+
for (const modPath of scannedModules) {
|
|
1226
|
+
if (!silgi.options.modules.includes(modPath)) {
|
|
1227
|
+
silgi.options.modules.push(modPath);
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
async function scanModules(silgi) {
|
|
1232
|
+
const files = await scanFiles(silgi, "modules");
|
|
1233
|
+
return files.map((f) => f.fullPath);
|
|
1234
|
+
}
|
|
1235
|
+
async function scanFiles(silgi, name) {
|
|
1236
|
+
const files = await Promise.all(
|
|
1237
|
+
silgi.options.scanDirs.map((dir) => scanDir(silgi, dir, name))
|
|
1238
|
+
).then((r) => r.flat());
|
|
1239
|
+
return files;
|
|
1240
|
+
}
|
|
1241
|
+
async function scanDir(silgi, dir, name) {
|
|
1242
|
+
const fileNames = await globby(join(name, GLOB_SCAN_PATTERN), {
|
|
1243
|
+
cwd: dir,
|
|
1244
|
+
dot: true,
|
|
1245
|
+
ignore: silgi.options.ignore,
|
|
1246
|
+
absolute: true
|
|
1247
|
+
});
|
|
1248
|
+
return fileNames.map((fullPath) => {
|
|
1249
|
+
return {
|
|
1250
|
+
fullPath,
|
|
1251
|
+
path: relative(join(dir, name), fullPath)
|
|
1252
|
+
};
|
|
1253
|
+
}).sort((a, b) => a.path.localeCompare(b.path));
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
async function createSilgiCLI(config = {}, opts = {}) {
|
|
1257
|
+
const options = await loadOptions(config, opts);
|
|
1258
|
+
const hooks = createHooks();
|
|
1259
|
+
const silgi = {
|
|
1260
|
+
modulesURIs: {},
|
|
1261
|
+
scannedURIs: /* @__PURE__ */ new Map(),
|
|
1262
|
+
services: {},
|
|
1263
|
+
uris: {},
|
|
1264
|
+
shareds: {},
|
|
1265
|
+
schemas: {},
|
|
1266
|
+
unimport: undefined,
|
|
1267
|
+
options,
|
|
1268
|
+
hooks,
|
|
1269
|
+
// vfs: {}
|
|
1270
|
+
_requiredModules: {},
|
|
1271
|
+
logger: consola.withTag("silgi"),
|
|
1272
|
+
close: () => silgi.hooks.callHook("close", silgi),
|
|
1273
|
+
storage: undefined,
|
|
1274
|
+
scanModules: [],
|
|
1275
|
+
_modules: [],
|
|
1276
|
+
callHook: hooks.callHook,
|
|
1277
|
+
addHooks: hooks.addHooks,
|
|
1278
|
+
hook: hooks.hook,
|
|
1279
|
+
async updateConfig(_config) {
|
|
1280
|
+
}
|
|
1281
|
+
};
|
|
1282
|
+
if (silgiCLICtx.tryUse()) {
|
|
1283
|
+
silgiCLICtx.unset();
|
|
1284
|
+
silgiCLICtx.set(silgi);
|
|
1285
|
+
} else {
|
|
1286
|
+
silgiCLICtx.set(silgi);
|
|
1287
|
+
silgi.hook("close", () => silgiCLICtx.unset());
|
|
1288
|
+
}
|
|
1289
|
+
if (silgi.options.debug) {
|
|
1290
|
+
createDebugger(silgi.hooks, { tag: "silgi" });
|
|
1291
|
+
silgi.options.plugins.push({
|
|
1292
|
+
path: join(runtimeDir, "internal/debug"),
|
|
1293
|
+
packageImport: "silgi/runtime/internal/debug"
|
|
1294
|
+
});
|
|
1295
|
+
}
|
|
1296
|
+
await scanAndSyncOptions(silgi);
|
|
1297
|
+
await scanModules$1(silgi);
|
|
1298
|
+
await scanFiles$1(silgi);
|
|
1299
|
+
await graphqlLoadSync(silgi);
|
|
1300
|
+
silgi.storage = await createStorageCLI(silgi);
|
|
1301
|
+
silgi.hooks.hook("close", async () => {
|
|
1302
|
+
await silgi.storage.dispose();
|
|
1303
|
+
});
|
|
1304
|
+
if (silgi.options.logLevel !== undefined) {
|
|
1305
|
+
silgi.logger.level = silgi.options.logLevel;
|
|
1306
|
+
}
|
|
1307
|
+
await installModules(silgi);
|
|
1308
|
+
if (silgi.options.imports) {
|
|
1309
|
+
silgi.unimport = createUnimport(silgi.options.imports);
|
|
1310
|
+
await silgi.unimport.init();
|
|
1311
|
+
}
|
|
1312
|
+
await afterModuleScan(silgi);
|
|
1313
|
+
return silgi;
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
const prepare = defineCommand({
|
|
1317
|
+
meta: {
|
|
1318
|
+
name: "prepare",
|
|
1319
|
+
description: "Generate types for the project",
|
|
1320
|
+
version: version
|
|
1321
|
+
},
|
|
1322
|
+
args: {
|
|
1323
|
+
...commonArgs,
|
|
1324
|
+
preset: {
|
|
1325
|
+
type: "string",
|
|
1326
|
+
description: "The build preset to use (you can also use `SILGI_PRESET` environment variable)."
|
|
1327
|
+
}
|
|
1328
|
+
},
|
|
1329
|
+
async run({ args }) {
|
|
1330
|
+
const rootDir = resolve(args.dir || args._dir || ".");
|
|
1331
|
+
const silgi = await createSilgiCLI({ rootDir });
|
|
1332
|
+
await prepare$1(silgi);
|
|
1333
|
+
for (const framework of frameworkSetup) {
|
|
1334
|
+
await framework(silgi);
|
|
1335
|
+
}
|
|
1336
|
+
await writeTypesAndFiles(silgi);
|
|
1337
|
+
await scanUris(silgi);
|
|
1338
|
+
await silgi.callHook("read:core.ts", async () => {
|
|
1339
|
+
const data = await readCoreFile(silgi);
|
|
1340
|
+
return data;
|
|
1341
|
+
});
|
|
1342
|
+
await silgi.close();
|
|
1343
|
+
}
|
|
1344
|
+
});
|
|
1345
|
+
|
|
1346
|
+
export { prepare as default };
|