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.
- package/dist/cli/build/prepare.mjs +43 -0
- package/dist/cli/{dev.mjs → commands/dev.mjs} +4 -43
- package/dist/cli/commands/env.mjs +40 -0
- package/dist/cli/{install.mjs → commands/install.mjs} +3 -37
- package/dist/cli/commands/prepare.mjs +66 -0
- package/dist/cli/{prepare.mjs → commands/run.mjs} +9 -82
- package/dist/cli/config/defaults.mjs +129 -0
- package/dist/cli/config/index.mjs +2 -16
- package/dist/cli/config/loader.mjs +97 -0
- package/dist/cli/config/resolvers/compatibility.mjs +90 -0
- package/dist/cli/config/resolvers/imports.mjs +96 -0
- package/dist/cli/config/resolvers/paths.mjs +167 -0
- package/dist/cli/config/resolvers/storage.mjs +25 -0
- package/dist/cli/config/resolvers/url.mjs +7 -0
- package/dist/cli/config/types.mjs +191 -0
- package/dist/cli/core/app.mjs +92 -0
- package/dist/cli/core/devServer.mjs +8 -0
- package/dist/cli/core/env.mjs +76 -0
- package/dist/cli/core/installPackage.mjs +31 -0
- package/dist/cli/core/prepare.mjs +7 -0
- package/dist/cli/core/runtimeConfig.mjs +14 -0
- package/dist/cli/core/scan.mjs +39 -0
- package/dist/cli/core/silgi.mjs +125 -0
- package/dist/cli/core/templates.mjs +31 -0
- package/dist/cli/framework/emptyFramework.mjs +9 -0
- package/dist/cli/framework/h3.mjs +89 -0
- package/dist/cli/framework/index.mjs +8 -0
- package/dist/cli/framework/nitro.mjs +106 -0
- package/dist/cli/framework/nuxt.mjs +9 -0
- package/dist/cli/index.mjs +6 -9
- package/dist/cli/module/exportScan.mjs +130 -0
- package/dist/cli/module/install.mjs +59 -0
- package/dist/cli/module/scan.mjs +227 -0
- package/dist/cli/scan/commands.mjs +47 -0
- package/dist/cli/scan/prepareConfigs.mjs +46 -0
- package/dist/cli/scan/prepareCoreFile.mjs +166 -0
- package/dist/cli/scan/prepareFramework.mjs +73 -0
- package/dist/cli/scan/prepareSchema.mjs +177 -0
- package/dist/cli/scan/prepareServerFiles.mjs +119 -0
- package/dist/cli/scan/scanExportFile.mjs +155 -0
- package/dist/cli/scan/writeCoreFile.mjs +34 -0
- package/dist/cli/scan/writeScanFiles.mjs +30 -0
- package/dist/cli/scan/writeTypesAndFiles.mjs +109 -0
- package/dist/cli/utils/build-uri-map.mjs +39 -0
- package/dist/cli/utils/common.mjs +13 -0
- package/dist/cli/{compatibility.mjs → utils/compatibility.mjs} +1 -1
- package/dist/cli/utils/generateRouterDTS.mjs +80 -0
- package/dist/cli/utils/ignore.mjs +46 -0
- package/dist/cli/utils/parser.mjs +168 -0
- package/dist/cli/utils/readScanFile.mjs +79 -0
- package/dist/cli/utils/storage.mjs +25 -0
- package/dist/core/createSilgi.mjs +76 -0
- package/dist/core/error.mjs +227 -0
- package/dist/core/index.mjs +8 -0
- package/dist/core/routeRules.mjs +288 -0
- package/dist/core/silgi.mjs +128 -0
- package/dist/core/silgiApp.mjs +25 -0
- package/dist/core/storage.mjs +15 -0
- package/dist/core/unctx.mjs +27 -0
- package/dist/core/uris/uri.mjs +33 -0
- package/dist/core/uris/utils.mjs +130 -0
- package/dist/core/utils/event.mjs +15 -0
- package/dist/core/utils/merge.mjs +29 -0
- package/dist/core/utils/runtime.mjs +22 -0
- package/dist/core/utils/schema.mjs +5 -0
- package/dist/core/utils/service.mjs +5 -0
- package/dist/core/utils/shared.mjs +5 -0
- package/dist/core/utils/storage.mjs +72 -0
- package/dist/index.mjs +20 -793
- package/dist/kit/errors.mjs +11 -0
- package/dist/kit/esm.mjs +14 -0
- package/dist/kit/fs.mjs +30 -0
- package/dist/kit/gen.mjs +44 -0
- package/dist/kit/hash.mjs +8 -0
- package/dist/kit/index.mjs +15 -528
- package/dist/kit/isFramework.mjs +25 -0
- package/dist/kit/logger.mjs +7 -0
- package/dist/kit/module.mjs +73 -0
- package/dist/kit/parseServices.mjs +57 -0
- package/dist/kit/path.mjs +34 -0
- package/dist/kit/preset.mjs +6 -0
- package/dist/kit/resolve.mjs +80 -0
- package/dist/kit/template.mjs +49 -0
- package/dist/kit/useRequest.mjs +91 -0
- package/dist/kit/utils.mjs +43 -0
- package/dist/package.json.mjs +5 -0
- package/dist/types/index.d.mts +1 -1
- package/package.json +1 -1
- package/dist/_chunks/silgiApp.mjs +0 -2682
- package/dist/cli/types.mjs +0 -772
- /package/dist/cli/{init.mjs → commands/init.mjs} +0 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { isAbsolute } from 'pathe';
|
|
2
|
+
import { relativeWithDot } from 'silgi/kit';
|
|
3
|
+
import { generateTypes, resolveSchema } from 'untyped';
|
|
4
|
+
|
|
5
|
+
async function prepareSchema(silgi) {
|
|
6
|
+
const importItems = {
|
|
7
|
+
"silgi/types": {
|
|
8
|
+
import: [
|
|
9
|
+
{
|
|
10
|
+
name: "URIsTypes",
|
|
11
|
+
type: true,
|
|
12
|
+
key: "URIsTypes"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
name: "Namespaces",
|
|
16
|
+
type: true,
|
|
17
|
+
key: "Namespaces"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
name: "SilgiRuntimeContext",
|
|
21
|
+
type: true,
|
|
22
|
+
key: "SilgiRuntimeContext"
|
|
23
|
+
}
|
|
24
|
+
],
|
|
25
|
+
from: "silgi/types"
|
|
26
|
+
},
|
|
27
|
+
"silgi/scan": {
|
|
28
|
+
import: [{
|
|
29
|
+
key: "modulesURIs",
|
|
30
|
+
name: "modulesURIs",
|
|
31
|
+
type: false
|
|
32
|
+
}],
|
|
33
|
+
from: relativeWithDot(silgi.options.build.typesDir, `${silgi.options.silgi.serverDir}/scan.ts`)
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
const data = {
|
|
37
|
+
importItems,
|
|
38
|
+
customImports: [],
|
|
39
|
+
options: [],
|
|
40
|
+
contexts: [],
|
|
41
|
+
actions: [],
|
|
42
|
+
shareds: [
|
|
43
|
+
{
|
|
44
|
+
key: "modulesURIs",
|
|
45
|
+
value: "{ modulesURIs: typeof modulesURIs }"
|
|
46
|
+
}
|
|
47
|
+
],
|
|
48
|
+
events: [],
|
|
49
|
+
hooks: [],
|
|
50
|
+
runtimeHooks: [],
|
|
51
|
+
runtimeOptions: [],
|
|
52
|
+
methods: [],
|
|
53
|
+
routeRules: [],
|
|
54
|
+
routeRulesConfig: []
|
|
55
|
+
};
|
|
56
|
+
await silgi.callHook("prepare:schema.ts", data);
|
|
57
|
+
relativeWithDot(silgi.options.build.typesDir, `${silgi.options.silgi.serverDir}/core.ts`);
|
|
58
|
+
const silgiScanTS = relativeWithDot(silgi.options.build.typesDir, `${silgi.options.silgi.serverDir}/scan.ts`);
|
|
59
|
+
let addSilgiContext = false;
|
|
60
|
+
const importsContent = [
|
|
61
|
+
...Object.entries(importItems).map(([_name, { from, import: imports }]) => {
|
|
62
|
+
const path = isAbsolute(from) ? relativeWithDot(silgi.options.build.typesDir, from) : from;
|
|
63
|
+
if (silgi.options.typescript.removeFileExtension) {
|
|
64
|
+
from = from.replace(/\.(js|ts|mjs|cjs|jsx|tsx)$/, "");
|
|
65
|
+
}
|
|
66
|
+
return `import { ${imports.map(({ type, name }) => type ? `type ${name}` : name).join(", ")} } from '${path}'`;
|
|
67
|
+
}),
|
|
68
|
+
"",
|
|
69
|
+
...data.customImports,
|
|
70
|
+
""
|
|
71
|
+
];
|
|
72
|
+
const importData = [
|
|
73
|
+
"interface InferredNamespaces {",
|
|
74
|
+
...(silgi.options.namespaces || []).map((key) => ` ${key}: string,`),
|
|
75
|
+
"}",
|
|
76
|
+
"",
|
|
77
|
+
`type SchemaExtends = Namespaces<typeof import('${silgiScanTS}')['schemas']>`,
|
|
78
|
+
"",
|
|
79
|
+
`type SilgiURIsMerge = URIsTypes<typeof import('${silgiScanTS}')['uris']>`,
|
|
80
|
+
"",
|
|
81
|
+
`type SilgiModuleContextExtends = ${data.contexts.length ? data.contexts.map(({ value }) => value).join(" & ") : "{}"}`,
|
|
82
|
+
"",
|
|
83
|
+
data.events.length ? `interface SilgiModuleEventsExtends extends ${data.events.map((item) => item.extends ? item.value : "").join(", ")} {
|
|
84
|
+
${data.events.map((item) => {
|
|
85
|
+
if (item.isSilgiContext) {
|
|
86
|
+
addSilgiContext = true;
|
|
87
|
+
}
|
|
88
|
+
return !item.extends && !addSilgiContext ? ` ${item.key}: ${item.value}` : item.isSilgiContext ? " context: SilgiRuntimeContext" : "";
|
|
89
|
+
}).join(",\n")}
|
|
90
|
+
}` : "interface SilgiModuleEventsExtends {}",
|
|
91
|
+
"",
|
|
92
|
+
`type RuntimeActionExtends = ${data.actions?.length ? data.actions.map(({ value }) => `${value}`).join(" & ") : "{}"}`,
|
|
93
|
+
"",
|
|
94
|
+
`type RuntimeMethodExtends = ${data.methods?.length ? data.methods.map(({ value }) => `${value}`).join(" & ") : "{}"}`,
|
|
95
|
+
"",
|
|
96
|
+
`type RuntimeRouteRulesExtends = ${data.routeRules?.length ? data.routeRules.map(({ value }) => `${value}`).join(" & ") : "{}"}`,
|
|
97
|
+
"",
|
|
98
|
+
`type RuntimeRouteRulesConfigExtends = ${data.routeRulesConfig?.length ? data.routeRulesConfig.map(({ value }) => `${value}`).join(" & ") : "{}"}`,
|
|
99
|
+
"",
|
|
100
|
+
`type SilgiModuleSharedExtends = ${data.shareds.length ? data.shareds.map(({ value }) => `${value}`).join(" & ") : "{}"}`,
|
|
101
|
+
"",
|
|
102
|
+
`type SilgiModuleOptionExtend = ${data.options?.length ? data.options.map(({ value }) => `${value}`).join(" & ") : "{}"}`,
|
|
103
|
+
"",
|
|
104
|
+
`type SilgiRuntimeOptionExtends = ${data.runtimeOptions?.length ? data.runtimeOptions.map(({ value }) => `${value}`).join(" & ") : "{}"}`,
|
|
105
|
+
"",
|
|
106
|
+
silgi.options.typescript.generateRuntimeConfigTypes ? generateTypes(
|
|
107
|
+
await resolveSchema(
|
|
108
|
+
{
|
|
109
|
+
...Object.fromEntries(
|
|
110
|
+
Object.entries(silgi.options.runtimeConfig).filter(
|
|
111
|
+
([key]) => !["app", "nitro", "nuxt"].includes(key)
|
|
112
|
+
)
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
),
|
|
116
|
+
{
|
|
117
|
+
interfaceName: "SilgiRuntimeConfigExtends",
|
|
118
|
+
addExport: false,
|
|
119
|
+
addDefaults: false,
|
|
120
|
+
allowExtraKeys: false,
|
|
121
|
+
indentation: 0
|
|
122
|
+
}
|
|
123
|
+
) : "",
|
|
124
|
+
"",
|
|
125
|
+
generateTypes(
|
|
126
|
+
await resolveSchema(
|
|
127
|
+
{
|
|
128
|
+
...silgi.options.storages?.reduce((acc, key) => ({ ...acc, [key]: "" }), {}) || {},
|
|
129
|
+
// 'redis': {} -> 'redis': string
|
|
130
|
+
...Object.entries(silgi.options.storage).map(([key]) => ({
|
|
131
|
+
[key]: ""
|
|
132
|
+
})).reduce((acc, obj) => ({ ...acc, ...obj }), {})
|
|
133
|
+
}
|
|
134
|
+
),
|
|
135
|
+
{
|
|
136
|
+
interfaceName: "SilgiStorageBaseExtends",
|
|
137
|
+
addExport: false,
|
|
138
|
+
addDefaults: false,
|
|
139
|
+
allowExtraKeys: false,
|
|
140
|
+
indentation: 0
|
|
141
|
+
}
|
|
142
|
+
),
|
|
143
|
+
"",
|
|
144
|
+
`type ModuleHooksExtend = ${data.hooks?.length ? data.hooks.map(({ value }) => `${value}`).join(" & ") : "{}"}`,
|
|
145
|
+
"",
|
|
146
|
+
`type SilgiRuntimeHooksExtends = ${data.runtimeHooks?.length ? data.runtimeHooks.map(({ value }) => `${value}`).join(" & ") : "{}"}`,
|
|
147
|
+
"",
|
|
148
|
+
"declare module 'silgi/types' {",
|
|
149
|
+
" interface FrameworkContext extends FrameworkContextExtends {}",
|
|
150
|
+
" interface SilgiSchema extends SchemaExtends {}",
|
|
151
|
+
" interface SilgiNamespaces extends InferredNamespaces {}",
|
|
152
|
+
" interface SilgiStorageBase extends SilgiStorageBaseExtends {}",
|
|
153
|
+
" interface SilgiURIs extends SilgiURIsMerge {}",
|
|
154
|
+
" interface SilgiRuntimeContext extends SilgiModuleContextExtends {}",
|
|
155
|
+
" interface SilgiEvents extends SilgiModuleEventsExtends {}",
|
|
156
|
+
" interface SilgiRuntimeSharedsExtend extends SilgiModuleSharedExtends {}",
|
|
157
|
+
" interface SilgiRuntimeActions extends RuntimeActionExtends {}",
|
|
158
|
+
" interface SilgiModuleOptions extends SilgiModuleOptionExtend {}",
|
|
159
|
+
" interface SilgiRuntimeOptions extends SilgiRuntimeOptionExtends {}",
|
|
160
|
+
" interface SilgiRuntimeHooks extends SilgiRuntimeHooksExtends {}",
|
|
161
|
+
" interface SilgiRuntimeConfig extends SilgiRuntimeConfigExtends {}",
|
|
162
|
+
" interface SilgiHooks extends ModuleHooksExtend {}",
|
|
163
|
+
" interface SilgiRuntimeMethods extends RuntimeMethodExtends {}",
|
|
164
|
+
" interface SilgiRuntimeRouteRules extends RuntimeRouteRulesExtends {}",
|
|
165
|
+
" interface SilgiRuntimeRouteRulesConfig extends RuntimeRouteRulesConfigExtends {}",
|
|
166
|
+
" interface SilgiCommands extends SilgiCommandsExtended {}",
|
|
167
|
+
"",
|
|
168
|
+
"}",
|
|
169
|
+
"",
|
|
170
|
+
"export {}"
|
|
171
|
+
];
|
|
172
|
+
await silgi.callHook("after:prepare:schema.ts", importData);
|
|
173
|
+
importData.unshift(...importsContent);
|
|
174
|
+
return importData;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export { prepareSchema };
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { defu } from 'defu';
|
|
2
|
+
|
|
3
|
+
async function prepareServerFiles(silgi) {
|
|
4
|
+
const importItems = {
|
|
5
|
+
"silgi": {
|
|
6
|
+
import: [
|
|
7
|
+
{ name: "createSilgi", key: "createSilgi" },
|
|
8
|
+
{ name: "createShared", key: "createShared" }
|
|
9
|
+
],
|
|
10
|
+
from: "silgi"
|
|
11
|
+
},
|
|
12
|
+
"silgi/types": {
|
|
13
|
+
import: [
|
|
14
|
+
{ name: "SilgiRuntimeOptions", type: true, key: "SilgiRuntimeOptions" },
|
|
15
|
+
{ name: "FrameworkContext", type: true, key: "FrameworkContext" }
|
|
16
|
+
],
|
|
17
|
+
from: "silgi/types"
|
|
18
|
+
},
|
|
19
|
+
"#silgi/vfs": {
|
|
20
|
+
import: [],
|
|
21
|
+
from: "./vfs"
|
|
22
|
+
},
|
|
23
|
+
"configs.ts": {
|
|
24
|
+
import: [
|
|
25
|
+
{
|
|
26
|
+
name: "cliConfigs",
|
|
27
|
+
type: false,
|
|
28
|
+
key: "cliConfigs"
|
|
29
|
+
}
|
|
30
|
+
],
|
|
31
|
+
from: "./configs.ts"
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const scanned = {
|
|
35
|
+
uris: {},
|
|
36
|
+
services: [],
|
|
37
|
+
shareds: [
|
|
38
|
+
`createShared({
|
|
39
|
+
modulesURIs,
|
|
40
|
+
})`
|
|
41
|
+
],
|
|
42
|
+
schemas: [],
|
|
43
|
+
modulesURIs: {},
|
|
44
|
+
customImports: [],
|
|
45
|
+
importItems
|
|
46
|
+
};
|
|
47
|
+
if (silgi.uris) {
|
|
48
|
+
defu(scanned.uris, silgi.uris);
|
|
49
|
+
}
|
|
50
|
+
if (silgi.modulesURIs) {
|
|
51
|
+
defu(scanned.modulesURIs, silgi.modulesURIs);
|
|
52
|
+
}
|
|
53
|
+
await silgi.callHook("prepare:scan.ts", scanned);
|
|
54
|
+
if (importItems["#silgi/vfs"].import.length === 0) {
|
|
55
|
+
delete importItems["#silgi/vfs"];
|
|
56
|
+
}
|
|
57
|
+
if (scanned.services.length > 0) {
|
|
58
|
+
importItems.silgi.import.push({ name: "mergeServices", key: "mergeServices" });
|
|
59
|
+
}
|
|
60
|
+
if (scanned.shareds.length > 0) {
|
|
61
|
+
importItems.silgi.import.push({ name: "mergeShared", key: "mergeShared" });
|
|
62
|
+
}
|
|
63
|
+
if (scanned.schemas.length > 0) {
|
|
64
|
+
importItems.silgi.import.push({ name: "mergeSchemas", key: "mergeSchemas" });
|
|
65
|
+
}
|
|
66
|
+
for (const key in importItems) {
|
|
67
|
+
importItems[key].import = deduplicateImportsByKey(importItems[key].import);
|
|
68
|
+
}
|
|
69
|
+
const importsContent = [
|
|
70
|
+
...Object.entries(importItems).map(([_name, { from, import: imports }]) => {
|
|
71
|
+
if (silgi.options.typescript.removeFileExtension) {
|
|
72
|
+
from = from.replace(/\.(js|ts|mjs|cjs|jsx|tsx)$/, "");
|
|
73
|
+
}
|
|
74
|
+
return `import { ${imports.map(({ type, name }) => type ? `type ${name}` : name).join(", ")} } from '${from}'`;
|
|
75
|
+
}),
|
|
76
|
+
"",
|
|
77
|
+
...scanned.customImports,
|
|
78
|
+
""
|
|
79
|
+
];
|
|
80
|
+
const importData = [
|
|
81
|
+
`export const uris = ${JSON.stringify(scanned.uris, null, 2)}`,
|
|
82
|
+
"",
|
|
83
|
+
`export const modulesURIs = ${JSON.stringify(scanned.modulesURIs, null, 2)}`,
|
|
84
|
+
"",
|
|
85
|
+
scanned.schemas.length > 0 ? "export const schemas = mergeSchemas([" : "export const schemas = {",
|
|
86
|
+
...scanned.schemas.map((name) => {
|
|
87
|
+
return ` ${name},`;
|
|
88
|
+
}),
|
|
89
|
+
scanned.schemas.length > 0 ? "])" : "}",
|
|
90
|
+
"",
|
|
91
|
+
scanned.services.length > 0 ? "export const services = mergeServices([" : "export const services = {",
|
|
92
|
+
...scanned.services.map((name) => {
|
|
93
|
+
return ` ${name},`;
|
|
94
|
+
}),
|
|
95
|
+
scanned.services.length > 0 ? "])" : "}",
|
|
96
|
+
"",
|
|
97
|
+
scanned.shareds.length > 0 ? "export const shareds = mergeShared([" : "export const shareds = {",
|
|
98
|
+
...scanned.shareds.map((name) => {
|
|
99
|
+
return ` ${name},`;
|
|
100
|
+
}),
|
|
101
|
+
scanned.shareds.length > 0 ? "])" : "}",
|
|
102
|
+
""
|
|
103
|
+
];
|
|
104
|
+
await silgi.callHook("after:prepare:scan.ts", importData);
|
|
105
|
+
importData.unshift(...importsContent);
|
|
106
|
+
return importData;
|
|
107
|
+
}
|
|
108
|
+
function deduplicateImportsByKey(imports) {
|
|
109
|
+
const seenKeys = /* @__PURE__ */ new Map();
|
|
110
|
+
return imports.filter((item) => {
|
|
111
|
+
if (seenKeys.has(item.key)) {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
seenKeys.set(item.key, true);
|
|
115
|
+
return true;
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export { prepareServerFiles };
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { readdir, readFile } from 'node:fs/promises';
|
|
2
|
+
import { consola } from 'consola';
|
|
3
|
+
import { globby } from 'globby';
|
|
4
|
+
import { dirname, basename, relative, join, resolve, extname } from 'pathe';
|
|
5
|
+
import { hasError, hash, relativeWithDot } from 'silgi/kit';
|
|
6
|
+
import { withTrailingSlash } from 'ufo';
|
|
7
|
+
import { isIgnored } from '../utils/ignore.mjs';
|
|
8
|
+
import { SchemaParser } from '../utils/parser.mjs';
|
|
9
|
+
|
|
10
|
+
async function scanExportFile(silgi) {
|
|
11
|
+
const filePaths = /* @__PURE__ */ new Set();
|
|
12
|
+
const scannedPaths = [];
|
|
13
|
+
const dir = silgi.options.serverDir;
|
|
14
|
+
const files = (await globby(dir, { cwd: silgi.options.rootDir, ignore: silgi.options.ignore })).sort();
|
|
15
|
+
if (files.length) {
|
|
16
|
+
const siblings = await readdir(dirname(dir)).catch(() => []);
|
|
17
|
+
const directory = basename(dir);
|
|
18
|
+
if (!siblings.includes(directory)) {
|
|
19
|
+
const directoryLowerCase = directory.toLowerCase();
|
|
20
|
+
const caseCorrected = siblings.find((sibling) => sibling.toLowerCase() === directoryLowerCase);
|
|
21
|
+
if (caseCorrected) {
|
|
22
|
+
const original = relative(silgi.options.serverDir, dir);
|
|
23
|
+
const corrected = relative(silgi.options.serverDir, join(dirname(dir), caseCorrected));
|
|
24
|
+
consola.warn(`Components not scanned from \`~/${corrected}\`. Did you mean to name the directory \`~/${original}\` instead?`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
for (const _file of files) {
|
|
29
|
+
const filePath = resolve(dir, _file);
|
|
30
|
+
if (scannedPaths.find((d) => filePath.startsWith(withTrailingSlash(d))) || isIgnored(filePath, silgi)) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (filePaths.has(filePath)) {
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
filePaths.add(filePath);
|
|
37
|
+
if (silgi.options.extensions.includes(extname(filePath))) {
|
|
38
|
+
const parser = new SchemaParser({
|
|
39
|
+
debug: false
|
|
40
|
+
});
|
|
41
|
+
const readfile = await readFile(filePath, "utf-8");
|
|
42
|
+
const { exportVariables, parseInterfaceDeclarations } = parser.parseExports(readfile, filePath);
|
|
43
|
+
const createServices = exportVariables("createService", filePath);
|
|
44
|
+
if (hasError("Parser", silgi)) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const scanTS = [];
|
|
48
|
+
const schemaTS = [];
|
|
49
|
+
if (createServices.length > 0) {
|
|
50
|
+
scanTS.push(...createServices.map(({ exportName, path }) => {
|
|
51
|
+
const randomString = hash(basename(path) + exportName);
|
|
52
|
+
const _name = `_v${randomString}`;
|
|
53
|
+
return { exportName, path, _name, type: "service" };
|
|
54
|
+
}));
|
|
55
|
+
}
|
|
56
|
+
const createSchemas = exportVariables("createSchema", filePath);
|
|
57
|
+
if (hasError("Parser", silgi)) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (createSchemas.length > 0) {
|
|
61
|
+
scanTS.push(...createSchemas.map(({ exportName, path }) => {
|
|
62
|
+
const randomString = hash(basename(path) + exportName);
|
|
63
|
+
const _name = `_v${randomString}`;
|
|
64
|
+
return { exportName, path, _name, type: "schema" };
|
|
65
|
+
}));
|
|
66
|
+
}
|
|
67
|
+
const createShareds = exportVariables("createShared", filePath);
|
|
68
|
+
if (hasError("Parser", silgi)) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (createShareds.length > 0) {
|
|
72
|
+
scanTS.push(...createShareds.map(({ exportName, path }) => {
|
|
73
|
+
const randomString = hash(basename(path) + exportName);
|
|
74
|
+
const _name = `_v${randomString}`;
|
|
75
|
+
return { exportName, path, _name, type: "shared" };
|
|
76
|
+
}));
|
|
77
|
+
}
|
|
78
|
+
const sharedsTypes = parseInterfaceDeclarations("ExtendShared", filePath);
|
|
79
|
+
if (hasError("Parser", silgi)) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (sharedsTypes.length > 0) {
|
|
83
|
+
schemaTS.push(...sharedsTypes.map(({ exportName, path }) => {
|
|
84
|
+
const randomString = hash(basename(path) + exportName);
|
|
85
|
+
const _name = `_v${randomString}`;
|
|
86
|
+
return { exportName, path, _name, type: "shared" };
|
|
87
|
+
}));
|
|
88
|
+
}
|
|
89
|
+
const contextTypes = parseInterfaceDeclarations("ExtendContext", filePath);
|
|
90
|
+
if (hasError("Parser", silgi)) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (contextTypes.length > 0) {
|
|
94
|
+
schemaTS.push(...contextTypes.map(({ exportName, path }) => {
|
|
95
|
+
const randomString = hash(basename(path) + exportName);
|
|
96
|
+
const _name = `_v${randomString}`;
|
|
97
|
+
return { exportName, path, _name, type: "context" };
|
|
98
|
+
}));
|
|
99
|
+
}
|
|
100
|
+
silgi.hook("prepare:scan.ts", (options) => {
|
|
101
|
+
for (const { exportName, path, _name, type } of scanTS) {
|
|
102
|
+
if (!path.includes("vfs")) {
|
|
103
|
+
silgi.options.devServer.watch.push(path);
|
|
104
|
+
}
|
|
105
|
+
if (type === "service") {
|
|
106
|
+
options.services.push(_name);
|
|
107
|
+
}
|
|
108
|
+
if (type === "shared") {
|
|
109
|
+
options.shareds.push(_name);
|
|
110
|
+
}
|
|
111
|
+
if (type === "schema") {
|
|
112
|
+
options.schemas.push(_name);
|
|
113
|
+
}
|
|
114
|
+
options.importItems[path] ??= {
|
|
115
|
+
import: [],
|
|
116
|
+
from: relativeWithDot(silgi.options.silgi.serverDir, path)
|
|
117
|
+
};
|
|
118
|
+
options.importItems[path].import.push({
|
|
119
|
+
name: `${exportName} as ${_name}`,
|
|
120
|
+
key: _name
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
silgi.hook("prepare:schema.ts", (options) => {
|
|
125
|
+
for (const { exportName, path, _name, type } of schemaTS) {
|
|
126
|
+
if (!path.includes("vfs")) {
|
|
127
|
+
silgi.options.devServer.watch.push(path);
|
|
128
|
+
}
|
|
129
|
+
if (type === "shared") {
|
|
130
|
+
options.shareds.push({
|
|
131
|
+
key: _name,
|
|
132
|
+
value: _name
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
if (type === "context") {
|
|
136
|
+
options.contexts.push({
|
|
137
|
+
key: _name,
|
|
138
|
+
value: _name
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
options.importItems[path] ??= {
|
|
142
|
+
import: [],
|
|
143
|
+
from: relativeWithDot(silgi.options.build.typesDir, path)
|
|
144
|
+
};
|
|
145
|
+
options.importItems[path].import.push({
|
|
146
|
+
name: `${exportName} as ${_name}`,
|
|
147
|
+
key: _name
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export { scanExportFile };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { resolve, join } from 'pathe';
|
|
2
|
+
import { writeFile } from 'silgi/kit';
|
|
3
|
+
import { prepareConfigs } from './prepareConfigs.mjs';
|
|
4
|
+
import { prepareCoreFile } from './prepareCoreFile.mjs';
|
|
5
|
+
import { prepareFramework } from './prepareFramework.mjs';
|
|
6
|
+
|
|
7
|
+
async function writeCoreFile(silgi) {
|
|
8
|
+
const data = await prepareFramework(silgi);
|
|
9
|
+
const coreContent = await prepareCoreFile({
|
|
10
|
+
_importItems: data?.importItems ?? {},
|
|
11
|
+
_customImports: data?.customImports ?? []
|
|
12
|
+
}, data?.content ?? [], silgi);
|
|
13
|
+
const configs = await prepareConfigs(silgi);
|
|
14
|
+
const silgiDir = resolve(silgi.options.silgi.serverDir);
|
|
15
|
+
const buildFiles = [];
|
|
16
|
+
buildFiles.push({
|
|
17
|
+
path: join(silgiDir, "core.ts"),
|
|
18
|
+
contents: coreContent.join("\n")
|
|
19
|
+
});
|
|
20
|
+
buildFiles.push({
|
|
21
|
+
path: join(silgiDir, "configs.ts"),
|
|
22
|
+
contents: configs.join("\n")
|
|
23
|
+
});
|
|
24
|
+
for await (const file of buildFiles) {
|
|
25
|
+
if (!silgi.errors.length) {
|
|
26
|
+
await writeFile(
|
|
27
|
+
resolve(silgi.options.build.dir, file.path),
|
|
28
|
+
file.contents
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export { writeCoreFile };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { resolve } from 'pathe';
|
|
2
|
+
import { writeFile, parseServices } from 'silgi/kit';
|
|
3
|
+
import { buildUriMap } from '../utils/build-uri-map.mjs';
|
|
4
|
+
import { readScanFile } from '../utils/readScanFile.mjs';
|
|
5
|
+
import { prepareServerFiles } from './prepareServerFiles.mjs';
|
|
6
|
+
|
|
7
|
+
async function writeScanFiles(silgi) {
|
|
8
|
+
const data = await prepareServerFiles(silgi);
|
|
9
|
+
if (!silgi.errors.length) {
|
|
10
|
+
await writeFile(
|
|
11
|
+
resolve(silgi.options.silgi.serverDir, "scan.ts"),
|
|
12
|
+
data.join("\n")
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
await readScanFile(silgi);
|
|
16
|
+
buildUriMap(silgi);
|
|
17
|
+
parseServices(silgi);
|
|
18
|
+
silgi.hook("prepare:scan.ts", (file) => {
|
|
19
|
+
file.uris = {
|
|
20
|
+
...file.uris,
|
|
21
|
+
...silgi.uris
|
|
22
|
+
};
|
|
23
|
+
file.modulesURIs = {
|
|
24
|
+
...file.modulesURIs,
|
|
25
|
+
...silgi.modulesURIs
|
|
26
|
+
};
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { writeScanFiles };
|
|
@@ -0,0 +1,109 @@
|
|
|
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 { isDirectory, writeFile } from 'silgi/kit';
|
|
6
|
+
import { runtimeDir } from 'silgi/runtime/meta';
|
|
7
|
+
import { toExports } from 'unimport';
|
|
8
|
+
import { generateRouterDTS } from '../utils/generateRouterDTS.mjs';
|
|
9
|
+
import { createDTSFramework } from './prepareFramework.mjs';
|
|
10
|
+
import { prepareSchema } from './prepareSchema.mjs';
|
|
11
|
+
import { silgiGenerateType } from '../config/types.mjs';
|
|
12
|
+
|
|
13
|
+
async function writeTypesAndFiles(silgi) {
|
|
14
|
+
const routerDTS = await generateRouterDTS(silgi);
|
|
15
|
+
silgi.hook("prepare:types", (opts) => {
|
|
16
|
+
opts.references.push({ path: "./schema.d.ts" });
|
|
17
|
+
opts.references.push({ path: "./silgi-routes.d.ts" });
|
|
18
|
+
opts.references.push({ path: "./framework.d.ts" });
|
|
19
|
+
});
|
|
20
|
+
const schemaContent = await prepareSchema(silgi);
|
|
21
|
+
const frameworkDTS = await createDTSFramework(silgi);
|
|
22
|
+
const { declarations, tsConfig } = await silgiGenerateType(silgi);
|
|
23
|
+
const tsConfigPath = resolve(
|
|
24
|
+
silgi.options.rootDir,
|
|
25
|
+
silgi.options.typescript.tsconfigPath
|
|
26
|
+
);
|
|
27
|
+
const typesDir = resolve(silgi.options.build.typesDir);
|
|
28
|
+
let autoImportedTypes = [];
|
|
29
|
+
let autoImportExports = "";
|
|
30
|
+
if (silgi.unimport) {
|
|
31
|
+
await silgi.unimport.init();
|
|
32
|
+
const allImports = await silgi.unimport.getImports();
|
|
33
|
+
autoImportExports = toExports(allImports).replace(
|
|
34
|
+
/#internal\/nitro/g,
|
|
35
|
+
relative(typesDir, runtimeDir)
|
|
36
|
+
);
|
|
37
|
+
const resolvedImportPathMap = /* @__PURE__ */ new Map();
|
|
38
|
+
for (const i of allImports.filter((i2) => !i2.type)) {
|
|
39
|
+
if (resolvedImportPathMap.has(i.from)) {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
let path = resolveAlias(i.from, silgi.options.alias);
|
|
43
|
+
if (isAbsolute(path)) {
|
|
44
|
+
const resolvedPath = await resolvePath(i.from, {
|
|
45
|
+
url: silgi.options.nodeModulesDirs
|
|
46
|
+
}).catch(() => null);
|
|
47
|
+
if (resolvedPath) {
|
|
48
|
+
const { dir, name } = parseNodeModulePath(resolvedPath);
|
|
49
|
+
if (!dir || !name) {
|
|
50
|
+
path = resolvedPath;
|
|
51
|
+
} else {
|
|
52
|
+
const subpath = await lookupNodeModuleSubpath(resolvedPath);
|
|
53
|
+
path = join(dir, name, subpath || "");
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (existsSync(path) && !await isDirectory(path)) {
|
|
58
|
+
path = path.replace(/\.[a-z]+$/, "");
|
|
59
|
+
}
|
|
60
|
+
if (isAbsolute(path)) {
|
|
61
|
+
path = relative(typesDir, path);
|
|
62
|
+
}
|
|
63
|
+
resolvedImportPathMap.set(i.from, path);
|
|
64
|
+
}
|
|
65
|
+
autoImportedTypes = [
|
|
66
|
+
silgi.options.imports && silgi.options.imports.autoImport !== false ? (await silgi.unimport.generateTypeDeclarations({
|
|
67
|
+
exportHelper: false,
|
|
68
|
+
resolvePath: (i) => resolvedImportPathMap.get(i.from) ?? i.from
|
|
69
|
+
})).trim() : ""
|
|
70
|
+
];
|
|
71
|
+
}
|
|
72
|
+
const buildFiles = [];
|
|
73
|
+
buildFiles.push({
|
|
74
|
+
path: join(typesDir, "silgi-routes.d.ts"),
|
|
75
|
+
contents: routerDTS.join("\n")
|
|
76
|
+
});
|
|
77
|
+
buildFiles.push({
|
|
78
|
+
path: join(typesDir, "silgi-imports.d.ts"),
|
|
79
|
+
contents: [...autoImportedTypes, autoImportExports || "export {}"].join(
|
|
80
|
+
"\n"
|
|
81
|
+
)
|
|
82
|
+
});
|
|
83
|
+
buildFiles.push({
|
|
84
|
+
path: join(typesDir, "schema.d.ts"),
|
|
85
|
+
contents: schemaContent.join("\n")
|
|
86
|
+
});
|
|
87
|
+
buildFiles.push({
|
|
88
|
+
path: join(typesDir, "silgi.d.ts"),
|
|
89
|
+
contents: declarations.join("\n")
|
|
90
|
+
});
|
|
91
|
+
buildFiles.push({
|
|
92
|
+
path: tsConfigPath,
|
|
93
|
+
contents: JSON.stringify(tsConfig, null, 2)
|
|
94
|
+
});
|
|
95
|
+
buildFiles.push({
|
|
96
|
+
path: join(typesDir, "framework.d.ts"),
|
|
97
|
+
contents: frameworkDTS.content.join("\n")
|
|
98
|
+
});
|
|
99
|
+
for await (const file of buildFiles) {
|
|
100
|
+
if (!silgi.errors.length) {
|
|
101
|
+
await writeFile(
|
|
102
|
+
resolve(silgi.options.build.dir, file.path),
|
|
103
|
+
file.contents
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export { writeTypesAndFiles };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { defu } from 'defu';
|
|
2
|
+
|
|
3
|
+
function buildUriMap(silgi, currentPath = []) {
|
|
4
|
+
const uriMap = /* @__PURE__ */ new Map();
|
|
5
|
+
function traverse(node, path = []) {
|
|
6
|
+
if (!node || typeof node !== "object")
|
|
7
|
+
return;
|
|
8
|
+
if (path.length === 4) {
|
|
9
|
+
const basePath = path.join("/");
|
|
10
|
+
let pathString = "";
|
|
11
|
+
if (node.pathParams) {
|
|
12
|
+
let paths = null;
|
|
13
|
+
if (node.pathParams?._def?.typeName !== void 0) {
|
|
14
|
+
try {
|
|
15
|
+
const shape = node.pathParams?.shape;
|
|
16
|
+
paths = shape ? Object.keys(shape) : null;
|
|
17
|
+
} catch {
|
|
18
|
+
paths = null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (paths?.length) {
|
|
22
|
+
pathString = paths.map((p) => `:${p}`).join("/");
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
uriMap.set(basePath, pathString);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
for (const key in node) {
|
|
29
|
+
if (!["_type", "fields"].includes(key)) {
|
|
30
|
+
traverse(node[key], [...path, key]);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
traverse(silgi.schemas, currentPath);
|
|
35
|
+
silgi.uris = defu(silgi.uris, Object.fromEntries(uriMap));
|
|
36
|
+
return uriMap;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export { buildUriMap };
|