@saykit/config 0.4.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/dist/commands/index.cjs +6 -5
  2. package/dist/commands/index.d.cts +1 -1
  3. package/dist/commands/index.d.mts +1 -1
  4. package/dist/commands/index.mjs +6 -4
  5. package/dist/features/catalogue/index.cjs +3 -4
  6. package/dist/features/catalogue/index.d.cts +1 -2
  7. package/dist/features/catalogue/index.d.mts +1 -2
  8. package/dist/features/catalogue/index.mjs +3 -3
  9. package/dist/features/loader/index.cjs +1 -1
  10. package/dist/features/loader/index.d.cts +1 -2
  11. package/dist/features/loader/index.d.mts +1 -2
  12. package/dist/features/loader/index.mjs +1 -1
  13. package/dist/features/messages/index.cjs +61 -8
  14. package/dist/features/messages/index.d.cts +8 -2
  15. package/dist/features/messages/index.d.mts +8 -2
  16. package/dist/features/messages/index.mjs +61 -8
  17. package/dist/{hash-CxBlj5Dz.cjs → hash-51ce8YiG.cjs} +0 -1
  18. package/dist/index.cjs +53 -3
  19. package/dist/index.d.cts +11 -5
  20. package/dist/index.d.mts +11 -5
  21. package/dist/index.mjs +29 -0
  22. package/dist/loader-A1uS7qkK.cjs +92 -0
  23. package/dist/loader-DLyuZzF2.mjs +87 -0
  24. package/dist/{shapes-Ba3xS10n.d.cts → shapes-CRlXtss0.d.cts} +48 -2
  25. package/dist/{shapes-NPfwlInG.d.mts → shapes-CRlXtss0.d.mts} +48 -2
  26. package/dist/{storage-CqKPYfCR.mjs → storage-B6mn0s3V.mjs} +1 -1
  27. package/dist/{storage-wS6e6qWM.cjs → storage-DqhzA5H8.cjs} +1 -2
  28. package/package.json +6 -11
  29. package/dist/chunk-CKQMccvm.cjs +0 -28
  30. package/dist/loader-BuAqYtfl.mjs +0 -191
  31. package/dist/loader-DATEQsXj.cjs +0 -198
  32. /package/dist/{hash-DyC8GzMa.mjs → hash-DvzpieJD.mjs} +0 -0
@@ -1,198 +0,0 @@
1
- const require_chunk = require("./chunk-CKQMccvm.cjs");
2
- let node_path = require("node:path");
3
- let node_fs = require("node:fs");
4
- let node_crypto = require("node:crypto");
5
- let node_module = require("node:module");
6
- node_module = require_chunk.__toESM(node_module, 1);
7
- let node_os = require("node:os");
8
- //#region src/features/loader/files.ts
9
- const SUPPORTED_CONFIG_FILES = [
10
- "saykit.config.js",
11
- "saykit.config.cjs",
12
- "saykit.config.mjs",
13
- "saykit.config.ts",
14
- "saykit.config.mts",
15
- "saykit.config.cts"
16
- ];
17
- function getConfigFileCandidates(name) {
18
- return SUPPORTED_CONFIG_FILES.map((file) => file.replace("saykit", name));
19
- }
20
- function findConfigFile(moduleName, projectDir) {
21
- for (const fileName of getConfigFileCandidates(moduleName)) {
22
- const id = (0, node_path.join)(projectDir, fileName);
23
- if ((0, node_fs.existsSync)(id)) return { id };
24
- }
25
- return null;
26
- }
27
- //#endregion
28
- //#region src/features/loader/module.ts
29
- function findUpwards(fromPath, visit) {
30
- let dir = (0, node_path.dirname)(fromPath);
31
- const { root } = (0, node_path.parse)(dir);
32
- while (true) {
33
- const found = visit(dir);
34
- if (found !== null) return found;
35
- if (dir === root) return null;
36
- dir = (0, node_path.dirname)(dir);
37
- }
38
- }
39
- function findNearestTsConfig(fromPath) {
40
- return findUpwards(fromPath, (dir) => {
41
- const candidate = (0, node_path.join)(dir, "tsconfig.json");
42
- return (0, node_fs.existsSync)(candidate) ? candidate : null;
43
- });
44
- }
45
- function digest(value) {
46
- return (0, node_crypto.createHash)("sha1").update(value).digest("hex").slice(0, 16);
47
- }
48
- /**
49
- * Builds are executable, so they belong beside the project's dependencies. The
50
- * fallback lands in a shared temp directory instead, where a fixed path would
51
- * let any other user on the machine plant code we later require — hence a
52
- * per-user directory, created private in `compileToCache`.
53
- */
54
- function findCacheDir(fromPath) {
55
- const nodeModules = findUpwards(fromPath, (dir) => {
56
- const candidate = (0, node_path.join)(dir, "node_modules");
57
- return (0, node_fs.existsSync)(candidate) ? candidate : null;
58
- });
59
- if (nodeModules) return (0, node_path.join)(nodeModules, ".cache", "saykit", "config");
60
- const { uid, username, homedir } = (0, node_os.userInfo)();
61
- return (0, node_path.join)((0, node_os.tmpdir)(), `saykit-config-${digest(`${uid}\0${username}\0${homedir}`)}`);
62
- }
63
- function mtimeOf(path) {
64
- return path ? (0, node_fs.statSync)(path).mtimeMs : 0;
65
- }
66
- /** Removes every build of `file` in `dir` except `keep`. */
67
- function pruneCache(dir, file, keep) {
68
- for (const entry of (0, node_fs.readdirSync)(dir)) {
69
- if (!entry.startsWith(`${file}.`) || (0, node_path.join)(dir, entry) === keep) continue;
70
- try {
71
- (0, node_fs.unlinkSync)((0, node_path.join)(dir, entry));
72
- } catch {}
73
- }
74
- }
75
- /**
76
- * Preferred: the classic TypeScript compiler API, which honours the project's
77
- * tsconfig and emits CommonJS. TypeScript 7's root export no longer ships it,
78
- * and the dependency is optional, so this gives up when it isn't there.
79
- */
80
- function transpileWithCompilerApi(source, tsConfigPath, require) {
81
- let ts;
82
- try {
83
- ts = require("typescript");
84
- } catch {
85
- return null;
86
- }
87
- if (typeof ts?.transpileModule !== "function" || typeof ts.sys?.readFile !== "function") return null;
88
- const { config, error } = tsConfigPath ? ts.readConfigFile(tsConfigPath, ts.sys.readFile) : {
89
- config: {},
90
- error: null
91
- };
92
- if (error) throw error;
93
- config.compilerOptions = {
94
- ...config.compilerOptions,
95
- allowJs: true,
96
- esModuleInterop: true,
97
- noEmit: false,
98
- module: ts.ModuleKind.CommonJS,
99
- moduleResolution: ts.ModuleResolutionKind.NodeJs,
100
- target: ts.ScriptTarget.ES2022
101
- };
102
- return {
103
- code: ts.transpileModule(source, config).outputText,
104
- extension: "cjs"
105
- };
106
- }
107
- /**
108
- * Fallback: Node erases the types itself. It ignores tsconfig and leaves the
109
- * module syntax alone, so the extension has to follow the source.
110
- */
111
- function transpileWithNode(source) {
112
- if (typeof node_module.default.stripTypeScriptTypes !== "function") throw new Error("Loading TypeScript config files requires the TypeScript compiler API (typescript <= 6), Node 22.13+, or a runtime that loads TypeScript itself (Bun, Deno, tsx)");
113
- const code = node_module.default.stripTypeScriptTypes(source, { mode: "transform" });
114
- return {
115
- code,
116
- extension: /^\s*(?:import|export)[\s({]/m.test(code) ? "mjs" : "cjs"
117
- };
118
- }
119
- /**
120
- * Writes a plain JavaScript copy of `path` next to the project's dependencies
121
- * and returns it. Copies are named `<file>.<inputs>.<extension>`, so a build
122
- * can be reused until its inputs change, and earlier builds of the same file
123
- * can be pruned once it does.
124
- */
125
- function compileToCache(path, require) {
126
- const tsConfigPath = findNearestTsConfig(path);
127
- const dir = findCacheDir(path);
128
- const file = digest(path);
129
- const inputs = digest(`${mtimeOf(path)}\0${tsConfigPath}\0${mtimeOf(tsConfigPath)}`);
130
- const cached = ["cjs", "mjs"].map((ext) => (0, node_path.join)(dir, `${file}.${inputs}.${ext}`)).find(node_fs.existsSync);
131
- if (cached) return cached;
132
- const source = (0, node_fs.readFileSync)(path, "utf8");
133
- const { code, extension } = transpileWithCompilerApi(source, tsConfigPath, require) ?? transpileWithNode(source);
134
- const target = (0, node_path.join)(dir, `${file}.${inputs}.${extension}`);
135
- (0, node_fs.mkdirSync)(dir, {
136
- recursive: true,
137
- mode: 448
138
- });
139
- (0, node_fs.writeFileSync)(target, code, { mode: 384 });
140
- pruneCache(dir, file, target);
141
- return target;
142
- }
143
- /**
144
- * Bun, Deno and hooks like tsx or ts-node load TypeScript better than we can,
145
- * resolving tsconfig `paths` and the project's own module resolution with it.
146
- */
147
- function runtimeLoadsTypeScript(require) {
148
- return Boolean(process.versions.bun || globalThis.Deno || require.extensions?.[".ts"] || Reflect.get(process, Symbol.for("ts-node.register.instance")));
149
- }
150
- /** Requires `path` without leaving it in (or reading it from) the require cache. */
151
- function requireFresh(require, path) {
152
- const resolved = require.resolve(path);
153
- delete require.cache[resolved];
154
- const module = require(path);
155
- delete require.cache[resolved];
156
- return module?.default ?? module;
157
- }
158
- /**
159
- * Loads a config file, compiling it first unless the runtime reads TypeScript
160
- * on its own.
161
- */
162
- function loadModule(path) {
163
- const require = (0, node_module.createRequire)(path);
164
- try {
165
- return requireFresh(require, runtimeLoadsTypeScript(require) ? path : compileToCache(path, require));
166
- } catch (error) {
167
- throw new Error("Failed to import module", { cause: error });
168
- }
169
- }
170
- const js = loadModule;
171
- const ts = loadModule;
172
- const configLoaders = Object.freeze({
173
- ".js": js,
174
- ".mjs": js,
175
- ".cjs": js,
176
- ".ts": ts,
177
- ".mts": ts,
178
- ".cts": ts
179
- });
180
- //#endregion
181
- //#region src/features/loader/resolve.ts
182
- function resolveConfig(name = "saykit") {
183
- const file = findConfigFile(name, process.cwd());
184
- if (!file) throw new Error(`Could not find config file for "${name}"`);
185
- const ext = (0, node_path.extname)(file.id).toLowerCase();
186
- const load = ext in configLoaders ? configLoaders[ext] : null;
187
- if (!load) throw new Error(`Unsupported config file type "${ext}" for "${name}"`);
188
- const config = load(file.id);
189
- if (!config || typeof config !== "object") throw new Error(`Invalid config file for "${name}"`);
190
- return config;
191
- }
192
- //#endregion
193
- Object.defineProperty(exports, "resolveConfig", {
194
- enumerable: true,
195
- get: function() {
196
- return resolveConfig;
197
- }
198
- });
File without changes