silgi 0.7.0 → 0.7.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.
Files changed (77) hide show
  1. package/dist/cli/build/framework/h3.mjs +46 -0
  2. package/dist/cli/build/framework/index.mjs +7 -0
  3. package/dist/cli/build/framework/nitro.mjs +28 -0
  4. package/dist/cli/build/framework/nuxt.mjs +9 -0
  5. package/dist/cli/build/prepare.mjs +7 -0
  6. package/dist/cli/build/scanURIs.mjs +27 -0
  7. package/dist/cli/build/template/framework.mjs +91 -0
  8. package/dist/cli/build/template/schema.mjs +115 -0
  9. package/dist/cli/build/template/silgi.mjs +149 -0
  10. package/dist/cli/build/types.mjs +130 -0
  11. package/dist/cli/commands/prepare.mjs +49 -0
  12. package/dist/cli/common.mjs +13 -0
  13. package/dist/cli/core/app.mjs +89 -0
  14. package/dist/cli/core/scan.mjs +40 -0
  15. package/dist/cli/core/silgi.mjs +77 -0
  16. package/dist/cli/core/storage.mjs +11 -0
  17. package/dist/cli/core/templates.mjs +29 -0
  18. package/dist/cli/index.mjs +3 -3
  19. package/dist/cli/module/exportScan.mjs +69 -0
  20. package/dist/cli/module/install.mjs +52 -0
  21. package/dist/cli/module/scan.mjs +141 -0
  22. package/dist/cli/{compatibility.mjs → utils/compatibility.mjs} +1 -1
  23. package/dist/cli/utils/generateRouterDTS.mjs +84 -0
  24. package/dist/cli/utils/ignore.mjs +46 -0
  25. package/dist/cli/utils/readCoreFile.mjs +47 -0
  26. package/dist/cli/utils/scan.mjs +147 -0
  27. package/dist/cli/utils/storage.mjs +21 -0
  28. package/dist/cli/utils/uri.mjs +71 -0
  29. package/dist/core/config/defaults.mjs +96 -0
  30. package/dist/core/config/loader.mjs +98 -0
  31. package/dist/core/config/resolvers/compatibility.mjs +90 -0
  32. package/dist/core/config/resolvers/imports.mjs +96 -0
  33. package/dist/core/config/resolvers/paths.mjs +194 -0
  34. package/dist/core/config/resolvers/storage.mjs +25 -0
  35. package/dist/core/config/resolvers/url.mjs +7 -0
  36. package/dist/core/config/types.mjs +160 -0
  37. package/dist/core/createSilgi.mjs +84 -0
  38. package/dist/core/error.mjs +227 -0
  39. package/dist/core/fetch/ofetch.mjs +35 -0
  40. package/dist/core/index.mjs +16 -1678
  41. package/dist/core/parser.mjs +136 -0
  42. package/dist/core/silgi.mjs +114 -0
  43. package/dist/core/silgiApp.mjs +15 -0
  44. package/dist/core/unctx.mjs +27 -0
  45. package/dist/core/uris/uri.mjs +33 -0
  46. package/dist/core/uris/utils.mjs +127 -0
  47. package/dist/core/utils/event.mjs +5 -0
  48. package/dist/core/utils/global.mjs +12 -0
  49. package/dist/core/utils/merge.mjs +25 -0
  50. package/dist/core/utils/schema.mjs +5 -0
  51. package/dist/core/utils/service.mjs +5 -0
  52. package/dist/core/utils/shared.mjs +5 -0
  53. package/dist/core/utils/storage.mjs +70 -0
  54. package/dist/ecosystem/nitro/index.mjs +1 -62
  55. package/dist/ecosystem/nitro/module.mjs +62 -0
  56. package/dist/kit/esm.mjs +10 -0
  57. package/dist/kit/fs.mjs +25 -0
  58. package/dist/kit/index.mjs +10 -299
  59. package/dist/kit/isFramework.mjs +25 -0
  60. package/dist/kit/logger.mjs +8 -0
  61. package/dist/kit/module.mjs +73 -0
  62. package/dist/kit/path.mjs +34 -0
  63. package/dist/kit/preset.mjs +6 -0
  64. package/dist/kit/resolve.mjs +78 -0
  65. package/dist/kit/template.mjs +47 -0
  66. package/dist/kit/utils.mjs +20 -0
  67. package/dist/meta/index.d.mts +1 -1
  68. package/dist/meta/index.d.ts +1 -1
  69. package/dist/meta/index.mjs +1 -1
  70. package/dist/package.json.mjs +5 -0
  71. package/dist/schema/common.mjs +43 -0
  72. package/dist/schema/index.mjs +9 -0
  73. package/dist/schema/internal.mjs +22 -0
  74. package/package.json +1 -1
  75. package/dist/_chunks/index.mjs +0 -5
  76. package/dist/cli/prepare.mjs +0 -1481
  77. /package/dist/cli/{init.mjs → commands/init.mjs} +0 -0
@@ -0,0 +1,46 @@
1
+ async function h3Framework(silgi, skip = false) {
2
+ if (silgi.options.preset !== "h3" && skip === false)
3
+ return;
4
+ if (silgi.options.preset === "h3") {
5
+ silgi.hook("after:prepare:schema.ts", (data) => {
6
+ data.unshift("type FrameworkContextExtends = NitroApp");
7
+ });
8
+ }
9
+ silgi.hook("prepare:schema.ts", (data) => {
10
+ data.importItems.nitropack = {
11
+ import: [
12
+ { name: "NitroApp", type: true }
13
+ ],
14
+ from: "nitropack/types"
15
+ };
16
+ data.importItems.h3 = {
17
+ import: [
18
+ { name: "H3Event", type: true }
19
+ ],
20
+ from: "h3"
21
+ };
22
+ data.events.push({
23
+ key: "H3Event",
24
+ value: "H3Event",
25
+ extends: true,
26
+ isSilgiContext: false
27
+ });
28
+ });
29
+ silgi.hook("prepare:createDTSFramework", (data) => {
30
+ data.importItems["silgi/types"] = {
31
+ import: [
32
+ { name: "ModuleRuntimeContext", type: true }
33
+ ],
34
+ from: "silgi/types"
35
+ };
36
+ data.customContent?.push(
37
+ "",
38
+ 'declare module "h3" {',
39
+ " interface H3EventContext extends ModuleRuntimeContext {}",
40
+ "}",
41
+ ""
42
+ );
43
+ });
44
+ }
45
+
46
+ export { h3Framework };
@@ -0,0 +1,7 @@
1
+ import { h3Framework } from './h3.mjs';
2
+ import { nitroFramework } from './nitro.mjs';
3
+ import { nuxtFramework } from './nuxt.mjs';
4
+
5
+ const frameworkSetup = [h3Framework, nitroFramework, nuxtFramework];
6
+
7
+ export { frameworkSetup };
@@ -0,0 +1,28 @@
1
+ import { join } from 'pathe';
2
+ import { runtimeDir } from 'silgi/runtime/meta';
3
+ import { h3Framework } from './h3.mjs';
4
+
5
+ async function nitroFramework(silgi, skip = false) {
6
+ if (silgi.options.preset !== "nitro" && skip === false)
7
+ return;
8
+ silgi.hook("prepare:schema.ts", (data) => {
9
+ data.importItems.nitropack = {
10
+ import: [
11
+ { name: "NitroApp", type: true }
12
+ ],
13
+ from: "nitropack/types"
14
+ };
15
+ });
16
+ silgi.hook("after:prepare:schema.ts", (data) => {
17
+ data.unshift("type FrameworkContextExtends = NitroApp");
18
+ });
19
+ silgi.options.plugins.push({
20
+ packageImport: "silgi/runtime/internal/nitro",
21
+ path: join(runtimeDir, "internal/nitro")
22
+ });
23
+ silgi.hook("prepare:core.ts", (_data) => {
24
+ });
25
+ await h3Framework(silgi, true);
26
+ }
27
+
28
+ export { nitroFramework };
@@ -0,0 +1,9 @@
1
+ import { nitroFramework } from './nitro.mjs';
2
+
3
+ async function nuxtFramework(silgi, skip = false) {
4
+ if (silgi.options.preset !== "nuxt" && skip === false)
5
+ return;
6
+ await nitroFramework(silgi, true);
7
+ }
8
+
9
+ export { nuxtFramework };
@@ -0,0 +1,7 @@
1
+ import 'node:fs';
2
+ import 'node:fs/promises';
3
+
4
+ async function prepare(_silgi) {
5
+ }
6
+
7
+ export { prepare };
@@ -0,0 +1,27 @@
1
+ import { promises } from 'node:fs';
2
+ import { readCoreFile } from '../utils/readCoreFile.mjs';
3
+ import { traverseObject, scanActionModulesUris } from '../utils/uri.mjs';
4
+
5
+ async function scanUris(silgi) {
6
+ const { context, object, path } = await readCoreFile(silgi);
7
+ const uriMap = traverseObject(silgi, object.schemas, []);
8
+ const modulesURIs = scanActionModulesUris(silgi, object.services, []);
9
+ const uriContent = Array.from(uriMap.entries()).map(([uri, params]) => ` '${uri}': '${params}',`).join("\n");
10
+ let newContext = "";
11
+ if (uriMap.size > 0) {
12
+ newContext = context.replace(
13
+ /export const uris = \{[^}]*\}/,
14
+ `export const uris = {
15
+ ${uriContent}
16
+ }`
17
+ ).replace(
18
+ /export const modulesURIs = \{[^}]*\}/,
19
+ `export const modulesURIs = ${JSON.stringify(modulesURIs, null, 2)}`
20
+ );
21
+ } else {
22
+ newContext = context;
23
+ }
24
+ await promises.writeFile(path, newContext);
25
+ }
26
+
27
+ export { scanUris };
@@ -0,0 +1,91 @@
1
+ import { isAbsolute } from 'pathe';
2
+ import { readPackageJSON } from 'pkg-types';
3
+ import { relativeWithDot } from 'silgi/kit';
4
+
5
+ async function createCoreFramework(silgi) {
6
+ const relativeRootDir = relativeWithDot(silgi.options.rootDir, silgi.options.serverDir);
7
+ if (silgi.options.preset !== "nitro" && silgi.options.preset !== "h3")
8
+ return;
9
+ const importItems = {
10
+ "silgi/types": {
11
+ import: [
12
+ { name: "ModuleRuntimeContext", type: true }
13
+ ],
14
+ from: "silgi/types"
15
+ }
16
+ };
17
+ await Promise.all([...silgi.options.modules, ...silgi.options._modules].map(async (id) => {
18
+ if (typeof id !== "string") {
19
+ return;
20
+ }
21
+ const pkg = await readPackageJSON(id, { url: silgi.options.modulesDir }).catch(() => null);
22
+ if (!pkg?.name) {
23
+ return;
24
+ }
25
+ if (importItems[pkg.name]) {
26
+ importItems[pkg.name].from = isAbsolute(id) ? relativeWithDot(relativeRootDir, id) : id;
27
+ }
28
+ }));
29
+ const customImports = [];
30
+ const functions = [];
31
+ await silgi.callHook("prepare:createCoreFramework", {
32
+ importItems,
33
+ customImports,
34
+ functions
35
+ });
36
+ const content = [
37
+ ...functions.map((f) => f.params?.length ? ` await ${f.name}(framework, ${f.params.join(",")})` : ` await ${f.name}(framework)`)
38
+ ];
39
+ return {
40
+ content,
41
+ importItems,
42
+ customImports
43
+ };
44
+ }
45
+ async function createDTSFramework(silgi) {
46
+ const relativeRootDir = relativeWithDot(silgi.options.rootDir, silgi.options.serverDir);
47
+ const importItems = {
48
+ "silgi/types": {
49
+ import: [
50
+ { name: "ModuleRuntimeContext", type: true }
51
+ ],
52
+ from: "silgi/types"
53
+ }
54
+ };
55
+ await Promise.all([...silgi.options.modules, ...silgi.options._modules].map(async (id) => {
56
+ if (typeof id !== "string") {
57
+ return;
58
+ }
59
+ const pkg = await readPackageJSON(id, { url: silgi.options.modulesDir }).catch(() => null);
60
+ if (!pkg?.name) {
61
+ return;
62
+ }
63
+ if (importItems[pkg.name]) {
64
+ importItems[pkg.name].from = isAbsolute(id) ? relativeWithDot(relativeRootDir, id) : id;
65
+ }
66
+ }));
67
+ const customImports = [];
68
+ const customContent = [];
69
+ await silgi.callHook("prepare:createDTSFramework", {
70
+ importItems,
71
+ customImports,
72
+ customContent
73
+ });
74
+ const content = [
75
+ ...Object.entries(importItems).map(([_name, { from, import: imports }]) => {
76
+ const path = isAbsolute(from) ? relativeWithDot(silgi.options.build.typesDir, from) : from;
77
+ return `import { ${imports.map(({ type, name }) => type ? `type ${name}` : name).join(", ")} } from '${path}'`;
78
+ }),
79
+ "",
80
+ ...customImports,
81
+ "",
82
+ ...customContent,
83
+ ""
84
+ ];
85
+ return {
86
+ content,
87
+ importItems
88
+ };
89
+ }
90
+
91
+ export { createCoreFramework, createDTSFramework };
@@ -0,0 +1,115 @@
1
+ import { isAbsolute } from 'pathe';
2
+ import { readPackageJSON } from 'pkg-types';
3
+ import { relativeWithDot } from 'silgi/kit';
4
+
5
+ async function schemaTemplate(silgi) {
6
+ const relativeRootDir = relativeWithDot(silgi.options.rootDir, silgi.options.serverDir);
7
+ const importItems = {
8
+ "silgi/types": {
9
+ import: [
10
+ { name: "URIsTypes", type: true },
11
+ { name: "Namespaces", type: true },
12
+ { name: "ModuleRuntimeContext", type: true }
13
+ ],
14
+ from: "silgi/types"
15
+ }
16
+ };
17
+ await Promise.all([...silgi.options.modules, ...silgi.options._modules].map(async (id) => {
18
+ if (typeof id !== "string") {
19
+ return;
20
+ }
21
+ const pkg = await readPackageJSON(id, { url: silgi.options.modulesDir }).catch(() => null);
22
+ if (!pkg?.name) {
23
+ return;
24
+ }
25
+ if (importItems[pkg.name]) {
26
+ importItems[pkg.name].from = isAbsolute(id) ? relativeWithDot(relativeRootDir, id) : id;
27
+ }
28
+ }));
29
+ const data = {
30
+ importItems,
31
+ customImports: [],
32
+ configs: [],
33
+ contexts: [],
34
+ methods: [],
35
+ shareds: [],
36
+ events: [],
37
+ storeBase: [],
38
+ hooks: [],
39
+ runtimeHooks: []
40
+ };
41
+ const storeBase = [];
42
+ await silgi.callHook("prepare:schema.ts", data);
43
+ const silgiExport = relativeWithDot(silgi.options.build.typesDir, `${silgi.options.silgi.serverDir}/core.ts`);
44
+ let addSilgiContext = false;
45
+ const importsContent = [
46
+ ...Object.entries(importItems).map(([_name, { from, import: imports }]) => {
47
+ const path = isAbsolute(from) ? relativeWithDot(silgi.options.build.typesDir, from) : from;
48
+ return `import { ${imports.map(({ type, name }) => type ? `type ${name}` : name).join(", ")} } from '${path}'`;
49
+ }),
50
+ "",
51
+ ...data.customImports,
52
+ ""
53
+ ];
54
+ const importData = [
55
+ "interface InferredNamespaces {",
56
+ ...(silgi.options.namespaces || []).map((key) => ` ${key}: string,`),
57
+ "}",
58
+ "",
59
+ `type SchemaExtends = Namespaces<typeof import('${silgiExport}')['schemas']>`,
60
+ "",
61
+ `type SilgiURIsMerge = URIsTypes<typeof import('${silgiExport}')['uris']>`,
62
+ "",
63
+ `type SilgiModuleContextExtends = ${data.contexts.length ? data.contexts.map(({ value }) => value).join(" & ") : "{}"}`,
64
+ "",
65
+ data.events.length ? `interface SilgiModuleEventsExtends extends ${data.events.map((item) => item.extends ? item.value : "").join(", ")} {
66
+ ${data.events.map((item) => {
67
+ if (item.isSilgiContext) {
68
+ addSilgiContext = true;
69
+ }
70
+ return !item.extends && !addSilgiContext ? ` ${item.key}: ${item.value}` : item.isSilgiContext ? " context: ModuleRuntimeContext" : "";
71
+ }).join(",\n")}
72
+ }` : "interface SilgiModuleEventsExtends {}",
73
+ "",
74
+ data.shareds.length ? `type SilgiModuleSharedExtends = ${data.shareds.map(({ value }) => value).join(" & ")}` : "type SilgiModuleSharedExtends = {}",
75
+ "",
76
+ "interface SilgiModuleMethodsExtends {",
77
+ ...(data.methods || []).map(({ key, value }) => ` ${key}: ${value},`),
78
+ "}",
79
+ "",
80
+ "interface SilgiModuleOptionsExtends {",
81
+ ...(data.configs || []).map(({ key, value }) => ` ${key}: ${value},`),
82
+ "}",
83
+ "",
84
+ "interface SilgiStorageBaseExtends {",
85
+ ...(storeBase || []).map((value) => ` ${value}: ''`),
86
+ "}",
87
+ "",
88
+ `type ModuleHooksExtend = ${data.hooks?.length ? data.hooks.map(({ value }) => `${value}`).join(" & ") : "{}"}`,
89
+ "",
90
+ `type SilgiRuntimeHooksExtends = ${data.runtimeHooks?.length ? data.runtimeHooks.map(({ value }) => `${value}`).join(" & ") : "{}"}`,
91
+ "",
92
+ "declare module 'silgi/types' {",
93
+ " interface FrameworkContext extends FrameworkContextExtends {}",
94
+ " interface SilgiSchema extends SchemaExtends {}",
95
+ " interface SilgiNamespaces extends InferredNamespaces {}",
96
+ " interface SilgiStorageBase extends SilgiStorageBaseExtends {}",
97
+ " interface SilgiModules extends SilgiModuleOptionsExtends {}",
98
+ " interface SilgiURIs extends SilgiURIsMerge {}",
99
+ " interface ModuleRuntimeContext extends SilgiModuleContextExtends {}",
100
+ " interface SilgiEvent extends SilgiModuleEventsExtends {}",
101
+ " interface SilgiDefaultShared extends SilgiModuleSharedExtends {}",
102
+ " interface ModuleRuntimeMethods extends SilgiModuleMethodsExtends {}",
103
+ " interface ModuleOptions extends SilgiModuleOptionsExtends {}",
104
+ " interface ModuleRuntimeHooks extends SilgiRuntimeHooksExtends {}",
105
+ " interface ModuleHooks extends ModuleHooksExtend {}",
106
+ "}",
107
+ "",
108
+ "export {}"
109
+ ];
110
+ await silgi.callHook("after:prepare:schema.ts", importData);
111
+ importData.unshift(...importsContent);
112
+ return importData;
113
+ }
114
+
115
+ export { schemaTemplate };
@@ -0,0 +1,149 @@
1
+ import { hash } from 'ohash';
2
+ import { isAbsolute } from 'pathe';
3
+ import { readPackageJSON } from 'pkg-types';
4
+ import { relativeWithDot } from 'silgi/kit';
5
+
6
+ async function silgiCoreFile(data, frameworkContext, silgi) {
7
+ const relativeRootDir = relativeWithDot(silgi.options.rootDir, silgi.options.serverDir);
8
+ let importItems = {
9
+ "silgi/core": {
10
+ import: [
11
+ { name: "createSilgi" }
12
+ ],
13
+ from: "silgi/core"
14
+ },
15
+ "silgi/types": {
16
+ import: [
17
+ { name: "ModuleOptions", type: true },
18
+ { name: "SilgiOptions", type: true },
19
+ { name: "FrameworkContext", type: true },
20
+ { name: "DeepPartial", type: true },
21
+ { name: "BuildConfig", type: true }
22
+ ],
23
+ from: "silgi/types"
24
+ },
25
+ "#silgi/vfs": {
26
+ import: [],
27
+ from: "./vfs"
28
+ }
29
+ };
30
+ importItems = { ...data._importItems, ...importItems };
31
+ await Promise.all([...silgi.options.modules, ...silgi.options._modules].map(async (id) => {
32
+ if (typeof id !== "string") {
33
+ return;
34
+ }
35
+ const pkg = await readPackageJSON(id, { url: silgi.options.modulesDir }).catch(() => null);
36
+ if (!pkg?.name) {
37
+ return;
38
+ }
39
+ if (importItems[pkg.name]) {
40
+ importItems[pkg.name].from = isAbsolute(id) ? relativeWithDot(relativeRootDir, id) : id;
41
+ }
42
+ }));
43
+ const customImports = data._customImports || [];
44
+ const uris = [];
45
+ const services = [];
46
+ const shareds = [];
47
+ const schemas = [];
48
+ const buildSilgiExtraContent = [];
49
+ const _silgiOptions = {};
50
+ const _silgiConfigs = [];
51
+ for (const module of silgi.scanModules) {
52
+ _silgiOptions[module.meta.configKey] = {
53
+ ...module.options
54
+ };
55
+ }
56
+ await silgi.callHook("prepare:core.ts", {
57
+ importItems,
58
+ customImports,
59
+ uris,
60
+ services,
61
+ shareds,
62
+ schemas,
63
+ buildSilgiExtraContent,
64
+ _silgiOptions,
65
+ _silgiConfigs
66
+ });
67
+ if (importItems["#silgi/vfs"].import.length === 0) {
68
+ delete importItems["#silgi/vfs"];
69
+ }
70
+ if (services.length > 0) {
71
+ importItems["silgi/core"].import.push({ name: "mergeServices" });
72
+ }
73
+ if (shareds.length > 0) {
74
+ importItems["silgi/core"].import.push({ name: "mergeShared" });
75
+ }
76
+ if (schemas.length > 0) {
77
+ importItems["silgi/core"].import.push({ name: "mergeSchemas" });
78
+ }
79
+ const plugins = [];
80
+ for (const plugin of silgi.options.plugins) {
81
+ const pluginImportName = `_${hash(plugin.packageImport)}`;
82
+ customImports.push(`import ${pluginImportName} from '${plugin.packageImport}'`);
83
+ plugins.push(pluginImportName);
84
+ }
85
+ const importsContent = [
86
+ ...Object.entries(importItems).map(([_name, { from, import: imports }]) => {
87
+ return `import { ${imports.map(({ type, name }) => type ? `type ${name}` : name).join(", ")} } from '${from}'`;
88
+ }),
89
+ "",
90
+ ...customImports,
91
+ ""
92
+ ];
93
+ const importData = [
94
+ "export const uris = {}",
95
+ "",
96
+ "export const modulesURIs = {}",
97
+ "",
98
+ schemas.length > 0 ? "export const schemas = mergeSchemas([" : "export const schemas = {",
99
+ ...schemas.map((name) => {
100
+ return ` ${name},`;
101
+ }),
102
+ schemas.length > 0 ? "])" : "}",
103
+ "",
104
+ services.length > 0 ? "export const services = mergeServices([" : "export const services = {",
105
+ ...services.map((name) => {
106
+ return ` ${name},`;
107
+ }),
108
+ services.length > 0 ? "])" : "}",
109
+ "",
110
+ shareds.length > 0 ? "export const shareds = mergeShared([" : "export const shareds = {",
111
+ ...shareds.map((name) => {
112
+ return ` ${name},`;
113
+ }),
114
+ shareds.length > 0 ? "])" : "}",
115
+ "",
116
+ `export const _silgiOptions: DeepPartial<ModuleOptions> = ${JSON.stringify(_silgiOptions, null, 2)}`,
117
+ "",
118
+ "export async function buildSilgi(framework: FrameworkContext, moduleOptions?: Partial<ModuleOptions>,buildOptions?: Partial<BuildConfig>, ) {",
119
+ " const silgi = await createSilgi({",
120
+ " framework,",
121
+ " shared: shareds as any,",
122
+ " services: services as any,",
123
+ " schemas: schemas as any,",
124
+ " uris,",
125
+ " modulesURIs,",
126
+ ` plugins: [${plugins.join(", ")}],`,
127
+ _silgiConfigs.length > 0 ? ` ${_silgiConfigs.map((config) => Object.entries(config).map(([key, value]) => `${key}: ${value}`)).join(",\n ")},` : "",
128
+ " ...buildOptions,",
129
+ " options: {",
130
+ ` present: '${silgi.options.preset}',`,
131
+ " ..._silgiOptions,",
132
+ " ...moduleOptions,",
133
+ " },",
134
+ " })",
135
+ "",
136
+ ...frameworkContext,
137
+ "",
138
+ ...buildSilgiExtraContent,
139
+ "",
140
+ " return silgi",
141
+ "}",
142
+ ""
143
+ ];
144
+ await silgi.callHook("after:prepare:core.ts", importData);
145
+ importData.unshift(...importsContent);
146
+ return importData;
147
+ }
148
+
149
+ export { silgiCoreFile };
@@ -0,0 +1,130 @@
1
+ import { existsSync } from 'node:fs';
2
+ import { resolvePath, parseNodeModulePath, lookupNodeModuleSubpath } from 'mlly';
3
+ import { resolve, relative, isAbsolute, join } from 'pathe';
4
+ import { resolveAlias } from 'pathe/utils';
5
+ import { silgiGenerateType } from 'silgi/core';
6
+ import { isDirectory, writeFile } from 'silgi/kit';
7
+ import { runtimeDir } from 'silgi/runtime/meta';
8
+ import { toExports } from 'unimport';
9
+ import { generateSilgiStorageBaseType } from '../core/storage.mjs';
10
+ import { generateRouterDTS } from '../utils/generateRouterDTS.mjs';
11
+ import { readCoreFile } from '../utils/readCoreFile.mjs';
12
+ import { scanUris } from './scanURIs.mjs';
13
+ import { createCoreFramework, createDTSFramework } from './template/framework.mjs';
14
+ import { schemaTemplate } from './template/schema.mjs';
15
+ import { silgiCoreFile } from './template/silgi.mjs';
16
+
17
+ async function writeTypesAndFiles(silgi) {
18
+ const data = await createCoreFramework(silgi);
19
+ await generateSilgiStorageBaseType(silgi);
20
+ await generateRouterDTS(silgi);
21
+ silgi.hook("prepare:types", (opts) => {
22
+ opts.references.push({ path: "./schema.d.ts" });
23
+ opts.references.push({ path: "./silgi-routes.d.ts" });
24
+ opts.references.push({ path: "./framework.d.ts" });
25
+ });
26
+ const schemaContent = await schemaTemplate(silgi);
27
+ const coreContent = await silgiCoreFile({
28
+ _importItems: data?.importItems ?? {},
29
+ _customImports: data?.customImports ?? []
30
+ }, data?.content ?? [], silgi);
31
+ const frameworkDTS = await createDTSFramework(silgi);
32
+ const { declarations, tsConfig } = await silgiGenerateType(silgi);
33
+ const tsConfigPath = resolve(
34
+ silgi.options.typescript.generateTsConfig ? silgi.options.build.dir : silgi.options.rootDir,
35
+ silgi.options.typescript.tsconfigPath
36
+ );
37
+ const typesDir = resolve(silgi.options.build.typesDir);
38
+ const silgiDir = resolve(silgi.options.silgi.serverDir);
39
+ let autoImportedTypes = [];
40
+ let autoImportExports = "";
41
+ if (silgi.unimport) {
42
+ await silgi.unimport.init();
43
+ const allImports = await silgi.unimport.getImports();
44
+ autoImportExports = toExports(allImports).replace(
45
+ /#internal\/nitro/g,
46
+ relative(typesDir, runtimeDir)
47
+ );
48
+ const resolvedImportPathMap = /* @__PURE__ */ new Map();
49
+ for (const i of allImports.filter((i2) => !i2.type)) {
50
+ if (resolvedImportPathMap.has(i.from)) {
51
+ continue;
52
+ }
53
+ let path = resolveAlias(i.from, silgi.options.alias);
54
+ if (isAbsolute(path)) {
55
+ const resolvedPath = await resolvePath(i.from, {
56
+ url: silgi.options.nodeModulesDirs
57
+ }).catch(() => null);
58
+ if (resolvedPath) {
59
+ const { dir, name } = parseNodeModulePath(resolvedPath);
60
+ if (!dir || !name) {
61
+ path = resolvedPath;
62
+ } else {
63
+ const subpath = await lookupNodeModuleSubpath(resolvedPath);
64
+ path = join(dir, name, subpath || "");
65
+ }
66
+ }
67
+ }
68
+ if (existsSync(path) && !await isDirectory(path)) {
69
+ path = path.replace(/\.[a-z]+$/, "");
70
+ }
71
+ if (isAbsolute(path)) {
72
+ path = relative(typesDir, path);
73
+ }
74
+ resolvedImportPathMap.set(i.from, path);
75
+ }
76
+ autoImportedTypes = [
77
+ silgi.options.imports && silgi.options.imports.autoImport !== false ? (await silgi.unimport.generateTypeDeclarations({
78
+ exportHelper: false,
79
+ resolvePath: (i) => resolvedImportPathMap.get(i.from) ?? i.from
80
+ })).trim() : ""
81
+ ];
82
+ }
83
+ const buildFiles = [];
84
+ buildFiles.push({
85
+ path: join(typesDir, "silgi-routes.d.ts"),
86
+ // contents: uris.join('\n'),
87
+ contents: ""
88
+ });
89
+ buildFiles.push({
90
+ path: join(silgiDir, "core.ts"),
91
+ contents: coreContent.join("\n")
92
+ });
93
+ buildFiles.push({
94
+ path: join(typesDir, "schema.d.ts"),
95
+ contents: schemaContent.join("\n")
96
+ });
97
+ buildFiles.push({
98
+ path: join(typesDir, "silgi-imports.d.ts"),
99
+ contents: [...autoImportedTypes, autoImportExports || "export {}"].join(
100
+ "\n"
101
+ )
102
+ });
103
+ buildFiles.push({
104
+ path: join(typesDir, "silgi.d.ts"),
105
+ contents: declarations.join("\n")
106
+ });
107
+ buildFiles.push({
108
+ path: tsConfigPath,
109
+ contents: JSON.stringify(tsConfig, null, 2)
110
+ });
111
+ buildFiles.push({
112
+ path: join(typesDir, "framework.d.ts"),
113
+ contents: frameworkDTS.content.join("\n")
114
+ });
115
+ for await (const file of buildFiles) {
116
+ await writeFile(
117
+ resolve(silgi.options.build.dir, file.path),
118
+ file.contents
119
+ );
120
+ }
121
+ await scanUris(silgi);
122
+ const readCore = await readCoreFile(silgi);
123
+ silgi.uris = readCore.object.uris;
124
+ silgi.modulesURIs = readCore.object.modulesURIs;
125
+ silgi.schemas = readCore.object.schemas;
126
+ silgi.shareds = readCore.object.shareds;
127
+ await silgi.hooks.callHook("finish:types", readCore);
128
+ }
129
+
130
+ export { writeTypesAndFiles };
@@ -0,0 +1,49 @@
1
+ import { defineCommand } from 'citty';
2
+ import { resolve } from 'pathe';
3
+ import { version } from 'silgi/meta';
4
+ import { frameworkSetup } from '../build/framework/index.mjs';
5
+ import { prepare as prepare$1 } from '../build/prepare.mjs';
6
+ import { writeTypesAndFiles } from '../build/types.mjs';
7
+ import { commonArgs } from '../common.mjs';
8
+ import { createSilgiCLI } from '../core/silgi.mjs';
9
+ import { readCoreFile } from '../utils/readCoreFile.mjs';
10
+
11
+ const prepare = defineCommand({
12
+ meta: {
13
+ name: "prepare",
14
+ description: "Generate types for the project",
15
+ version: version
16
+ },
17
+ args: {
18
+ ...commonArgs,
19
+ preset: {
20
+ type: "string",
21
+ description: "The build preset to use (you can also use `SILGI_PRESET` environment variable)."
22
+ },
23
+ stub: {
24
+ type: "boolean",
25
+ description: "Run in silgi development mode"
26
+ }
27
+ },
28
+ async run({ args }) {
29
+ const rootDir = resolve(args.dir || args._dir || ".");
30
+ const silgi = await createSilgiCLI({
31
+ rootDir,
32
+ dev: args.stub,
33
+ stub: args.stub,
34
+ typescript: { internalPaths: args.stub }
35
+ });
36
+ await prepare$1();
37
+ for (const framework of frameworkSetup) {
38
+ await framework(silgi);
39
+ }
40
+ await writeTypesAndFiles(silgi);
41
+ await silgi.callHook("read:core.ts", async () => {
42
+ const data = await readCoreFile(silgi);
43
+ return data;
44
+ });
45
+ await silgi.close();
46
+ }
47
+ });
48
+
49
+ export { prepare as default };
@@ -0,0 +1,13 @@
1
+ const commonArgs = {
2
+ dir: {
3
+ type: "string",
4
+ description: "project root directory"
5
+ },
6
+ _dir: {
7
+ type: "positional",
8
+ default: ".",
9
+ description: "project root directory (prefer using `--dir`)"
10
+ }
11
+ };
12
+
13
+ export { commonArgs };