silgi 0.24.15 → 0.24.17

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.
Files changed (91) hide show
  1. package/dist/cli/build/prepare.mjs +43 -0
  2. package/dist/cli/{dev.mjs → commands/dev.mjs} +4 -43
  3. package/dist/cli/commands/env.mjs +40 -0
  4. package/dist/cli/{install.mjs → commands/install.mjs} +3 -37
  5. package/dist/cli/commands/prepare.mjs +66 -0
  6. package/dist/cli/{prepare.mjs → commands/run.mjs} +9 -82
  7. package/dist/cli/config/defaults.mjs +129 -0
  8. package/dist/cli/config/index.mjs +2 -16
  9. package/dist/cli/config/loader.mjs +97 -0
  10. package/dist/cli/config/resolvers/compatibility.mjs +90 -0
  11. package/dist/cli/config/resolvers/imports.mjs +96 -0
  12. package/dist/cli/config/resolvers/paths.mjs +167 -0
  13. package/dist/cli/config/resolvers/storage.mjs +25 -0
  14. package/dist/cli/config/resolvers/url.mjs +7 -0
  15. package/dist/cli/config/types.mjs +191 -0
  16. package/dist/cli/core/app.mjs +92 -0
  17. package/dist/cli/core/devServer.mjs +8 -0
  18. package/dist/cli/core/env.mjs +76 -0
  19. package/dist/cli/core/installPackage.mjs +31 -0
  20. package/dist/cli/core/prepare.mjs +7 -0
  21. package/dist/cli/core/runtimeConfig.mjs +14 -0
  22. package/dist/cli/core/scan.mjs +39 -0
  23. package/dist/cli/core/silgi.mjs +125 -0
  24. package/dist/cli/core/templates.mjs +31 -0
  25. package/dist/cli/framework/emptyFramework.mjs +9 -0
  26. package/dist/cli/framework/h3.mjs +89 -0
  27. package/dist/cli/framework/index.mjs +8 -0
  28. package/dist/cli/framework/nitro.mjs +106 -0
  29. package/dist/cli/framework/nuxt.mjs +9 -0
  30. package/dist/cli/index.mjs +6 -9
  31. package/dist/cli/module/exportScan.mjs +130 -0
  32. package/dist/cli/module/install.mjs +59 -0
  33. package/dist/cli/module/scan.mjs +227 -0
  34. package/dist/cli/scan/commands.mjs +47 -0
  35. package/dist/cli/scan/prepareConfigs.mjs +46 -0
  36. package/dist/cli/scan/prepareCoreFile.mjs +166 -0
  37. package/dist/cli/scan/prepareFramework.mjs +73 -0
  38. package/dist/cli/scan/prepareSchema.mjs +177 -0
  39. package/dist/cli/scan/prepareServerFiles.mjs +119 -0
  40. package/dist/cli/scan/scanExportFile.mjs +155 -0
  41. package/dist/cli/scan/writeCoreFile.mjs +34 -0
  42. package/dist/cli/scan/writeScanFiles.mjs +30 -0
  43. package/dist/cli/scan/writeTypesAndFiles.mjs +109 -0
  44. package/dist/cli/utils/build-uri-map.mjs +39 -0
  45. package/dist/cli/utils/common.mjs +13 -0
  46. package/dist/cli/{compatibility.mjs → utils/compatibility.mjs} +1 -1
  47. package/dist/cli/utils/generateRouterDTS.mjs +80 -0
  48. package/dist/cli/utils/ignore.mjs +46 -0
  49. package/dist/cli/utils/parser.mjs +168 -0
  50. package/dist/cli/utils/readScanFile.mjs +79 -0
  51. package/dist/cli/utils/storage.mjs +25 -0
  52. package/dist/core/createSilgi.mjs +76 -0
  53. package/dist/core/error.mjs +227 -0
  54. package/dist/core/index.mjs +8 -0
  55. package/dist/core/routeRules.mjs +288 -0
  56. package/dist/core/silgi.mjs +128 -0
  57. package/dist/core/silgiApp.mjs +25 -0
  58. package/dist/core/storage.mjs +15 -0
  59. package/dist/core/unctx.mjs +27 -0
  60. package/dist/core/uris/uri.mjs +33 -0
  61. package/dist/core/uris/utils.mjs +130 -0
  62. package/dist/core/utils/event.mjs +15 -0
  63. package/dist/core/utils/merge.mjs +29 -0
  64. package/dist/core/utils/runtime.mjs +22 -0
  65. package/dist/core/utils/schema.mjs +5 -0
  66. package/dist/core/utils/service.mjs +5 -0
  67. package/dist/core/utils/shared.mjs +5 -0
  68. package/dist/core/utils/storage.mjs +72 -0
  69. package/dist/index.mjs +20 -793
  70. package/dist/kit/errors.mjs +11 -0
  71. package/dist/kit/esm.mjs +14 -0
  72. package/dist/kit/fs.mjs +30 -0
  73. package/dist/kit/gen.mjs +44 -0
  74. package/dist/kit/hash.mjs +8 -0
  75. package/dist/kit/index.mjs +15 -528
  76. package/dist/kit/isFramework.mjs +25 -0
  77. package/dist/kit/logger.mjs +7 -0
  78. package/dist/kit/module.mjs +73 -0
  79. package/dist/kit/parseServices.mjs +57 -0
  80. package/dist/kit/path.mjs +34 -0
  81. package/dist/kit/preset.mjs +6 -0
  82. package/dist/kit/resolve.mjs +80 -0
  83. package/dist/kit/template.mjs +49 -0
  84. package/dist/kit/useRequest.mjs +91 -0
  85. package/dist/kit/utils.mjs +43 -0
  86. package/dist/package.json.mjs +5 -0
  87. package/dist/types/index.d.mts +1 -1
  88. package/package.json +1 -1
  89. package/dist/_chunks/silgiApp.mjs +0 -2682
  90. package/dist/cli/types.mjs +0 -772
  91. /package/dist/cli/{init.mjs → commands/init.mjs} +0 -0
@@ -0,0 +1,59 @@
1
+ import { createJiti } from 'dev-jiti';
2
+ import { useSilgiCLI } from 'silgi';
3
+ import { hasInstalledModule } from '../utils/compatibility.mjs';
4
+
5
+ async function loadSilgiModuleInstance(silgiModule) {
6
+ if (typeof silgiModule === "string") {
7
+ throw new TypeError(`Could not load \`${silgiModule}\`. Is it installed?`);
8
+ }
9
+ if (typeof silgiModule !== "function") {
10
+ throw new TypeError(`Nuxt module should be a function: ${silgiModule}`);
11
+ }
12
+ return { silgiModule };
13
+ }
14
+ async function installModules(silgi, prepare = false) {
15
+ silgi.options.isPreparingModules = prepare;
16
+ const jiti = createJiti(silgi.options.rootDir, {
17
+ alias: silgi.options.alias,
18
+ fsCache: true,
19
+ moduleCache: true
20
+ });
21
+ for (const module of silgi.scanModules) {
22
+ if (hasInstalledModule(module.meta.configKey) && !silgi.options.dev) {
23
+ silgi.logger.info(`Module ${module.meta.configKey} installed`);
24
+ }
25
+ try {
26
+ const silgiModule = module.entryPath !== void 0 ? await jiti.import(module.entryPath, {
27
+ default: true,
28
+ conditions: silgi.options.conditions
29
+ }) : module.module;
30
+ if (silgiModule.name !== "silgiNormalizedModule") {
31
+ silgi.scanModules = silgi.scanModules.filter((m) => m.entryPath !== module.entryPath);
32
+ continue;
33
+ }
34
+ await installModule(silgiModule, silgi, prepare);
35
+ } catch (err) {
36
+ silgi.logger.error(err);
37
+ }
38
+ }
39
+ silgi.options.isPreparingModules = false;
40
+ }
41
+ async function installModule(moduleToInstall, silgi = useSilgiCLI(), inlineOptions, prepare = false) {
42
+ const { silgiModule } = await loadSilgiModuleInstance(moduleToInstall);
43
+ const res = await silgiModule(inlineOptions || {}, silgi) ?? {};
44
+ if (res === false) {
45
+ return false;
46
+ }
47
+ const metaData = await silgiModule.getMeta?.();
48
+ if (prepare) {
49
+ return metaData;
50
+ }
51
+ const installedModule = silgi.scanModules.find((m) => m.meta.configKey === metaData?.configKey);
52
+ if (installedModule) {
53
+ installedModule.installed = true;
54
+ } else {
55
+ throw new Error(`Module ${metaData?.name} not found`);
56
+ }
57
+ }
58
+
59
+ export { installModules, loadSilgiModuleInstance };
@@ -0,0 +1,227 @@
1
+ import { existsSync, promises } from 'node:fs';
2
+ import { fileURLToPath } from 'node:url';
3
+ import 'consola';
4
+ import defu from 'defu';
5
+ import { createJiti } from 'dev-jiti';
6
+ import { resolveModuleURL } from 'exsolve';
7
+ import { resolve } from 'pathe';
8
+ import { resolveAlias, directoryToURL } from 'silgi/kit';
9
+ import { isRelative } from 'ufo';
10
+ import { scanExports } from 'unimport';
11
+
12
+ const MissingModuleMatcher = /Cannot find module\s+['"]?([^'")\s]+)['"]?/i;
13
+ async function _resolveSilgiModule(silgiModule, silgi) {
14
+ let resolvedModulePath;
15
+ let buildTimeModuleMeta = {};
16
+ const jiti = createJiti(silgi.options.rootDir, {
17
+ alias: silgi.options.alias,
18
+ fsCache: true,
19
+ moduleCache: true
20
+ });
21
+ if (typeof silgiModule === "string") {
22
+ silgiModule = resolveAlias(silgiModule, silgi.options.alias);
23
+ if (isRelative(silgiModule)) {
24
+ silgiModule = resolve(silgi.options.rootDir, silgiModule);
25
+ }
26
+ try {
27
+ const src = resolveModuleURL(silgiModule, {
28
+ from: silgi.options.modulesDir.map((m) => directoryToURL(m.replace(/\/node_modules\/?$/, "/"))),
29
+ suffixes: ["silgi", "silgi/index", "module", "module/index", "", "index"],
30
+ extensions: [".js", ".mjs", ".cjs", ".ts", ".mts", ".cts"]
31
+ // Maybe add https://github.com/unjs/exsolve/blob/dfff3e9bbc4a3a173a2d56b9b9ff731ab15598be/src/resolve.ts#L7
32
+ // conditions: silgi.options.conditions,
33
+ });
34
+ resolvedModulePath = fileURLToPath(src);
35
+ const resolvedSilgiModule = await jiti.import(src, { default: true });
36
+ if (typeof resolvedSilgiModule !== "function") {
37
+ throw new TypeError(`Nuxt module should be a function: ${silgiModule}.`);
38
+ }
39
+ silgiModule = await jiti.import(src, {
40
+ default: true,
41
+ conditions: silgi.options.conditions
42
+ });
43
+ const moduleMetadataPath = new URL("module.json", src);
44
+ if (existsSync(moduleMetadataPath)) {
45
+ buildTimeModuleMeta = JSON.parse(await promises.readFile(moduleMetadataPath, "utf-8"));
46
+ } else {
47
+ if (typeof silgiModule === "function") {
48
+ const meta = await silgiModule.getMeta?.();
49
+ const _exports = await scanExports(resolvedModulePath, true);
50
+ buildTimeModuleMeta = {
51
+ ...meta,
52
+ exports: _exports.map(({ from, ...rest }) => rest)
53
+ };
54
+ }
55
+ }
56
+ } catch (error) {
57
+ const code = error.code;
58
+ if (code === "MODULE_NOT_FOUND" || code === "ERR_PACKAGE_PATH_NOT_EXPORTED" || code === "ERR_MODULE_NOT_FOUND" || code === "ERR_UNSUPPORTED_DIR_IMPORT" || code === "ENOTDIR") {
59
+ throw new TypeError(`Could not load \`${silgiModule}\`. Is it installed?`);
60
+ }
61
+ if (code === "MODULE_NOT_FOUND" || code === "ERR_MODULE_NOT_FOUND") {
62
+ const module = MissingModuleMatcher.exec(error.message)?.[1];
63
+ if (module && !module.includes(silgiModule)) {
64
+ throw new TypeError(`Error while importing module \`${silgiModule}\`: ${error}`);
65
+ }
66
+ }
67
+ }
68
+ }
69
+ if (!buildTimeModuleMeta) {
70
+ throw new Error(`Module ${silgiModule} is not a valid Silgi module`);
71
+ }
72
+ if (typeof silgiModule === "function") {
73
+ if (!buildTimeModuleMeta.configKey) {
74
+ const meta = await silgiModule.getMeta?.();
75
+ buildTimeModuleMeta = {
76
+ ...meta,
77
+ exports: []
78
+ };
79
+ }
80
+ if (silgi.scanModules.some((m) => m.meta?.configKey === buildTimeModuleMeta.configKey)) {
81
+ throw new Error(`Module with key \`${buildTimeModuleMeta.configKey}\` already exists`);
82
+ }
83
+ const options = await silgiModule.getOptions?.() || {};
84
+ if (options) {
85
+ silgi.options._c12.config[buildTimeModuleMeta.configKey] = defu(
86
+ silgi.options._c12.config[buildTimeModuleMeta.configKey] || {},
87
+ options || {}
88
+ );
89
+ } else {
90
+ throw new TypeError(`Could not load \`${silgiModule}\`. Is it installed?`);
91
+ }
92
+ silgi.scanModules.push({
93
+ meta: buildTimeModuleMeta,
94
+ entryPath: resolvedModulePath || void 0,
95
+ installed: false,
96
+ options,
97
+ module: silgiModule
98
+ });
99
+ }
100
+ }
101
+ async function scanModules(silgi) {
102
+ const _modules = [
103
+ ...silgi.options._modules,
104
+ ...silgi.options.modules
105
+ ];
106
+ for await (const mod of _modules) {
107
+ await _resolveSilgiModule(mod, silgi);
108
+ }
109
+ const moduleMap = new Map(
110
+ silgi.scanModules.map((m) => [m.meta?.configKey, m])
111
+ );
112
+ const graphData = createDependencyGraph(silgi.scanModules);
113
+ const sortedKeys = topologicalSort(graphData);
114
+ const modules = sortedKeys.map((key) => moduleMap.get(key)).filter((module) => Boolean(module));
115
+ silgi.scanModules = modules;
116
+ }
117
+ function createDependencyGraph(modules) {
118
+ const graph = /* @__PURE__ */ new Map();
119
+ const inDegree = /* @__PURE__ */ new Map();
120
+ modules.forEach((module) => {
121
+ const key = module.meta?.configKey;
122
+ if (key) {
123
+ graph.set(key, /* @__PURE__ */ new Set());
124
+ inDegree.set(key, 0);
125
+ }
126
+ });
127
+ modules.forEach((module) => {
128
+ const key = module.meta?.configKey;
129
+ if (!key) {
130
+ return;
131
+ }
132
+ const requiredDeps = module.meta?.requiredDependencies || [];
133
+ const beforeDeps = module.meta?.beforeDependencies || [];
134
+ const afterDeps = module.meta?.afterDependencies || [];
135
+ const processedDeps = /* @__PURE__ */ new Set();
136
+ requiredDeps.forEach((dep) => {
137
+ if (!graph.has(dep)) {
138
+ throw new Error(`Required dependency "${dep}" for module "${key}" is missing`);
139
+ }
140
+ graph.get(dep)?.add(key);
141
+ inDegree.set(key, (inDegree.get(key) || 0) + 1);
142
+ processedDeps.add(dep);
143
+ });
144
+ beforeDeps.forEach((dep) => {
145
+ if (!graph.has(dep)) {
146
+ return;
147
+ }
148
+ graph.get(key)?.add(dep);
149
+ inDegree.set(dep, (inDegree.get(dep) || 0) + 1);
150
+ });
151
+ afterDeps.forEach((dep) => {
152
+ if (processedDeps.has(dep)) {
153
+ return;
154
+ }
155
+ if (!graph.has(dep)) {
156
+ return;
157
+ }
158
+ graph.get(dep)?.add(key);
159
+ inDegree.set(key, (inDegree.get(key) || 0) + 1);
160
+ });
161
+ });
162
+ return { graph, inDegree };
163
+ }
164
+ function findCyclicDependencies(graph) {
165
+ const visited = /* @__PURE__ */ new Set();
166
+ const recursionStack = /* @__PURE__ */ new Set();
167
+ const cycles = [];
168
+ function dfs(node, path = []) {
169
+ visited.add(node);
170
+ recursionStack.add(node);
171
+ path.push(node);
172
+ for (const neighbor of graph.get(node) || []) {
173
+ if (recursionStack.has(neighbor)) {
174
+ const cycleStart = path.indexOf(neighbor);
175
+ if (cycleStart !== -1) {
176
+ cycles.push([...path.slice(cycleStart), neighbor]);
177
+ }
178
+ } else if (!visited.has(neighbor)) {
179
+ dfs(neighbor, [...path]);
180
+ }
181
+ }
182
+ recursionStack.delete(node);
183
+ path.pop();
184
+ }
185
+ for (const node of graph.keys()) {
186
+ if (!visited.has(node)) {
187
+ dfs(node, []);
188
+ }
189
+ }
190
+ return cycles;
191
+ }
192
+ function topologicalSort(graphData) {
193
+ const { graph, inDegree } = graphData;
194
+ const order = [];
195
+ const queue = [];
196
+ for (const [node, degree] of inDegree.entries()) {
197
+ if (degree === 0) {
198
+ queue.push(node);
199
+ }
200
+ }
201
+ while (queue.length > 0) {
202
+ const node = queue.shift();
203
+ order.push(node);
204
+ const neighbors = Array.from(graph.get(node) || []);
205
+ for (const neighbor of neighbors) {
206
+ const newDegree = (inDegree.get(neighbor) || 0) - 1;
207
+ inDegree.set(neighbor, newDegree);
208
+ if (newDegree === 0) {
209
+ queue.push(neighbor);
210
+ }
211
+ }
212
+ }
213
+ if (order.length !== graph.size) {
214
+ const cycles = findCyclicDependencies(graph);
215
+ if (cycles.length > 0) {
216
+ const cycleStr = cycles.map((cycle) => ` ${cycle.join(" -> ")}`).join("\n");
217
+ throw new Error(`Circular dependencies detected:
218
+ ${cycleStr}`);
219
+ } else {
220
+ const unresolvedModules = Array.from(graph.keys()).filter((key) => !order.includes(key));
221
+ throw new Error(`Unable to resolve dependencies for modules: ${unresolvedModules.join(", ")}`);
222
+ }
223
+ }
224
+ return order;
225
+ }
226
+
227
+ export { scanModules };
@@ -0,0 +1,47 @@
1
+ import { addTemplate } from 'silgi/kit';
2
+ import { generateTypes, resolveSchema } from 'untyped';
3
+
4
+ async function commands(silgi) {
5
+ const commands2 = {
6
+ ...silgi.options.commands
7
+ };
8
+ await silgi.callHook("prepare:commands", commands2);
9
+ addTemplate({
10
+ filename: "cli.json",
11
+ where: ".silgi",
12
+ write: true,
13
+ getContents: () => JSON.stringify(commands2, null, 2)
14
+ });
15
+ silgi.commands = commands2;
16
+ silgi.hook("prepare:schema.ts", async (object) => {
17
+ const allTags = Object.values(commands2).reduce((acc, commandGroup) => {
18
+ Object.values(commandGroup).forEach((command) => {
19
+ if (command.tags) {
20
+ command.tags.forEach((tag) => acc.add(tag));
21
+ }
22
+ });
23
+ return acc;
24
+ }, /* @__PURE__ */ new Set());
25
+ const data = [
26
+ "",
27
+ generateTypes(
28
+ await resolveSchema(
29
+ {
30
+ ...Object.fromEntries(Array.from(allTags.values()).map((tag) => [tag, "string"]))
31
+ }
32
+ ),
33
+ {
34
+ interfaceName: "SilgiCommandsExtended",
35
+ addExport: false,
36
+ addDefaults: false,
37
+ allowExtraKeys: false,
38
+ indentation: 0
39
+ }
40
+ ),
41
+ ""
42
+ ];
43
+ object.customImports?.push(...data);
44
+ });
45
+ }
46
+
47
+ export { commands };
@@ -0,0 +1,46 @@
1
+ import { genObjectFromRawEntries } from 'knitwork';
2
+ import { genEnsureSafeVar } from 'silgi/kit';
3
+
4
+ async function prepareConfigs(silgi) {
5
+ const _data = {
6
+ runtimeConfig: {}
7
+ };
8
+ for (const module of silgi.scanModules) {
9
+ if (module.meta.cliToRuntimeOptionsKeys && module.meta.cliToRuntimeOptionsKeys?.length > 0) {
10
+ for (const key of module.meta.cliToRuntimeOptionsKeys) {
11
+ _data[module.meta.configKey] = {
12
+ ..._data[module.meta.configKey],
13
+ [key]: module.options[key]
14
+ };
15
+ }
16
+ } else {
17
+ _data[module.meta.configKey] = {};
18
+ }
19
+ }
20
+ await silgi.callHook("prepare:configs.ts", _data);
21
+ const importData = [
22
+ "import type { SilgiRuntimeOptions, SilgiRuntimeConfig, SilgiOptions } from 'silgi/types'",
23
+ "import { useSilgiRuntimeConfig } from 'silgi/runtime'",
24
+ "",
25
+ `export const runtimeConfig: Partial<SilgiRuntimeConfig> = ${genObjectFromRawEntries(
26
+ Object.entries(_data.runtimeConfig).map(([key, value]) => [key, genEnsureSafeVar(value)]),
27
+ ""
28
+ )}`,
29
+ "",
30
+ "const runtime = useSilgiRuntimeConfig(undefined, runtimeConfig)",
31
+ ""
32
+ ];
33
+ delete _data.runtimeConfig;
34
+ importData.push(`export const cliConfigs: Partial<SilgiRuntimeOptions & SilgiOptions> = ${genObjectFromRawEntries(
35
+ Object.entries(_data).map(
36
+ ([key, value]) => [key, genEnsureSafeVar(value)]
37
+ ).concat(
38
+ [
39
+ ["runtimeConfig", "runtime"]
40
+ ]
41
+ )
42
+ )}`);
43
+ return importData;
44
+ }
45
+
46
+ export { prepareConfigs };
@@ -0,0 +1,166 @@
1
+ import { hash } from 'silgi/kit';
2
+
3
+ async function prepareCoreFile(data, frameworkContext, silgi) {
4
+ let importItems = {
5
+ "silgi": {
6
+ import: [
7
+ {
8
+ name: "createSilgi",
9
+ key: "createSilgi"
10
+ }
11
+ ],
12
+ from: "silgi"
13
+ },
14
+ "silgi/types": {
15
+ import: [
16
+ {
17
+ name: "SilgiRuntimeOptions",
18
+ type: true,
19
+ key: "SilgiRuntimeOptions"
20
+ },
21
+ {
22
+ name: "FrameworkContext",
23
+ type: true,
24
+ key: "FrameworkContext"
25
+ },
26
+ {
27
+ name: "SilgiOptions",
28
+ type: true,
29
+ key: "SilgiOptions"
30
+ }
31
+ ],
32
+ from: "silgi/types"
33
+ },
34
+ "#silgi/vfs": {
35
+ import: [],
36
+ from: "./vfs"
37
+ },
38
+ "scan.ts": {
39
+ import: [
40
+ {
41
+ name: "uris",
42
+ type: false,
43
+ key: "uris"
44
+ },
45
+ {
46
+ name: "services",
47
+ type: false,
48
+ key: "services"
49
+ },
50
+ {
51
+ name: "shareds",
52
+ type: false,
53
+ key: "shareds"
54
+ },
55
+ {
56
+ name: "schemas",
57
+ type: false,
58
+ key: "schemas"
59
+ },
60
+ {
61
+ name: "modulesURIs",
62
+ type: false,
63
+ key: "modulesURIs"
64
+ }
65
+ ],
66
+ from: "./scan.ts"
67
+ },
68
+ "configs.ts": {
69
+ import: [
70
+ {
71
+ name: "cliConfigs",
72
+ type: false,
73
+ key: "cliConfigs"
74
+ }
75
+ ],
76
+ from: "./configs.ts"
77
+ },
78
+ "rules.ts": {
79
+ import: [
80
+ {
81
+ name: "routeRules",
82
+ key: "routeRules"
83
+ }
84
+ ],
85
+ from: "./rules.ts"
86
+ }
87
+ };
88
+ importItems = { ...data._importItems, ...importItems };
89
+ const _data = {
90
+ customImports: data._customImports || [],
91
+ buildSilgiExtraContent: [],
92
+ beforeBuildSilgiExtraContent: [],
93
+ afterCliOptions: [],
94
+ _silgiConfigs: [],
95
+ customContent: [],
96
+ importItems
97
+ };
98
+ await silgi.callHook("prepare:core.ts", _data);
99
+ if (importItems["#silgi/vfs"].import.length === 0) {
100
+ delete importItems["#silgi/vfs"];
101
+ }
102
+ const plugins = [];
103
+ for (const plugin of silgi.options.plugins) {
104
+ const pluginImportName = `_${hash(plugin.packageImport)}`;
105
+ _data.customImports.push(`import ${pluginImportName} from '${plugin.packageImport}'`);
106
+ plugins.push(pluginImportName);
107
+ }
108
+ const importsContent = [
109
+ 'import deepmerge from "@fastify/deepmerge"',
110
+ ...Object.entries(importItems).map(([_name, { from, import: imports }]) => {
111
+ if (silgi.options.typescript.removeFileExtension) {
112
+ from = from.replace(/\.(js|ts|mjs|cjs|jsx|tsx)$/, "");
113
+ }
114
+ return `import { ${imports.map(({ type, name }) => type ? `type ${name}` : name).join(", ")} } from '${from}'`;
115
+ }),
116
+ "",
117
+ ..._data.customImports,
118
+ ""
119
+ ];
120
+ const importData = [
121
+ "",
122
+ "const mergeDeep = deepmerge({all: true})",
123
+ "",
124
+ "export async function buildSilgi(framework: FrameworkContext, moduleOptions?: Partial<SilgiRuntimeOptions>,buildOptions?: Partial<SilgiOptions>) {",
125
+ "",
126
+ _data.beforeBuildSilgiExtraContent.length > 0 ? _data.beforeBuildSilgiExtraContent.map(({ value, type }) => {
127
+ return type === "function" ? value : `const ${value}`;
128
+ }) : "",
129
+ "",
130
+ " const silgi = await createSilgi({",
131
+ " framework,",
132
+ " shared: shareds as any,",
133
+ " services: services as any,",
134
+ " schemas: schemas as any,",
135
+ " uris,",
136
+ " modulesURIs,",
137
+ ` plugins: [${plugins.join(", ")}],`,
138
+ _data._silgiConfigs.length > 0 ? ` ${_data._silgiConfigs.map((config) => typeof config === "string" ? config : typeof config === "object" ? Object.entries(config).map(([key, value]) => `${key}: ${value}`).join(",\n ") : "").join(",\n ")},` : "",
139
+ " options: mergeDeep(",
140
+ " {",
141
+ " runtimeConfig: {} as SilgiRuntimeOptions,",
142
+ " routeRules: routeRules as any,",
143
+ " },",
144
+ " moduleOptions || {},",
145
+ " {",
146
+ ` present: '${silgi.options.preset}',`,
147
+ " ...cliConfigs,",
148
+ " },",
149
+ " buildOptions,",
150
+ " ) as any,",
151
+ " })",
152
+ "",
153
+ ...frameworkContext,
154
+ "",
155
+ ..._data.buildSilgiExtraContent,
156
+ "",
157
+ " return silgi",
158
+ "}",
159
+ ""
160
+ ];
161
+ await silgi.callHook("after:prepare:core.ts", importData);
162
+ importData.unshift(...importsContent);
163
+ return importData;
164
+ }
165
+
166
+ export { prepareCoreFile };
@@ -0,0 +1,73 @@
1
+ import { isAbsolute } from 'pathe';
2
+ import { relativeWithDot } from 'silgi/kit';
3
+
4
+ async function prepareFramework(silgi) {
5
+ const importItems = {
6
+ "silgi/types": {
7
+ import: [
8
+ {
9
+ name: "SilgiRuntimeContext",
10
+ type: true,
11
+ key: "SilgiRuntimeContext"
12
+ }
13
+ ],
14
+ from: "silgi/types"
15
+ }
16
+ };
17
+ const customImports = [];
18
+ const functions = [];
19
+ await silgi.callHook("prepare:createCoreFramework", {
20
+ importItems,
21
+ customImports,
22
+ functions
23
+ });
24
+ const content = [
25
+ ...functions.map((f) => f.params?.length ? ` await ${f.name}(framework, ${f.params.join(",")})` : ` await ${f.name}(framework)`)
26
+ ];
27
+ return {
28
+ content,
29
+ importItems,
30
+ customImports
31
+ };
32
+ }
33
+ async function createDTSFramework(silgi) {
34
+ const importItems = {
35
+ "silgi/types": {
36
+ import: [
37
+ {
38
+ name: "SilgiRuntimeContext",
39
+ type: true,
40
+ key: "SilgiRuntimeContext"
41
+ }
42
+ ],
43
+ from: "silgi/types"
44
+ }
45
+ };
46
+ const customImports = [];
47
+ const customContent = [];
48
+ await silgi.callHook("prepare:createDTSFramework", {
49
+ importItems,
50
+ customImports,
51
+ customContent
52
+ });
53
+ const content = [
54
+ ...Object.entries(importItems).map(([_name, { from, import: imports }]) => {
55
+ const path = isAbsolute(from) ? relativeWithDot(silgi.options.build.typesDir, from) : from;
56
+ if (silgi.options.typescript.removeFileExtension) {
57
+ from = from.replace(/\.(js|ts|mjs|cjs|jsx|tsx)$/, "");
58
+ }
59
+ return `import { ${imports.map(({ type, name }) => type ? `type ${name}` : name).join(", ")} } from '${path}'`;
60
+ }),
61
+ "",
62
+ ...customImports,
63
+ "",
64
+ ...customContent,
65
+ ""
66
+ ];
67
+ return {
68
+ content,
69
+ importItems
70
+ };
71
+ }
72
+
73
+ export { createDTSFramework, prepareFramework };