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
package/dist/kit/esm.mjs
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { pathToFileURL } from 'node:url';
|
|
2
|
+
import { resolvePath } from 'mlly';
|
|
3
|
+
|
|
4
|
+
function directoryToURL(dir) {
|
|
5
|
+
return pathToFileURL(`${dir}/`);
|
|
6
|
+
}
|
|
7
|
+
async function tryResolveModule(id, url = import.meta.url) {
|
|
8
|
+
try {
|
|
9
|
+
return await resolvePath(id, { url });
|
|
10
|
+
} catch {
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { directoryToURL, tryResolveModule };
|
package/dist/kit/fs.mjs
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import fsp from 'node:fs/promises';
|
|
2
|
+
import { consola } from 'consola';
|
|
3
|
+
import { dirname } from 'pathe';
|
|
4
|
+
import { useSilgiCLI } from 'silgi';
|
|
5
|
+
import { prettyPath } from './path.mjs';
|
|
6
|
+
|
|
7
|
+
async function writeFile(file, contents, log = false) {
|
|
8
|
+
const silgi = useSilgiCLI();
|
|
9
|
+
if (silgi.errors.length) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
await fsp.mkdir(dirname(file), { recursive: true });
|
|
13
|
+
await fsp.writeFile(
|
|
14
|
+
file,
|
|
15
|
+
contents,
|
|
16
|
+
typeof contents === "string" ? "utf8" : void 0
|
|
17
|
+
);
|
|
18
|
+
if (log) {
|
|
19
|
+
consola.info("Generated", prettyPath(file));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async function isDirectory(path) {
|
|
23
|
+
try {
|
|
24
|
+
return (await fsp.stat(path)).isDirectory();
|
|
25
|
+
} catch {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { isDirectory, writeFile };
|
package/dist/kit/gen.mjs
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { genString, genObjectFromRaw, genObjectFromValues, genObjectFromRawEntries } from 'knitwork';
|
|
2
|
+
|
|
3
|
+
const reservedCode = /* @__PURE__ */ new Set([
|
|
4
|
+
"process",
|
|
5
|
+
"global",
|
|
6
|
+
"runtime",
|
|
7
|
+
"runtimeConfig"
|
|
8
|
+
]);
|
|
9
|
+
function genEnsureSafeVar(name) {
|
|
10
|
+
if (typeof name !== "string") {
|
|
11
|
+
return genString(name);
|
|
12
|
+
}
|
|
13
|
+
for (const reserved of reservedCode) {
|
|
14
|
+
if (name === reserved || name.startsWith(`${reserved}.`)) {
|
|
15
|
+
return name;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return genString(name);
|
|
19
|
+
}
|
|
20
|
+
function getAllEntries(obj) {
|
|
21
|
+
return Object.entries(obj).map(([
|
|
22
|
+
key,
|
|
23
|
+
value
|
|
24
|
+
]) => {
|
|
25
|
+
if (typeof value === "function") {
|
|
26
|
+
return [key, genObjectFromRaw(value)];
|
|
27
|
+
}
|
|
28
|
+
if (typeof value === "string") {
|
|
29
|
+
return [key, genEnsureSafeVar(value)];
|
|
30
|
+
}
|
|
31
|
+
if (typeof value === "object" && value !== null) {
|
|
32
|
+
if (Array.isArray(value)) {
|
|
33
|
+
return [key, genObjectFromValues(value.map(
|
|
34
|
+
(item) => typeof item === "object" && item !== null ? genObjectFromRawEntries(getAllEntries(item)) : item
|
|
35
|
+
))];
|
|
36
|
+
} else {
|
|
37
|
+
return [key, genObjectFromRawEntries(getAllEntries(value))];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return [key, value];
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export { genEnsureSafeVar, getAllEntries };
|
package/dist/kit/index.mjs
CHANGED
|
@@ -1,528 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
import { hash as hash$2 } from 'silgi/kit';
|
|
17
|
-
import { genString, genObjectFromRaw, genObjectFromValues, genObjectFromRawEntries } from 'knitwork';
|
|
18
|
-
import 'semver/functions/satisfies.js';
|
|
19
|
-
import 'silgi/meta';
|
|
20
|
-
|
|
21
|
-
function useLogger(tag, options = {}) {
|
|
22
|
-
return tag ? consola.create(options).withTag(tag) : consola;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function hasError(type, silgi) {
|
|
26
|
-
silgi = silgi ?? tryUseSilgiCLI() ?? void 0;
|
|
27
|
-
if (silgi && silgi.errors.some((error) => error.type === type)) {
|
|
28
|
-
return true;
|
|
29
|
-
}
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function prettyPath(p, highlight = true) {
|
|
34
|
-
p = relative(process.cwd(), p);
|
|
35
|
-
return highlight ? colors.cyan(p) : p;
|
|
36
|
-
}
|
|
37
|
-
function resolveSilgiPath(path, silgiCLIOptions, base) {
|
|
38
|
-
if (typeof path !== "string") {
|
|
39
|
-
throw new TypeError(`Invalid path: ${path}`);
|
|
40
|
-
}
|
|
41
|
-
path = _compilePathTemplate(path)(silgiCLIOptions);
|
|
42
|
-
for (const base2 in silgiCLIOptions.alias) {
|
|
43
|
-
if (path.startsWith(base2)) {
|
|
44
|
-
path = silgiCLIOptions.alias[base2] + path.slice(base2.length);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return resolve(base || silgiCLIOptions.srcDir, path);
|
|
48
|
-
}
|
|
49
|
-
function _compilePathTemplate(contents) {
|
|
50
|
-
return (params) => contents.replace(/\{\{ ?([\w.]+) ?\}\}/g, (_, match) => {
|
|
51
|
-
const val = getProperty(params, match);
|
|
52
|
-
if (!val) {
|
|
53
|
-
consola$1.warn(
|
|
54
|
-
`cannot resolve template param '${match}' in ${contents.slice(0, 20)}`
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
return val || `${match}`;
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
async function writeFile(file, contents, log = false) {
|
|
62
|
-
const silgi = useSilgiCLI();
|
|
63
|
-
if (silgi.errors.length) {
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
await fsp.mkdir(dirname(file), { recursive: true });
|
|
67
|
-
await fsp.writeFile(
|
|
68
|
-
file,
|
|
69
|
-
contents,
|
|
70
|
-
typeof contents === "string" ? "utf8" : void 0
|
|
71
|
-
);
|
|
72
|
-
if (log) {
|
|
73
|
-
consola.info("Generated", prettyPath(file));
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
async function isDirectory$1(path) {
|
|
77
|
-
try {
|
|
78
|
-
return (await fsp.stat(path)).isDirectory();
|
|
79
|
-
} catch {
|
|
80
|
-
return false;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function hash(data) {
|
|
85
|
-
return camelCase(hash$1(data));
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function defineSilgiModule(definition) {
|
|
89
|
-
if (definition) {
|
|
90
|
-
return _defineSilgiModule(definition);
|
|
91
|
-
}
|
|
92
|
-
return {
|
|
93
|
-
with: (definition2) => _defineSilgiModule(definition2)
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
function _defineSilgiModule(definition) {
|
|
97
|
-
if (typeof definition === "function") {
|
|
98
|
-
return _defineSilgiModule({ setup: definition });
|
|
99
|
-
}
|
|
100
|
-
const module = defu(definition, { meta: {} });
|
|
101
|
-
module.meta.configKey ||= module.meta.name;
|
|
102
|
-
async function getOptions(inlineOptions, silgi = useSilgiCLI()) {
|
|
103
|
-
const nuxtConfigOptionsKey = module.meta.configKey || module.meta.name;
|
|
104
|
-
const nuxtConfigOptions = nuxtConfigOptionsKey && nuxtConfigOptionsKey in silgi.options ? silgi.options[nuxtConfigOptionsKey] : {};
|
|
105
|
-
const optionsDefaults = typeof module.defaults === "function" ? await module.defaults(silgi) : module.defaults ?? {};
|
|
106
|
-
const options = defu(inlineOptions, nuxtConfigOptions, optionsDefaults);
|
|
107
|
-
return Promise.resolve(options);
|
|
108
|
-
}
|
|
109
|
-
async function silgiNormalizedModule(inlineOptions, silgi = tryUseSilgiCLI()) {
|
|
110
|
-
if (!silgi) {
|
|
111
|
-
throw new TypeError("Cannot use module outside of Silgi context");
|
|
112
|
-
}
|
|
113
|
-
const uniqueKey = module.meta.configKey || module.meta.name;
|
|
114
|
-
if (uniqueKey && !silgi.options.isPreparingModules) {
|
|
115
|
-
silgi._requiredModules ||= {};
|
|
116
|
-
if (silgi._requiredModules[uniqueKey]) {
|
|
117
|
-
return false;
|
|
118
|
-
}
|
|
119
|
-
silgi._requiredModules[uniqueKey] = true;
|
|
120
|
-
}
|
|
121
|
-
if (module.meta.compatibility) {
|
|
122
|
-
const issues = await checkSilgiCompatibility(module.meta.compatibility, silgi);
|
|
123
|
-
if (issues.length) {
|
|
124
|
-
silgi.logger.warn(`Module \`${module.meta.name}\` is disabled due to incompatibility issues:
|
|
125
|
-
${issues.toString()}`);
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
const _options = await getOptions(inlineOptions, silgi);
|
|
130
|
-
if (module.hooks) {
|
|
131
|
-
silgi.hooks.addHooks(module.hooks);
|
|
132
|
-
}
|
|
133
|
-
const start = performance.now();
|
|
134
|
-
const res = await module.setup?.call(null, _options, silgi) ?? {};
|
|
135
|
-
const perf = performance.now() - start;
|
|
136
|
-
const setupTime = Math.round(perf * 100) / 100;
|
|
137
|
-
if (setupTime > 5e3 && uniqueKey !== "@silgi/telemetry") {
|
|
138
|
-
silgi.logger.warn(`Slow module \`${uniqueKey || "<no name>"}\` took \`${setupTime}ms\` to setup.`);
|
|
139
|
-
} else if (silgi.options.debug) {
|
|
140
|
-
silgi.logger.info(`Module \`${uniqueKey || "<no name>"}\` took \`${setupTime}ms\` to setup.`);
|
|
141
|
-
}
|
|
142
|
-
if (res === false) {
|
|
143
|
-
return false;
|
|
144
|
-
}
|
|
145
|
-
return defu(res, {
|
|
146
|
-
timings: {
|
|
147
|
-
setup: setupTime
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
silgiNormalizedModule.getMeta = () => Promise.resolve(module.meta);
|
|
152
|
-
silgiNormalizedModule.getOptions = getOptions;
|
|
153
|
-
return silgiNormalizedModule;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function serviceParseModule(params) {
|
|
157
|
-
return params;
|
|
158
|
-
}
|
|
159
|
-
const parseRouteRules = serviceParseModule(({
|
|
160
|
-
node,
|
|
161
|
-
basePath,
|
|
162
|
-
silgi,
|
|
163
|
-
pathLength
|
|
164
|
-
}) => {
|
|
165
|
-
const routeRules = node.routeRules;
|
|
166
|
-
if (!routeRules)
|
|
167
|
-
return;
|
|
168
|
-
let removeMethod = "";
|
|
169
|
-
removeMethod = withLeadingSlash(basePath);
|
|
170
|
-
if (pathLength === 1 || pathLength === 2 || pathLength === 3) {
|
|
171
|
-
if (routeRules.doubleSplat === void 0) {
|
|
172
|
-
routeRules.doubleSplat = true;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
if (routeRules.splat)
|
|
176
|
-
removeMethod = `${removeMethod}/*`;
|
|
177
|
-
else if (routeRules.doubleSplat)
|
|
178
|
-
removeMethod = `${removeMethod}/**`;
|
|
179
|
-
removeMethod = removeMethod.replace(/\/{2,}/g, "/");
|
|
180
|
-
delete routeRules.splat;
|
|
181
|
-
delete routeRules.doubleSplat;
|
|
182
|
-
silgi.routeRules.addRule(removeMethod, routeRules);
|
|
183
|
-
});
|
|
184
|
-
function parseServices(silgi, pathLength = [4, 3, 2, 1], servicesObject, currentPath = []) {
|
|
185
|
-
const modulesURIs = {};
|
|
186
|
-
silgi.options.serviceParseModules.push(parseRouteRules);
|
|
187
|
-
const pathLengths = Array.isArray(pathLength) ? pathLength : [pathLength];
|
|
188
|
-
function traverse(node, path = []) {
|
|
189
|
-
if (!node || typeof node !== "object")
|
|
190
|
-
return;
|
|
191
|
-
if (pathLengths.includes(path.length)) {
|
|
192
|
-
const basePath = path.join("/");
|
|
193
|
-
for (const handler of silgi.options.serviceParseModules) {
|
|
194
|
-
handler({ node, basePath, silgi, modulesURIs, pathLength: path.length });
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
for (const key in node) {
|
|
198
|
-
if (!["_type", "fields"].includes(key)) {
|
|
199
|
-
traverse(node[key], [...path, key]);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
traverse(servicesObject || silgi.services, currentPath);
|
|
204
|
-
silgi.options.serviceParseModules = [];
|
|
205
|
-
silgi.modulesURIs = defu(modulesURIs, silgi.modulesURIs);
|
|
206
|
-
return modulesURIs;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
function defineSilgiPreset(preset, meta) {
|
|
210
|
-
if (meta?.url && typeof preset !== "function") ;
|
|
211
|
-
return { ...preset, _meta: meta };
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
async function resolvePath(path, opts = {}) {
|
|
215
|
-
const _path = path;
|
|
216
|
-
path = normalize(path);
|
|
217
|
-
if (isAbsolute(path)) {
|
|
218
|
-
if (existsSync(path) && !await isDirectory(path)) {
|
|
219
|
-
return path;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
const cwd = opts.cwd || process.cwd();
|
|
223
|
-
const extensions = opts.extensions || [".ts", ".mjs", ".cjs", ".json"];
|
|
224
|
-
const modulesDir = opts.modulesDir || [];
|
|
225
|
-
path = resolveAlias(path);
|
|
226
|
-
if (!isAbsolute(path)) {
|
|
227
|
-
path = resolve(cwd, path);
|
|
228
|
-
}
|
|
229
|
-
let _isDir = false;
|
|
230
|
-
if (existsSync(path)) {
|
|
231
|
-
_isDir = await isDirectory(path);
|
|
232
|
-
if (!_isDir) {
|
|
233
|
-
return path;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
for (const ext of extensions) {
|
|
237
|
-
const pathWithExt = path + ext;
|
|
238
|
-
if (existsSync(pathWithExt)) {
|
|
239
|
-
return pathWithExt;
|
|
240
|
-
}
|
|
241
|
-
const pathWithIndex = join(path, `index${ext}`);
|
|
242
|
-
if (_isDir && existsSync(pathWithIndex)) {
|
|
243
|
-
return pathWithIndex;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
const resolveModulePath = await resolvePath$1(_path, { url: [cwd, ...modulesDir] }).catch(() => null);
|
|
247
|
-
if (resolveModulePath) {
|
|
248
|
-
return resolveModulePath;
|
|
249
|
-
}
|
|
250
|
-
return opts.fallbackToOriginal ? _path : path;
|
|
251
|
-
}
|
|
252
|
-
async function isDirectory(path) {
|
|
253
|
-
return (await promises.lstat(path)).isDirectory();
|
|
254
|
-
}
|
|
255
|
-
function resolveAlias(path, alias) {
|
|
256
|
-
alias ||= tryUseSilgiCLI()?.options.alias || {};
|
|
257
|
-
return resolveAlias$1(path, alias || {});
|
|
258
|
-
}
|
|
259
|
-
function createResolver(base) {
|
|
260
|
-
if (!base) {
|
|
261
|
-
throw new Error("`base` argument is missing for createResolver(base)!");
|
|
262
|
-
}
|
|
263
|
-
base = base.toString();
|
|
264
|
-
if (base.startsWith("file://")) {
|
|
265
|
-
base = dirname(fileURLToPath(base));
|
|
266
|
-
}
|
|
267
|
-
return {
|
|
268
|
-
resolve: (...path) => resolve(base, ...path),
|
|
269
|
-
resolvePath: (path, opts) => resolvePath(path, { cwd: base, ...opts })
|
|
270
|
-
};
|
|
271
|
-
}
|
|
272
|
-
async function resolveSilgiModule(base, paths) {
|
|
273
|
-
const resolved = [];
|
|
274
|
-
const resolver = createResolver(base);
|
|
275
|
-
for (const path of paths) {
|
|
276
|
-
if (path.startsWith(base)) {
|
|
277
|
-
resolved.push(path.split("/index.ts")[0]);
|
|
278
|
-
} else {
|
|
279
|
-
const resolvedPath = await resolver.resolvePath(path);
|
|
280
|
-
resolved.push(resolvedPath.slice(0, resolvedPath.lastIndexOf(path) + path.length));
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
return resolved;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
const RELATIVE_RE = /^([^.])/;
|
|
287
|
-
function relativeWithDot(from, to) {
|
|
288
|
-
return relative(from, to).replace(RELATIVE_RE, "./$1") || ".";
|
|
289
|
-
}
|
|
290
|
-
function toArray(value) {
|
|
291
|
-
return Array.isArray(value) ? value : [value];
|
|
292
|
-
}
|
|
293
|
-
function filterInPlace(array, predicate) {
|
|
294
|
-
for (let i = array.length; i--; i >= 0) {
|
|
295
|
-
if (!predicate(array[i], i, array)) {
|
|
296
|
-
array.splice(i, 1);
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
return array;
|
|
300
|
-
}
|
|
301
|
-
const MODE_RE = /\.(server|client)(\.\w+)*$/;
|
|
302
|
-
function hasSilgiModule(moduleKey, silgi = useSilgiCLI()) {
|
|
303
|
-
return silgi.scanModules.some(({ meta }) => meta.configKey === moduleKey) || Object.keys(silgi.scanModules).includes(moduleKey);
|
|
304
|
-
}
|
|
305
|
-
function hasInstalledModule(moduleKey, silgi = useSilgiCLI()) {
|
|
306
|
-
const find = silgi.scanModules.find(({ meta }) => meta.configKey === moduleKey);
|
|
307
|
-
return find?.installed ?? false;
|
|
308
|
-
}
|
|
309
|
-
const baseHeaderBannerComment = [
|
|
310
|
-
"// DO NOT EDIT THIS FILE",
|
|
311
|
-
"// This file is generated by Silgi",
|
|
312
|
-
"/* eslint-disable */",
|
|
313
|
-
"/* prettier-ignore */",
|
|
314
|
-
"/* tslint:disable */"
|
|
315
|
-
];
|
|
316
|
-
function processFilePath(src) {
|
|
317
|
-
const silgi = useSilgiCLI();
|
|
318
|
-
if (silgi.options.typescript.removeFileExtension) {
|
|
319
|
-
src = src.replace(/\.ts$/, "");
|
|
320
|
-
return src;
|
|
321
|
-
}
|
|
322
|
-
return src;
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
function addTemplate(_template) {
|
|
326
|
-
const silgi = useSilgiCLI();
|
|
327
|
-
const template = normalizeTemplate(_template);
|
|
328
|
-
filterInPlace(silgi.options.build.templates, (p) => normalizeTemplate(p).dst !== template.dst);
|
|
329
|
-
silgi.options.build.templates.push(template);
|
|
330
|
-
return template;
|
|
331
|
-
}
|
|
332
|
-
function normalizeTemplate(template, buildDir) {
|
|
333
|
-
if (!template) {
|
|
334
|
-
throw new Error(`Invalid template: ${JSON.stringify(template)}`);
|
|
335
|
-
}
|
|
336
|
-
if (typeof template === "string") {
|
|
337
|
-
template = { src: template };
|
|
338
|
-
} else {
|
|
339
|
-
template = { ...template };
|
|
340
|
-
}
|
|
341
|
-
if (template.src) {
|
|
342
|
-
if (!existsSync(template.src)) {
|
|
343
|
-
throw new Error(`Template not found: ${template.src}`);
|
|
344
|
-
}
|
|
345
|
-
if (!template.filename) {
|
|
346
|
-
const srcPath = parse(template.src);
|
|
347
|
-
template.filename = template.fileName || `${basename(srcPath.dir)}.${srcPath.name}.${hash$2(template.src)}${srcPath.ext}`;
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
if (!template.src && !template.getContents) {
|
|
351
|
-
throw new Error(`Invalid template. Either \`getContents\` or \`src\` should be provided: ${JSON.stringify(template)}`);
|
|
352
|
-
}
|
|
353
|
-
if (!template.filename) {
|
|
354
|
-
throw new Error(`Invalid template. \`filename\` must be provided: ${JSON.stringify(template)}`);
|
|
355
|
-
}
|
|
356
|
-
if (template.filename.endsWith(".d.ts")) {
|
|
357
|
-
template.write = true;
|
|
358
|
-
}
|
|
359
|
-
if (!template.dst) {
|
|
360
|
-
const silgi = useSilgiCLI();
|
|
361
|
-
const dir = template.where === ".silgi" ? silgi.options.build.dir : template.where === "server" ? silgi.options.silgi.serverDir : template.where === "client" ? silgi.options.silgi.clientDir : silgi.options.silgi.serverDir;
|
|
362
|
-
template.dst = resolve(buildDir ?? dir, template.filename);
|
|
363
|
-
}
|
|
364
|
-
return template;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
function useRequest(event) {
|
|
368
|
-
const silgi = useSilgi();
|
|
369
|
-
if (silgi.options.present === "nuxt" || silgi.options.present === "nitro" || silgi.options.present === "h3") {
|
|
370
|
-
return event.node.req;
|
|
371
|
-
}
|
|
372
|
-
return event;
|
|
373
|
-
}
|
|
374
|
-
function useResponse(event) {
|
|
375
|
-
const silgi = useSilgi();
|
|
376
|
-
if (silgi.options.present === "nuxt" || silgi.options.present === "nitro" || silgi.options.present === "h3") {
|
|
377
|
-
return event.node.res;
|
|
378
|
-
}
|
|
379
|
-
return event;
|
|
380
|
-
}
|
|
381
|
-
function getIpAddress(event) {
|
|
382
|
-
const silgi = useSilgi();
|
|
383
|
-
if (silgi.options.present === "nuxt" || silgi.options.present === "nitro" || silgi.options.present === "h3") {
|
|
384
|
-
const _ipAddress = ipAddress(event.node.req);
|
|
385
|
-
return _ipAddress;
|
|
386
|
-
}
|
|
387
|
-
return false;
|
|
388
|
-
}
|
|
389
|
-
function ipAddress(req) {
|
|
390
|
-
const headers = [
|
|
391
|
-
"cf-connecting-ip",
|
|
392
|
-
// Cloudflare
|
|
393
|
-
"true-client-ip",
|
|
394
|
-
// Akamai and Cloudflare
|
|
395
|
-
"x-real-ip",
|
|
396
|
-
// Nginx proxy/FastCGI
|
|
397
|
-
"x-forwarded-for",
|
|
398
|
-
// Standard proxy header
|
|
399
|
-
"x-client-ip",
|
|
400
|
-
// Apache
|
|
401
|
-
"x-forwarded",
|
|
402
|
-
// Alternative format
|
|
403
|
-
"forwarded",
|
|
404
|
-
// RFC 7239 compliant format
|
|
405
|
-
"forwarded-for",
|
|
406
|
-
// RFC 7239 variant
|
|
407
|
-
"x-appengine-user-ip",
|
|
408
|
-
// Google App Engine
|
|
409
|
-
"x-cluster-client-ip",
|
|
410
|
-
// GCP load balancer
|
|
411
|
-
"fastly-client-ip",
|
|
412
|
-
// Fastly CDN
|
|
413
|
-
"x-azure-clientip",
|
|
414
|
-
// Azure
|
|
415
|
-
"x-original-forwarded-for",
|
|
416
|
-
// Secondary forwarded-for header
|
|
417
|
-
"x-lb-forwarded-for",
|
|
418
|
-
// Load balancer specific
|
|
419
|
-
"cdn-loop"
|
|
420
|
-
// Used by some CDNs
|
|
421
|
-
];
|
|
422
|
-
for (const header of headers) {
|
|
423
|
-
const value = req.headers[header];
|
|
424
|
-
if (!value)
|
|
425
|
-
continue;
|
|
426
|
-
let ip;
|
|
427
|
-
if (Array.isArray(value)) {
|
|
428
|
-
ip = value[0];
|
|
429
|
-
} else if (header === "x-forwarded-for" || header === "forwarded-for" || header === "x-original-forwarded-for") {
|
|
430
|
-
ip = value.split(",")[0];
|
|
431
|
-
} else if (header === "forwarded") {
|
|
432
|
-
const match = value.match(/for=([^;]+)/);
|
|
433
|
-
ip = match ? match[1].trim().replace(/^\[|\]$/g, "") : "";
|
|
434
|
-
} else {
|
|
435
|
-
ip = value;
|
|
436
|
-
}
|
|
437
|
-
const cleanIp = ip.trim();
|
|
438
|
-
if (cleanIp && isValidIp(cleanIp)) {
|
|
439
|
-
return cleanIp;
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
const socketIp = req.socket.remoteAddress ?? "";
|
|
443
|
-
return socketIp && isValidIp(socketIp) ? socketIp : "";
|
|
444
|
-
}
|
|
445
|
-
function isValidIp(ip) {
|
|
446
|
-
if (ip === "::1" || ip === "localhost" || ip === "127.0.0.1")
|
|
447
|
-
return true;
|
|
448
|
-
if (/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(ip))
|
|
449
|
-
return true;
|
|
450
|
-
if (/^[0-9a-f:]+$/i.test(ip))
|
|
451
|
-
return true;
|
|
452
|
-
return false;
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
function directoryToURL(dir) {
|
|
456
|
-
return pathToFileURL(`${dir}/`);
|
|
457
|
-
}
|
|
458
|
-
async function tryResolveModule(id, url = import.meta.url) {
|
|
459
|
-
try {
|
|
460
|
-
return await resolvePath$1(id, { url });
|
|
461
|
-
} catch {
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
const reservedCode = /* @__PURE__ */ new Set([
|
|
466
|
-
"process",
|
|
467
|
-
"global",
|
|
468
|
-
"runtime",
|
|
469
|
-
"runtimeConfig"
|
|
470
|
-
]);
|
|
471
|
-
function genEnsureSafeVar(name) {
|
|
472
|
-
if (typeof name !== "string") {
|
|
473
|
-
return genString(name);
|
|
474
|
-
}
|
|
475
|
-
for (const reserved of reservedCode) {
|
|
476
|
-
if (name === reserved || name.startsWith(`${reserved}.`)) {
|
|
477
|
-
return name;
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
return genString(name);
|
|
481
|
-
}
|
|
482
|
-
function getAllEntries(obj) {
|
|
483
|
-
return Object.entries(obj).map(([
|
|
484
|
-
key,
|
|
485
|
-
value
|
|
486
|
-
]) => {
|
|
487
|
-
if (typeof value === "function") {
|
|
488
|
-
return [key, genObjectFromRaw(value)];
|
|
489
|
-
}
|
|
490
|
-
if (typeof value === "string") {
|
|
491
|
-
return [key, genEnsureSafeVar(value)];
|
|
492
|
-
}
|
|
493
|
-
if (typeof value === "object" && value !== null) {
|
|
494
|
-
if (Array.isArray(value)) {
|
|
495
|
-
return [key, genObjectFromValues(value.map(
|
|
496
|
-
(item) => typeof item === "object" && item !== null ? genObjectFromRawEntries(getAllEntries(item)) : item
|
|
497
|
-
))];
|
|
498
|
-
} else {
|
|
499
|
-
return [key, genObjectFromRawEntries(getAllEntries(value))];
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
return [key, value];
|
|
503
|
-
});
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
function isNuxt() {
|
|
507
|
-
const silgi = useSilgi();
|
|
508
|
-
if (silgi.options.present === "nitro" || silgi.options.present === "h3" || silgi.options.present === "nuxt") {
|
|
509
|
-
return true;
|
|
510
|
-
}
|
|
511
|
-
return false;
|
|
512
|
-
}
|
|
513
|
-
function isNitro() {
|
|
514
|
-
const silgi = useSilgi();
|
|
515
|
-
if (silgi.options.present === "nitro" || silgi.options.present === "h3") {
|
|
516
|
-
return true;
|
|
517
|
-
}
|
|
518
|
-
return false;
|
|
519
|
-
}
|
|
520
|
-
function isH3() {
|
|
521
|
-
const silgi = useSilgi();
|
|
522
|
-
if (silgi.options.present === "h3") {
|
|
523
|
-
return true;
|
|
524
|
-
}
|
|
525
|
-
return false;
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
export { MODE_RE, addTemplate, baseHeaderBannerComment, createResolver, defineSilgiModule, defineSilgiPreset, directoryToURL, filterInPlace, genEnsureSafeVar, getAllEntries, getIpAddress, hasError, hasInstalledModule, hasSilgiModule, hash, ipAddress, isDirectory$1 as isDirectory, isH3, isNitro, isNuxt, normalizeTemplate, parseServices, prettyPath, processFilePath, relativeWithDot, resolveAlias, resolvePath, resolveSilgiModule, resolveSilgiPath, serviceParseModule, toArray, tryResolveModule, useLogger, useRequest, useResponse, writeFile };
|
|
1
|
+
export { useLogger } from './logger.mjs';
|
|
2
|
+
export { hasError } from './errors.mjs';
|
|
3
|
+
export { isDirectory, writeFile } from './fs.mjs';
|
|
4
|
+
export { hash } from './hash.mjs';
|
|
5
|
+
export { defineSilgiModule } from './module.mjs';
|
|
6
|
+
export { parseServices, serviceParseModule } from './parseServices.mjs';
|
|
7
|
+
export { prettyPath, resolveSilgiPath } from './path.mjs';
|
|
8
|
+
export { defineSilgiPreset } from './preset.mjs';
|
|
9
|
+
export { createResolver, resolveAlias, resolvePath, resolveSilgiModule } from './resolve.mjs';
|
|
10
|
+
export { addTemplate, normalizeTemplate } from './template.mjs';
|
|
11
|
+
export { getIpAddress, ipAddress, useRequest, useResponse } from './useRequest.mjs';
|
|
12
|
+
export { directoryToURL, tryResolveModule } from './esm.mjs';
|
|
13
|
+
export { genEnsureSafeVar, getAllEntries } from './gen.mjs';
|
|
14
|
+
export { isH3, isNitro, isNuxt } from './isFramework.mjs';
|
|
15
|
+
export { MODE_RE, baseHeaderBannerComment, filterInPlace, hasInstalledModule, hasSilgiModule, processFilePath, relativeWithDot, toArray } from './utils.mjs';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useSilgi } from 'silgi';
|
|
2
|
+
|
|
3
|
+
function isNuxt() {
|
|
4
|
+
const silgi = useSilgi();
|
|
5
|
+
if (silgi.options.present === "nitro" || silgi.options.present === "h3" || silgi.options.present === "nuxt") {
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
function isNitro() {
|
|
11
|
+
const silgi = useSilgi();
|
|
12
|
+
if (silgi.options.present === "nitro" || silgi.options.present === "h3") {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
function isH3() {
|
|
18
|
+
const silgi = useSilgi();
|
|
19
|
+
if (silgi.options.present === "h3") {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { isH3, isNitro, isNuxt };
|