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,96 @@
|
|
|
1
|
+
import escapeRE from 'escape-string-regexp';
|
|
2
|
+
import { resolveModuleExportNames } from 'mlly';
|
|
3
|
+
import { join } from 'pathe';
|
|
4
|
+
|
|
5
|
+
async function resolveImportsOptions(options) {
|
|
6
|
+
if (options.imports === false) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
options.imports.presets ??= [];
|
|
10
|
+
options.imports.presets.push(...getSilgiImportsPreset());
|
|
11
|
+
if (options.preset === "h3") {
|
|
12
|
+
const h3Exports = await resolveModuleExportNames("h3", {
|
|
13
|
+
url: import.meta.url
|
|
14
|
+
});
|
|
15
|
+
options.imports.presets ??= [];
|
|
16
|
+
options.imports.presets.push({
|
|
17
|
+
from: "h3",
|
|
18
|
+
imports: h3Exports.filter((n) => !/^[A-Z]/.test(n) && n !== "use")
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
options.imports.dirs ??= [];
|
|
22
|
+
options.imports.dirs.push(
|
|
23
|
+
...options.scanDirs.map((dir) => join(dir, "utils/**/*"))
|
|
24
|
+
);
|
|
25
|
+
if (Array.isArray(options.imports.exclude) && options.imports.exclude.length === 0) {
|
|
26
|
+
options.imports.exclude.push(/[/\\]\.git[/\\]/);
|
|
27
|
+
options.imports.exclude.push(options.build.dir);
|
|
28
|
+
const scanDirsInNodeModules = options.scanDirs.map((dir) => dir.match(/(?<=\/)node_modules\/(.+)$/)?.[1]).filter(Boolean);
|
|
29
|
+
options.imports.exclude.push(
|
|
30
|
+
scanDirsInNodeModules.length > 0 ? new RegExp(
|
|
31
|
+
`node_modules\\/(?!${scanDirsInNodeModules.map((dir) => escapeRE(dir)).join("|")})`
|
|
32
|
+
) : /[/\\]node_modules[/\\]/
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function getSilgiImportsPreset() {
|
|
37
|
+
return [
|
|
38
|
+
// TODO: buraya bizim importlarimiz gelecek.
|
|
39
|
+
{
|
|
40
|
+
from: "silgi",
|
|
41
|
+
imports: [
|
|
42
|
+
"createShared",
|
|
43
|
+
"useSilgi",
|
|
44
|
+
"createService",
|
|
45
|
+
"createSchema"
|
|
46
|
+
]
|
|
47
|
+
},
|
|
48
|
+
// {
|
|
49
|
+
// from: 'nitropack/runtime',
|
|
50
|
+
// imports: ['useRuntimeConfig', 'useAppConfig'],
|
|
51
|
+
// },
|
|
52
|
+
// {
|
|
53
|
+
// from: 'nitropack/runtime',
|
|
54
|
+
// imports: ['defineNitroPlugin', 'nitroPlugin'],
|
|
55
|
+
// },
|
|
56
|
+
// {
|
|
57
|
+
// from: 'nitropack/runtime/internal/cache',
|
|
58
|
+
// imports: [
|
|
59
|
+
// 'defineCachedFunction',
|
|
60
|
+
// 'defineCachedEventHandler',
|
|
61
|
+
// 'cachedFunction',
|
|
62
|
+
// 'cachedEventHandler',
|
|
63
|
+
// ],
|
|
64
|
+
// },
|
|
65
|
+
{
|
|
66
|
+
from: "silgi",
|
|
67
|
+
imports: ["useSilgiStorage"]
|
|
68
|
+
}
|
|
69
|
+
// {
|
|
70
|
+
// from: 'nitropack/runtime/internal/renderer',
|
|
71
|
+
// imports: ['defineRenderHandler'],
|
|
72
|
+
// },
|
|
73
|
+
// {
|
|
74
|
+
// from: 'nitropack/runtime/internal/meta',
|
|
75
|
+
// imports: ['defineRouteMeta'],
|
|
76
|
+
// },
|
|
77
|
+
// {
|
|
78
|
+
// from: 'nitropack/runtime/internal/route-rules',
|
|
79
|
+
// imports: ['getRouteRules'],
|
|
80
|
+
// },
|
|
81
|
+
// {
|
|
82
|
+
// from: 'nitropack/runtime/internal/context',
|
|
83
|
+
// imports: ['useEvent'],
|
|
84
|
+
// },
|
|
85
|
+
// {
|
|
86
|
+
// from: 'nitropack/runtime/internal/task',
|
|
87
|
+
// imports: ['defineTask', 'runTask'],
|
|
88
|
+
// },
|
|
89
|
+
// {
|
|
90
|
+
// from: 'nitropack/runtime/internal/error/utils',
|
|
91
|
+
// imports: ['defineNitroErrorHandler'],
|
|
92
|
+
// },
|
|
93
|
+
];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export { resolveImportsOptions };
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { resolve, join } from 'pathe';
|
|
3
|
+
import { findWorkspaceDir, readPackageJSON } from 'pkg-types';
|
|
4
|
+
import { resolveSilgiPath } from 'silgi/kit';
|
|
5
|
+
import { runtimeDir, pkgDir } from 'silgi/runtime/meta';
|
|
6
|
+
import { SilgiCLIDefaults } from '../defaults.mjs';
|
|
7
|
+
|
|
8
|
+
async function resolvePathOptions(options) {
|
|
9
|
+
options.rootDir = resolve(options.rootDir || ".");
|
|
10
|
+
options.workspaceDir = await findWorkspaceDir(options.rootDir).catch(
|
|
11
|
+
() => options.rootDir
|
|
12
|
+
);
|
|
13
|
+
options.srcDir = resolve(options.srcDir || options.rootDir);
|
|
14
|
+
for (const key of ["srcDir"]) {
|
|
15
|
+
options[key] = resolve(options.rootDir, options[key]);
|
|
16
|
+
}
|
|
17
|
+
options.build.dir = resolve(options.rootDir, options.build.dir);
|
|
18
|
+
options.build.typesDir = resolveSilgiPath(
|
|
19
|
+
options.build.typesDir || SilgiCLIDefaults.build.typesDir,
|
|
20
|
+
options,
|
|
21
|
+
options.rootDir
|
|
22
|
+
);
|
|
23
|
+
if (options.preset === "npm-package") {
|
|
24
|
+
const packageJsonPath = resolve(options.rootDir, "package.json");
|
|
25
|
+
const packageJson = await readPackageJSON(packageJsonPath);
|
|
26
|
+
if (packageJson.name === void 0) {
|
|
27
|
+
throw new Error("Package name is undefined");
|
|
28
|
+
}
|
|
29
|
+
options.alias ||= {};
|
|
30
|
+
options.alias[packageJson.name] = join(options.rootDir, "src/module");
|
|
31
|
+
options.alias[`${packageJson.name}/runtime/`] = join(options.rootDir, "src/runtime");
|
|
32
|
+
options.alias[`${packageJson.name}/runtime/*`] = join(options.rootDir, "src/runtime/*");
|
|
33
|
+
options.alias[`${packageJson.name}/types`] = join(options.rootDir, "src/types");
|
|
34
|
+
}
|
|
35
|
+
if (options.stub) {
|
|
36
|
+
options.alias = {
|
|
37
|
+
...options.alias,
|
|
38
|
+
"silgi/runtime": join(runtimeDir),
|
|
39
|
+
"#internal/silgi": join(runtimeDir),
|
|
40
|
+
"silgi/runtime/*": join(runtimeDir, "*"),
|
|
41
|
+
"#internal/silgi/*": join(runtimeDir, "*")
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
options.alias = {
|
|
45
|
+
...options.alias,
|
|
46
|
+
"~/": join(options.srcDir, "/"),
|
|
47
|
+
"@/": join(options.srcDir, "/"),
|
|
48
|
+
"~~/": join(options.rootDir, "/"),
|
|
49
|
+
"@@/": join(options.rootDir, "/")
|
|
50
|
+
};
|
|
51
|
+
if (options.preset === "npm-package") {
|
|
52
|
+
options.alias = {
|
|
53
|
+
...options.alias
|
|
54
|
+
// '#silgi/app/': join(options.build.dir, '/'),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (options.alias && typeof options.alias === "object") {
|
|
58
|
+
((options.typescript.tsConfig ??= {}).compilerOptions ??= {}).paths ??= {};
|
|
59
|
+
const paths = options.typescript.tsConfig.compilerOptions.paths;
|
|
60
|
+
for (const [key, value] of Object.entries(options.alias)) {
|
|
61
|
+
if (typeof paths === "object") {
|
|
62
|
+
paths[key] = [value];
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (options.typescript.tsConfig.compilerOptions?.paths && typeof options.typescript.tsConfig.compilerOptions.paths === "object") {
|
|
67
|
+
((options.typescript.tsConfig ??= {}).compilerOptions ??= {}).paths ??= {};
|
|
68
|
+
const paths = options.typescript.tsConfig.compilerOptions.paths;
|
|
69
|
+
for (const [key, value] of Object.entries(options.alias)) {
|
|
70
|
+
if (typeof paths === "object") {
|
|
71
|
+
paths[key] = [value];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
options.modulesDir = [resolve(options.rootDir, "node_modules")];
|
|
76
|
+
options.output.dir = resolveSilgiPath(
|
|
77
|
+
options.output.dir || SilgiCLIDefaults.output.dir,
|
|
78
|
+
options,
|
|
79
|
+
options.rootDir
|
|
80
|
+
);
|
|
81
|
+
options.output.publicDir = resolveSilgiPath(
|
|
82
|
+
options.output.publicDir || SilgiCLIDefaults.output.publicDir,
|
|
83
|
+
options,
|
|
84
|
+
options.rootDir
|
|
85
|
+
);
|
|
86
|
+
options.output.serverDir = resolveSilgiPath(
|
|
87
|
+
options.output.serverDir || SilgiCLIDefaults.output.serverDir,
|
|
88
|
+
options,
|
|
89
|
+
options.rootDir
|
|
90
|
+
);
|
|
91
|
+
options.serverDir = resolveSilgiPath(
|
|
92
|
+
options.serverDir || SilgiCLIDefaults.serverDir,
|
|
93
|
+
options,
|
|
94
|
+
options.rootDir
|
|
95
|
+
);
|
|
96
|
+
options.clientDir = resolveSilgiPath(
|
|
97
|
+
options.clientDir || SilgiCLIDefaults.clientDir,
|
|
98
|
+
options,
|
|
99
|
+
options.rootDir
|
|
100
|
+
);
|
|
101
|
+
options.silgi.serverDir = resolveSilgiPath(
|
|
102
|
+
options.silgi.serverDir || SilgiCLIDefaults.silgi.serverDir,
|
|
103
|
+
options,
|
|
104
|
+
options.rootDir
|
|
105
|
+
);
|
|
106
|
+
options.silgi.clientDir = resolveSilgiPath(
|
|
107
|
+
options.silgi.clientDir || SilgiCLIDefaults.silgi.clientDir,
|
|
108
|
+
options,
|
|
109
|
+
options.rootDir
|
|
110
|
+
);
|
|
111
|
+
options.silgi.publicDir = resolveSilgiPath(
|
|
112
|
+
options.silgi.publicDir || SilgiCLIDefaults.silgi.publicDir,
|
|
113
|
+
options,
|
|
114
|
+
options.rootDir
|
|
115
|
+
);
|
|
116
|
+
options.silgi.utilsDir = resolveSilgiPath(
|
|
117
|
+
options.silgi.utilsDir || SilgiCLIDefaults.silgi.utilsDir,
|
|
118
|
+
options,
|
|
119
|
+
options.rootDir
|
|
120
|
+
);
|
|
121
|
+
options.silgi.vfsDir = resolveSilgiPath(
|
|
122
|
+
options.silgi.vfsDir || SilgiCLIDefaults.silgi.vfsDir,
|
|
123
|
+
options,
|
|
124
|
+
options.rootDir
|
|
125
|
+
);
|
|
126
|
+
options.silgi.typesDir = resolveSilgiPath(
|
|
127
|
+
options.silgi.typesDir || SilgiCLIDefaults.silgi.typesDir,
|
|
128
|
+
options,
|
|
129
|
+
options.rootDir
|
|
130
|
+
);
|
|
131
|
+
options.nodeModulesDirs.push(resolve(options.workspaceDir, "node_modules"));
|
|
132
|
+
options.nodeModulesDirs.push(resolve(options.rootDir, "node_modules"));
|
|
133
|
+
options.nodeModulesDirs.push(resolve(pkgDir, "node_modules"));
|
|
134
|
+
options.nodeModulesDirs.push(resolve(pkgDir, ".."));
|
|
135
|
+
options.nodeModulesDirs = [
|
|
136
|
+
...new Set(
|
|
137
|
+
options.nodeModulesDirs.map((dir) => resolve(options.rootDir, dir))
|
|
138
|
+
)
|
|
139
|
+
];
|
|
140
|
+
options.scanDirs.unshift(options.srcDir);
|
|
141
|
+
options.scanDirs = options.scanDirs.map(
|
|
142
|
+
(dir) => resolve(options.srcDir, dir)
|
|
143
|
+
);
|
|
144
|
+
options.scanDirs = [...new Set(options.scanDirs)];
|
|
145
|
+
options.appConfigFiles ??= [];
|
|
146
|
+
options.appConfigFiles = options.appConfigFiles.map((file) => _tryResolve(resolveSilgiPath(file, options))).filter(Boolean);
|
|
147
|
+
for (const dir of options.scanDirs) {
|
|
148
|
+
const configFile = _tryResolve("app.config", dir);
|
|
149
|
+
if (configFile && !options.appConfigFiles.includes(configFile)) {
|
|
150
|
+
options.appConfigFiles.push(configFile);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function _tryResolve(path, base = ".", extensions = ["", ".js", ".ts", ".mjs", ".cjs", ".json"]) {
|
|
155
|
+
path = resolve(base, path);
|
|
156
|
+
if (existsSync(path)) {
|
|
157
|
+
return path;
|
|
158
|
+
}
|
|
159
|
+
for (const ext of extensions) {
|
|
160
|
+
const p = path + ext;
|
|
161
|
+
if (existsSync(p)) {
|
|
162
|
+
return p;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export { resolvePathOptions };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { resolve } from 'pathe';
|
|
2
|
+
|
|
3
|
+
async function resolveStorageOptions(options) {
|
|
4
|
+
const fsMounts = {
|
|
5
|
+
root: resolve(options.rootDir),
|
|
6
|
+
src: resolve(options.srcDir),
|
|
7
|
+
build: resolve(options.build.dir),
|
|
8
|
+
cache: resolve(options.build.dir, "cache")
|
|
9
|
+
};
|
|
10
|
+
for (const p in fsMounts) {
|
|
11
|
+
options.devStorage[p] = options.devStorage[p] || {
|
|
12
|
+
driver: "fs",
|
|
13
|
+
readOnly: p === "root" || p === "src",
|
|
14
|
+
base: fsMounts[p]
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
if (options.dev && options.storage.data === void 0 && options.devStorage.data === void 0) {
|
|
18
|
+
options.devStorage.data = {
|
|
19
|
+
driver: "fs",
|
|
20
|
+
base: resolve(options.rootDir, ".data/kv")
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { resolveStorageOptions };
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { promises, lstatSync } from 'node:fs';
|
|
2
|
+
import { defu } from 'defu';
|
|
3
|
+
import { resolve, dirname, join, isAbsolute, relative } from 'pathe';
|
|
4
|
+
import { readPackageJSON } from 'pkg-types';
|
|
5
|
+
import { relativeWithDot, resolveSilgiModule } from 'silgi/kit';
|
|
6
|
+
import { withTrailingSlash } from 'ufo';
|
|
7
|
+
|
|
8
|
+
function getDirectory(p) {
|
|
9
|
+
try {
|
|
10
|
+
return isAbsolute(p) && lstatSync(p).isFile() ? dirname(p) : p;
|
|
11
|
+
} catch {
|
|
12
|
+
}
|
|
13
|
+
return p;
|
|
14
|
+
}
|
|
15
|
+
function renderAttrs(obj) {
|
|
16
|
+
const attrs = [];
|
|
17
|
+
for (const key in obj) {
|
|
18
|
+
attrs.push(renderAttr(key, obj[key]));
|
|
19
|
+
}
|
|
20
|
+
return attrs.join(" ");
|
|
21
|
+
}
|
|
22
|
+
function renderAttr(key, value) {
|
|
23
|
+
return value ? `${key}="${value}"` : "";
|
|
24
|
+
}
|
|
25
|
+
async function silgiGenerateType(silgi) {
|
|
26
|
+
const rootDirWithSlash = withTrailingSlash(silgi.options.rootDir);
|
|
27
|
+
const tsConfigPath = resolve(
|
|
28
|
+
silgi.options.rootDir,
|
|
29
|
+
silgi.options.typescript.tsconfigPath
|
|
30
|
+
);
|
|
31
|
+
const tsconfigDir = dirname(tsConfigPath);
|
|
32
|
+
const include = /* @__PURE__ */ new Set([
|
|
33
|
+
relativeWithDot(tsconfigDir, join(silgi.options.build.typesDir, "silgi.d.ts")).replace(
|
|
34
|
+
/^(?=[^.])/,
|
|
35
|
+
"./"
|
|
36
|
+
),
|
|
37
|
+
join(relativeWithDot(tsconfigDir, silgi.options.rootDir), "**/*"),
|
|
38
|
+
...silgi.options.srcDir === silgi.options.rootDir ? [] : [join(relativeWithDot(tsconfigDir, silgi.options.srcDir), "**/*")]
|
|
39
|
+
]);
|
|
40
|
+
const exclude = /* @__PURE__ */ new Set([
|
|
41
|
+
// nitro generate output: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/core/nitro.ts#L186
|
|
42
|
+
relativeWithDot(tsconfigDir, "node_modules"),
|
|
43
|
+
relativeWithDot(tsconfigDir, "dist")
|
|
44
|
+
]);
|
|
45
|
+
for (const dir of silgi.options.modulesDir) {
|
|
46
|
+
exclude.add(relativeWithDot(silgi.options.build.dir, dir));
|
|
47
|
+
}
|
|
48
|
+
const moduleEntryPaths = [];
|
|
49
|
+
for (const m of silgi.scanModules) {
|
|
50
|
+
if (m.entryPath) {
|
|
51
|
+
moduleEntryPaths.push(getDirectory(m.entryPath));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const modulePaths = await resolveSilgiModule(rootDirWithSlash, moduleEntryPaths);
|
|
55
|
+
for (const path of modulePaths) {
|
|
56
|
+
const relative2 = relativeWithDot(tsconfigDir, path);
|
|
57
|
+
include.add(join(relative2, "runtime"));
|
|
58
|
+
exclude.add(join(relative2, "runtime/server"));
|
|
59
|
+
include.add(join(relative2, "dist/runtime"));
|
|
60
|
+
exclude.add(join(relative2, "dist/runtime/server"));
|
|
61
|
+
}
|
|
62
|
+
const userTsConfig = {
|
|
63
|
+
compilerOptions: {
|
|
64
|
+
/* Base options: */
|
|
65
|
+
esModuleInterop: true,
|
|
66
|
+
allowSyntheticDefaultImports: true,
|
|
67
|
+
skipLibCheck: true,
|
|
68
|
+
allowImportingTsExtensions: true,
|
|
69
|
+
/* Target options: */
|
|
70
|
+
target: "ESNext",
|
|
71
|
+
allowJs: true,
|
|
72
|
+
resolveJsonModule: true,
|
|
73
|
+
moduleDetection: "force",
|
|
74
|
+
isolatedModules: true,
|
|
75
|
+
verbatimModuleSyntax: true,
|
|
76
|
+
/* Strictness */
|
|
77
|
+
strict: silgi.options.typescript.strict,
|
|
78
|
+
noUncheckedIndexedAccess: true,
|
|
79
|
+
noImplicitOverride: true,
|
|
80
|
+
forceConsistentCasingInFileNames: true,
|
|
81
|
+
/* If NOT transpiling with TypeScript: */
|
|
82
|
+
module: "Preserve",
|
|
83
|
+
customConditions: silgi.options.typescript.customConditions,
|
|
84
|
+
paths: {
|
|
85
|
+
// '#silgiImports': [
|
|
86
|
+
// relativeWithDot(tsconfigDir, join(silgi.options.build.typesDir, 'silgi-imports')),
|
|
87
|
+
// ],
|
|
88
|
+
...silgi.scanModules.reduce((acc, m) => {
|
|
89
|
+
if (m.entryPath) {
|
|
90
|
+
acc[m.meta.name] = [relativeWithDot(tsconfigDir, m.entryPath)];
|
|
91
|
+
}
|
|
92
|
+
return acc;
|
|
93
|
+
}, {}),
|
|
94
|
+
...silgi.scanModules.reduce((acc, m) => {
|
|
95
|
+
if (m.entryPath) {
|
|
96
|
+
const directory = getDirectory(m.entryPath);
|
|
97
|
+
acc[`${m.meta.name}/*`] = [`${relativeWithDot(tsconfigDir, directory)}/*`];
|
|
98
|
+
}
|
|
99
|
+
return acc;
|
|
100
|
+
}, {})
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
include: [...include],
|
|
104
|
+
exclude: [...exclude]
|
|
105
|
+
};
|
|
106
|
+
const npmPackageTsConfig = {
|
|
107
|
+
compilerOptions: {
|
|
108
|
+
target: "es2022",
|
|
109
|
+
lib: ["es2022", "webworker", "dom.iterable"],
|
|
110
|
+
moduleDetection: "force",
|
|
111
|
+
/* If NOT transpiling with TypeScript: */
|
|
112
|
+
module: "preserve",
|
|
113
|
+
paths: {},
|
|
114
|
+
resolveJsonModule: true,
|
|
115
|
+
allowJs: true,
|
|
116
|
+
/* Strictness */
|
|
117
|
+
strict: true,
|
|
118
|
+
noImplicitOverride: true,
|
|
119
|
+
noEmit: true,
|
|
120
|
+
allowSyntheticDefaultImports: true,
|
|
121
|
+
/* Base options: */
|
|
122
|
+
esModuleInterop: false,
|
|
123
|
+
forceConsistentCasingInFileNames: true,
|
|
124
|
+
isolatedModules: true,
|
|
125
|
+
verbatimModuleSyntax: true,
|
|
126
|
+
skipLibCheck: true
|
|
127
|
+
},
|
|
128
|
+
// include: ['src', 'test', './moduleTypes.d.ts'],
|
|
129
|
+
// exclude: ['dist', 'examples', 'playground', 'test/fixture'],
|
|
130
|
+
include: [...include],
|
|
131
|
+
exclude: [...exclude]
|
|
132
|
+
};
|
|
133
|
+
const tsConfig = defu(
|
|
134
|
+
silgi.options.typescript?.tsConfig,
|
|
135
|
+
silgi.options.preset === "npm-package" ? npmPackageTsConfig : userTsConfig
|
|
136
|
+
);
|
|
137
|
+
tsConfig.compilerOptions ||= {};
|
|
138
|
+
tsConfig.compilerOptions.paths ||= {};
|
|
139
|
+
tsConfig.include ||= [];
|
|
140
|
+
for (const alias in tsConfig.compilerOptions.paths) {
|
|
141
|
+
const paths = tsConfig.compilerOptions.paths[alias];
|
|
142
|
+
tsConfig.compilerOptions.paths[alias] = await Promise.all(
|
|
143
|
+
paths.map(async (path) => {
|
|
144
|
+
if (!isAbsolute(path)) {
|
|
145
|
+
return path;
|
|
146
|
+
}
|
|
147
|
+
const stats = await promises.stat(path).catch(
|
|
148
|
+
() => null
|
|
149
|
+
/* file does not exist */
|
|
150
|
+
);
|
|
151
|
+
return relativeWithDot(
|
|
152
|
+
tsconfigDir,
|
|
153
|
+
stats?.isFile() ? path.replace(/\b\.\w+$/g, "") : path
|
|
154
|
+
);
|
|
155
|
+
})
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
const references = [];
|
|
159
|
+
await Promise.all([...silgi.options.modules, ...silgi.options._modules].map(async (id) => {
|
|
160
|
+
if (typeof id !== "string") {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (id === "./src" && silgi.options.preset === "npm-package") {
|
|
164
|
+
id = resolve(silgi.options.rootDir);
|
|
165
|
+
}
|
|
166
|
+
const pkg = await readPackageJSON(id, { url: silgi.options.rootDir }).catch(() => null);
|
|
167
|
+
references.push({ types: pkg?.name || id });
|
|
168
|
+
}));
|
|
169
|
+
const declarations = [];
|
|
170
|
+
await silgi.callHook("prepare:types", { references, declarations, tsConfig });
|
|
171
|
+
tsConfig.include = [...new Set(tsConfig.include.map((p) => isAbsolute(p) ? relativeWithDot(tsconfigDir, p) : p))];
|
|
172
|
+
tsConfig.exclude = [...new Set(tsConfig.exclude.map((p) => isAbsolute(p) ? relativeWithDot(tsconfigDir, p) : p))];
|
|
173
|
+
const _declarations = [
|
|
174
|
+
...references.map((ref) => {
|
|
175
|
+
if ("path" in ref && isAbsolute(ref.path)) {
|
|
176
|
+
ref.path = relative(silgi.options.build.dir, ref.path);
|
|
177
|
+
}
|
|
178
|
+
return `/// <reference ${renderAttrs(ref)} />`;
|
|
179
|
+
}),
|
|
180
|
+
...declarations,
|
|
181
|
+
"",
|
|
182
|
+
"export {}",
|
|
183
|
+
""
|
|
184
|
+
];
|
|
185
|
+
return {
|
|
186
|
+
declarations: _declarations,
|
|
187
|
+
tsConfig
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export { getDirectory, silgiGenerateType };
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { mkdirSync, existsSync, writeFileSync, promises } from 'node:fs';
|
|
2
|
+
import { resolve, dirname } from 'pathe';
|
|
3
|
+
import { normalizeTemplate, useLogger } from 'silgi/kit';
|
|
4
|
+
import * as templates from './templates.mjs';
|
|
5
|
+
import { pluginsDeclaration } from './templates.mjs';
|
|
6
|
+
|
|
7
|
+
const postTemplates = [
|
|
8
|
+
pluginsDeclaration.filename
|
|
9
|
+
];
|
|
10
|
+
const logger = useLogger("silgi");
|
|
11
|
+
async function generateApp(app, options = {}) {
|
|
12
|
+
app.templates = Object.values(templates).concat(app.options.build.templates);
|
|
13
|
+
await app.callHook("app:templates", app);
|
|
14
|
+
app.templates = app.templates.map((tmpl) => {
|
|
15
|
+
const dir = tmpl.where === ".silgi" ? app.options.build.dir : tmpl.where === "server" ? app.options.silgi.serverDir : tmpl.where === "client" ? app.options.silgi.clientDir : app.options.silgi.vfsDir;
|
|
16
|
+
return normalizeTemplate(tmpl, dir);
|
|
17
|
+
});
|
|
18
|
+
const filteredTemplates = {
|
|
19
|
+
pre: [],
|
|
20
|
+
post: []
|
|
21
|
+
};
|
|
22
|
+
for (const template of app.templates) {
|
|
23
|
+
if (options.filter && !options.filter(template)) {
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
const key = template.filename && postTemplates.includes(template.filename) ? "post" : "pre";
|
|
27
|
+
filteredTemplates[key].push(template);
|
|
28
|
+
}
|
|
29
|
+
const templateContext = { app };
|
|
30
|
+
const writes = [];
|
|
31
|
+
const dirs = /* @__PURE__ */ new Set();
|
|
32
|
+
const changedTemplates = [];
|
|
33
|
+
async function processTemplate(template) {
|
|
34
|
+
const dir = template.where === ".silgi" ? app.options.build.dir : template.where === "server" ? app.options.silgi.serverDir : template.where === "client" ? app.options.silgi.clientDir : app.options.silgi.vfsDir;
|
|
35
|
+
const fullPath = template.dst || resolve(dir, template.filename);
|
|
36
|
+
const start = performance.now();
|
|
37
|
+
const contents = await compileTemplate(template, templateContext).catch((e) => {
|
|
38
|
+
logger.error(`Could not compile template \`${template.filename}\`.`);
|
|
39
|
+
logger.error(e);
|
|
40
|
+
throw e;
|
|
41
|
+
});
|
|
42
|
+
template.modified = true;
|
|
43
|
+
if (template.modified) {
|
|
44
|
+
changedTemplates.push(template);
|
|
45
|
+
}
|
|
46
|
+
const perf = performance.now() - start;
|
|
47
|
+
const setupTime = Math.round(perf * 100) / 100;
|
|
48
|
+
if (app.options.debug || setupTime > 500) {
|
|
49
|
+
logger.info(`Compiled \`${template.filename}\` in ${setupTime}ms`);
|
|
50
|
+
}
|
|
51
|
+
if (template.modified && template.write) {
|
|
52
|
+
dirs.add(dirname(fullPath));
|
|
53
|
+
if (template.skipIfExists && existsSync(fullPath)) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
writes.push(() => writeFileSync(fullPath, contents, "utf8"));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
await Promise.allSettled(filteredTemplates.pre.map(processTemplate));
|
|
60
|
+
await Promise.allSettled(filteredTemplates.post.map(processTemplate));
|
|
61
|
+
for (const dir of dirs) {
|
|
62
|
+
mkdirSync(dir, { recursive: true });
|
|
63
|
+
}
|
|
64
|
+
for (const write of writes) {
|
|
65
|
+
if (!app.errors.length) {
|
|
66
|
+
write();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (changedTemplates.length) {
|
|
70
|
+
await app.callHook("app:templatesGenerated", app, changedTemplates, options);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async function compileTemplate(template, ctx) {
|
|
74
|
+
delete ctx.utils;
|
|
75
|
+
if (template.src) {
|
|
76
|
+
try {
|
|
77
|
+
return await promises.readFile(template.src, "utf-8");
|
|
78
|
+
} catch (err) {
|
|
79
|
+
logger.error(`[nuxt] Error reading template from \`${template.src}\``);
|
|
80
|
+
throw err;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (template.getContents) {
|
|
84
|
+
return template.getContents({
|
|
85
|
+
...ctx,
|
|
86
|
+
options: template.options
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
throw new Error(`[nuxt] Invalid template. Templates must have either \`src\` or \`getContents\`: ${JSON.stringify(template)}`);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export { generateApp, logger };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { existsSync, promises } from 'node:fs';
|
|
2
|
+
import * as dotenv from 'dotenv';
|
|
3
|
+
import { resolve } from 'pathe';
|
|
4
|
+
|
|
5
|
+
async function setupDotenv(options) {
|
|
6
|
+
const targetEnvironment = options.env ?? process.env;
|
|
7
|
+
const environment = await loadDotenv({
|
|
8
|
+
cwd: options.cwd,
|
|
9
|
+
fileName: options.fileName ?? ".env",
|
|
10
|
+
env: targetEnvironment,
|
|
11
|
+
interpolate: options.interpolate ?? true
|
|
12
|
+
});
|
|
13
|
+
for (const key in environment) {
|
|
14
|
+
if (!key.startsWith("_") && targetEnvironment[key] === void 0) {
|
|
15
|
+
targetEnvironment[key] = environment[key];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return environment;
|
|
19
|
+
}
|
|
20
|
+
async function loadDotenv(options) {
|
|
21
|
+
const environment = /* @__PURE__ */ Object.create(null);
|
|
22
|
+
const dotenvFile = resolve(options.cwd, options.fileName);
|
|
23
|
+
if (existsSync(dotenvFile)) {
|
|
24
|
+
const parsed = dotenv.parse(await promises.readFile(dotenvFile, "utf8"));
|
|
25
|
+
Object.assign(environment, parsed);
|
|
26
|
+
}
|
|
27
|
+
if (!options.env?._applied) {
|
|
28
|
+
Object.assign(environment, options.env);
|
|
29
|
+
environment._applied = true;
|
|
30
|
+
}
|
|
31
|
+
if (options.interpolate) {
|
|
32
|
+
interpolate(environment);
|
|
33
|
+
}
|
|
34
|
+
return environment;
|
|
35
|
+
}
|
|
36
|
+
function interpolate(target, source = {}, parse = (v) => v) {
|
|
37
|
+
function getValue(key) {
|
|
38
|
+
return source[key] === void 0 ? target[key] : source[key];
|
|
39
|
+
}
|
|
40
|
+
function interpolate2(value, parents = []) {
|
|
41
|
+
if (typeof value !== "string") {
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
const matches = value.match(/(.?\$\{?[\w:]*\}?)/g) || [];
|
|
45
|
+
return parse(
|
|
46
|
+
matches.reduce((newValue, match) => {
|
|
47
|
+
const parts = /(.?)\$\{?([\w:]+)?\}?/.exec(match) || [];
|
|
48
|
+
const prefix = parts[1];
|
|
49
|
+
let value2, replacePart;
|
|
50
|
+
if (prefix === "\\") {
|
|
51
|
+
replacePart = parts[0] || "";
|
|
52
|
+
value2 = replacePart.replace(String.raw`\$`, "$");
|
|
53
|
+
} else {
|
|
54
|
+
const key = parts[2];
|
|
55
|
+
replacePart = (parts[0] || "").slice(prefix.length);
|
|
56
|
+
if (parents.includes(key)) {
|
|
57
|
+
console.warn(
|
|
58
|
+
`Please avoid recursive environment variables ( loop: ${parents.join(
|
|
59
|
+
" > "
|
|
60
|
+
)} > ${key} )`
|
|
61
|
+
);
|
|
62
|
+
return "";
|
|
63
|
+
}
|
|
64
|
+
value2 = getValue(key);
|
|
65
|
+
value2 = interpolate2(value2, [...parents, key]);
|
|
66
|
+
}
|
|
67
|
+
return value2 === void 0 ? newValue : newValue.replace(replacePart, value2);
|
|
68
|
+
}, value)
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
for (const key in target) {
|
|
72
|
+
target[key] = interpolate2(getValue(key));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export { loadDotenv, setupDotenv };
|