@saykit/config 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,28 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+ //#endregion
23
+ Object.defineProperty(exports, "__toESM", {
24
+ enumerable: true,
25
+ get: function() {
26
+ return __toESM;
27
+ }
28
+ });
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
- require("../index.cjs");
3
- const require_storage = require("../storage-C4rOvLGi.cjs");
4
- const require_loader = require("../loader-B25O-vF6.cjs");
2
+ require("../chunk-CKQMccvm.cjs");
3
+ const require_storage = require("../storage-wS6e6qWM.cjs");
4
+ const require_loader = require("../loader-DATEQsXj.cjs");
5
5
  let _commander_js_extra_typings = require("@commander-js/extra-typings");
6
6
  let node_fs_promises = require("node:fs/promises");
7
7
  let node_path = require("node:path");
@@ -79,13 +79,17 @@ async function extractMessagesFromFile(path, bucket) {
79
79
  //#region src/features/watch.ts
80
80
  /**
81
81
  * Expand a buckets include and exclude patterns into a flat list of file paths.
82
+ *
83
+ * We stat each match instead of using `glob`'s `withFileTypes` option, which
84
+ * Bun's `node:fs/promises` compatibility layer does not yet support.
82
85
  */
83
86
  async function globBucket(bucket) {
84
87
  const paths = [];
85
- for await (const file of (0, node_fs_promises.glob)(bucket.include, {
86
- exclude: bucket.exclude,
87
- withFileTypes: true
88
- })) if (file.isFile()) paths.push((0, node_path.join)(file.parentPath, file.name));
88
+ for await (const path of (0, node_fs_promises.glob)(bucket.include, { exclude: bucket.exclude })) try {
89
+ if ((await (0, node_fs_promises.stat)(path)).isFile()) paths.push(path);
90
+ } catch (error) {
91
+ if (error.code !== "ENOENT") throw error;
92
+ }
89
93
  return paths;
90
94
  }
91
95
  /**
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
  import { a as mergeExtractedMessages, i as expandBucketOutputPath, n as writeCatalogueMessages, o as pruneLocaleMessages, t as readCatalogueMessages } from "../storage-CqKPYfCR.mjs";
3
- import { t as resolveConfig } from "../loader-C2eolE_1.mjs";
3
+ import { t as resolveConfig } from "../loader-BuAqYtfl.mjs";
4
4
  import { Command, program } from "@commander-js/extra-typings";
5
- import { access, glob, readFile, watch } from "node:fs/promises";
5
+ import { access, glob, readFile, stat, watch } from "node:fs/promises";
6
6
  import { join, relative } from "node:path";
7
7
  import { AsyncLocalStorage } from "node:async_hooks";
8
8
  //#region src/features/logger.ts
@@ -78,13 +78,17 @@ async function extractMessagesFromFile(path, bucket) {
78
78
  //#region src/features/watch.ts
79
79
  /**
80
80
  * Expand a buckets include and exclude patterns into a flat list of file paths.
81
+ *
82
+ * We stat each match instead of using `glob`'s `withFileTypes` option, which
83
+ * Bun's `node:fs/promises` compatibility layer does not yet support.
81
84
  */
82
85
  async function globBucket(bucket) {
83
86
  const paths = [];
84
- for await (const file of glob(bucket.include, {
85
- exclude: bucket.exclude,
86
- withFileTypes: true
87
- })) if (file.isFile()) paths.push(join(file.parentPath, file.name));
87
+ for await (const path of glob(bucket.include, { exclude: bucket.exclude })) try {
88
+ if ((await stat(path)).isFile()) paths.push(path);
89
+ } catch (error) {
90
+ if (error.code !== "ENOENT") throw error;
91
+ }
88
92
  return paths;
89
93
  }
90
94
  /**
@@ -1,7 +1,7 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- require("../../index.cjs");
3
- const require_hash = require("../../hash-8It7TJpB.cjs");
4
- const require_storage = require("../../storage-C4rOvLGi.cjs");
2
+ require("../../chunk-CKQMccvm.cjs");
3
+ const require_hash = require("../../hash-CxBlj5Dz.cjs");
4
+ const require_storage = require("../../storage-wS6e6qWM.cjs");
5
5
  let node_path = require("node:path");
6
6
  //#region src/features/catalogue/record.ts
7
7
  /**
@@ -1,3 +1,3 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_loader = require("../../loader-B25O-vF6.cjs");
2
+ const require_loader = require("../../loader-DATEQsXj.cjs");
3
3
  exports.resolveConfig = require_loader.resolveConfig;
@@ -1,2 +1,2 @@
1
- import { t as resolveConfig } from "../../loader-C2eolE_1.mjs";
1
+ import { t as resolveConfig } from "../../loader-BuAqYtfl.mjs";
2
2
  export { resolveConfig };
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_hash = require("../../hash-8It7TJpB.cjs");
2
+ const require_hash = require("../../hash-CxBlj5Dz.cjs");
3
3
  //#region src/features/messages/identifier.ts
4
4
  const AUTO_INCREMENT_IDENTIFIER = Symbol("auto-increment");
5
5
  function assignSequenceIdentifiers(message, sequence = { current: 0 }) {
@@ -1,4 +1,4 @@
1
- require("./index.cjs");
1
+ require("./chunk-CKQMccvm.cjs");
2
2
  let js_sha256 = require("js-sha256");
3
3
  //#region src/features/messages/hash.ts
4
4
  function generateHash(input, context) {
package/dist/index.cjs CHANGED
@@ -1,30 +1,9 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- //#region \0rolldown/runtime.js
3
- var __create = Object.create;
4
- var __defProp = Object.defineProperty;
5
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
- var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __getProtoOf = Object.getPrototypeOf;
8
- var __hasOwnProp = Object.prototype.hasOwnProperty;
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
- key = keys[i];
12
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
13
- get: ((k) => from[k]).bind(null, key),
14
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
- });
16
- }
17
- return to;
18
- };
19
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
20
- value: mod,
21
- enumerable: true
22
- }) : target, mod));
23
- //#endregion
2
+ const require_chunk = require("./chunk-CKQMccvm.cjs");
24
3
  let picomatch = require("picomatch");
25
- picomatch = __toESM(picomatch, 1);
4
+ picomatch = require_chunk.__toESM(picomatch, 1);
26
5
  let zod = require("zod");
27
- zod = __toESM(zod, 1);
6
+ zod = require_chunk.__toESM(zod, 1);
28
7
  zod.object({
29
8
  message: zod.string(),
30
9
  translation: zod.string().optional(),
@@ -80,5 +59,4 @@ function defineConfig(config) {
80
59
  return Config.parse(config);
81
60
  }
82
61
  //#endregion
83
- exports.__toESM = __toESM;
84
62
  exports.defineConfig = defineConfig;
@@ -0,0 +1,191 @@
1
+ import nodeModule, { createRequire } from "node:module";
2
+ import { dirname, extname, join, parse } from "node:path";
3
+ import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, unlinkSync, writeFileSync } from "node:fs";
4
+ import { createHash } from "node:crypto";
5
+ import { tmpdir, userInfo } from "node:os";
6
+ //#region src/features/loader/files.ts
7
+ const SUPPORTED_CONFIG_FILES = [
8
+ "saykit.config.js",
9
+ "saykit.config.cjs",
10
+ "saykit.config.mjs",
11
+ "saykit.config.ts",
12
+ "saykit.config.mts",
13
+ "saykit.config.cts"
14
+ ];
15
+ function getConfigFileCandidates(name) {
16
+ return SUPPORTED_CONFIG_FILES.map((file) => file.replace("saykit", name));
17
+ }
18
+ function findConfigFile(moduleName, projectDir) {
19
+ for (const fileName of getConfigFileCandidates(moduleName)) {
20
+ const id = join(projectDir, fileName);
21
+ if (existsSync(id)) return { id };
22
+ }
23
+ return null;
24
+ }
25
+ //#endregion
26
+ //#region src/features/loader/module.ts
27
+ function findUpwards(fromPath, visit) {
28
+ let dir = dirname(fromPath);
29
+ const { root } = parse(dir);
30
+ while (true) {
31
+ const found = visit(dir);
32
+ if (found !== null) return found;
33
+ if (dir === root) return null;
34
+ dir = dirname(dir);
35
+ }
36
+ }
37
+ function findNearestTsConfig(fromPath) {
38
+ return findUpwards(fromPath, (dir) => {
39
+ const candidate = join(dir, "tsconfig.json");
40
+ return existsSync(candidate) ? candidate : null;
41
+ });
42
+ }
43
+ function digest(value) {
44
+ return createHash("sha1").update(value).digest("hex").slice(0, 16);
45
+ }
46
+ /**
47
+ * Builds are executable, so they belong beside the project's dependencies. The
48
+ * fallback lands in a shared temp directory instead, where a fixed path would
49
+ * let any other user on the machine plant code we later require — hence a
50
+ * per-user directory, created private in `compileToCache`.
51
+ */
52
+ function findCacheDir(fromPath) {
53
+ const nodeModules = findUpwards(fromPath, (dir) => {
54
+ const candidate = join(dir, "node_modules");
55
+ return existsSync(candidate) ? candidate : null;
56
+ });
57
+ if (nodeModules) return join(nodeModules, ".cache", "saykit", "config");
58
+ const { uid, username, homedir } = userInfo();
59
+ return join(tmpdir(), `saykit-config-${digest(`${uid}\0${username}\0${homedir}`)}`);
60
+ }
61
+ function mtimeOf(path) {
62
+ return path ? statSync(path).mtimeMs : 0;
63
+ }
64
+ /** Removes every build of `file` in `dir` except `keep`. */
65
+ function pruneCache(dir, file, keep) {
66
+ for (const entry of readdirSync(dir)) {
67
+ if (!entry.startsWith(`${file}.`) || join(dir, entry) === keep) continue;
68
+ try {
69
+ unlinkSync(join(dir, entry));
70
+ } catch {}
71
+ }
72
+ }
73
+ /**
74
+ * Preferred: the classic TypeScript compiler API, which honours the project's
75
+ * tsconfig and emits CommonJS. TypeScript 7's root export no longer ships it,
76
+ * and the dependency is optional, so this gives up when it isn't there.
77
+ */
78
+ function transpileWithCompilerApi(source, tsConfigPath, require) {
79
+ let ts;
80
+ try {
81
+ ts = require("typescript");
82
+ } catch {
83
+ return null;
84
+ }
85
+ if (typeof ts?.transpileModule !== "function" || typeof ts.sys?.readFile !== "function") return null;
86
+ const { config, error } = tsConfigPath ? ts.readConfigFile(tsConfigPath, ts.sys.readFile) : {
87
+ config: {},
88
+ error: null
89
+ };
90
+ if (error) throw error;
91
+ config.compilerOptions = {
92
+ ...config.compilerOptions,
93
+ allowJs: true,
94
+ esModuleInterop: true,
95
+ noEmit: false,
96
+ module: ts.ModuleKind.CommonJS,
97
+ moduleResolution: ts.ModuleResolutionKind.NodeJs,
98
+ target: ts.ScriptTarget.ES2022
99
+ };
100
+ return {
101
+ code: ts.transpileModule(source, config).outputText,
102
+ extension: "cjs"
103
+ };
104
+ }
105
+ /**
106
+ * Fallback: Node erases the types itself. It ignores tsconfig and leaves the
107
+ * module syntax alone, so the extension has to follow the source.
108
+ */
109
+ function transpileWithNode(source) {
110
+ if (typeof nodeModule.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)");
111
+ const code = nodeModule.stripTypeScriptTypes(source, { mode: "transform" });
112
+ return {
113
+ code,
114
+ extension: /^\s*(?:import|export)[\s({]/m.test(code) ? "mjs" : "cjs"
115
+ };
116
+ }
117
+ /**
118
+ * Writes a plain JavaScript copy of `path` next to the project's dependencies
119
+ * and returns it. Copies are named `<file>.<inputs>.<extension>`, so a build
120
+ * can be reused until its inputs change, and earlier builds of the same file
121
+ * can be pruned once it does.
122
+ */
123
+ function compileToCache(path, require) {
124
+ const tsConfigPath = findNearestTsConfig(path);
125
+ const dir = findCacheDir(path);
126
+ const file = digest(path);
127
+ const inputs = digest(`${mtimeOf(path)}\0${tsConfigPath}\0${mtimeOf(tsConfigPath)}`);
128
+ const cached = ["cjs", "mjs"].map((ext) => join(dir, `${file}.${inputs}.${ext}`)).find(existsSync);
129
+ if (cached) return cached;
130
+ const source = readFileSync(path, "utf8");
131
+ const { code, extension } = transpileWithCompilerApi(source, tsConfigPath, require) ?? transpileWithNode(source);
132
+ const target = join(dir, `${file}.${inputs}.${extension}`);
133
+ mkdirSync(dir, {
134
+ recursive: true,
135
+ mode: 448
136
+ });
137
+ writeFileSync(target, code, { mode: 384 });
138
+ pruneCache(dir, file, target);
139
+ return target;
140
+ }
141
+ /**
142
+ * Bun, Deno and hooks like tsx or ts-node load TypeScript better than we can,
143
+ * resolving tsconfig `paths` and the project's own module resolution with it.
144
+ */
145
+ function runtimeLoadsTypeScript(require) {
146
+ return Boolean(process.versions.bun || globalThis.Deno || require.extensions?.[".ts"] || Reflect.get(process, Symbol.for("ts-node.register.instance")));
147
+ }
148
+ /** Requires `path` without leaving it in (or reading it from) the require cache. */
149
+ function requireFresh(require, path) {
150
+ const resolved = require.resolve(path);
151
+ delete require.cache[resolved];
152
+ const module = require(path);
153
+ delete require.cache[resolved];
154
+ return module?.default ?? module;
155
+ }
156
+ /**
157
+ * Loads a config file, compiling it first unless the runtime reads TypeScript
158
+ * on its own.
159
+ */
160
+ function loadModule(path) {
161
+ const require = createRequire(path);
162
+ try {
163
+ return requireFresh(require, runtimeLoadsTypeScript(require) ? path : compileToCache(path, require));
164
+ } catch (error) {
165
+ throw new Error("Failed to import module", { cause: error });
166
+ }
167
+ }
168
+ const js = loadModule;
169
+ const ts = loadModule;
170
+ const configLoaders = Object.freeze({
171
+ ".js": js,
172
+ ".mjs": js,
173
+ ".cjs": js,
174
+ ".ts": ts,
175
+ ".mts": ts,
176
+ ".cts": ts
177
+ });
178
+ //#endregion
179
+ //#region src/features/loader/resolve.ts
180
+ function resolveConfig(name = "saykit") {
181
+ const file = findConfigFile(name, process.cwd());
182
+ if (!file) throw new Error(`Could not find config file for "${name}"`);
183
+ const ext = extname(file.id).toLowerCase();
184
+ const load = ext in configLoaders ? configLoaders[ext] : null;
185
+ if (!load) throw new Error(`Unsupported config file type "${ext}" for "${name}"`);
186
+ const config = load(file.id);
187
+ if (!config || typeof config !== "object") throw new Error(`Invalid config file for "${name}"`);
188
+ return config;
189
+ }
190
+ //#endregion
191
+ export { resolveConfig as t };
@@ -0,0 +1,198 @@
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
+ });
@@ -1,5 +1,5 @@
1
- require("./index.cjs");
2
- const require_hash = require("./hash-8It7TJpB.cjs");
1
+ require("./chunk-CKQMccvm.cjs");
2
+ const require_hash = require("./hash-CxBlj5Dz.cjs");
3
3
  let node_fs_promises = require("node:fs/promises");
4
4
  let node_path = require("node:path");
5
5
  //#region src/features/catalogue/merge.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saykit/config",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "CLI and configuration tooling for saykit",
5
5
  "keywords": [
6
6
  "cli",
@@ -1,121 +0,0 @@
1
- require("./index.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
- let node_os = require("node:os");
7
- //#region src/features/loader/files.ts
8
- const SUPPORTED_CONFIG_FILES = [
9
- "saykit.config.js",
10
- "saykit.config.cjs",
11
- "saykit.config.mjs",
12
- "saykit.config.ts",
13
- "saykit.config.mts",
14
- "saykit.config.cts"
15
- ];
16
- function getConfigFileCandidates(name) {
17
- return SUPPORTED_CONFIG_FILES.map((file) => file.replace("saykit", name));
18
- }
19
- function findConfigFile(moduleName, projectDir) {
20
- for (const fileName of getConfigFileCandidates(moduleName)) {
21
- const id = (0, node_path.join)(projectDir, fileName);
22
- if ((0, node_fs.existsSync)(id)) return { id };
23
- }
24
- return null;
25
- }
26
- //#endregion
27
- //#region src/features/loader/module.ts
28
- function findNearestTsConfig(fromPath) {
29
- let dir = (0, node_path.dirname)(fromPath);
30
- const { root } = (0, node_path.parse)(dir);
31
- while (true) {
32
- const candidate = (0, node_path.join)(dir, "tsconfig.json");
33
- if ((0, node_fs.existsSync)(candidate)) return candidate;
34
- if (dir === root) return null;
35
- dir = (0, node_path.dirname)(dir);
36
- }
37
- }
38
- function findCacheDir(fromPath) {
39
- let dir = (0, node_path.dirname)(fromPath);
40
- const { root } = (0, node_path.parse)(dir);
41
- while (true) {
42
- const nm = (0, node_path.join)(dir, "node_modules");
43
- if ((0, node_fs.existsSync)(nm)) return (0, node_path.join)(nm, ".cache", "saykit", "config");
44
- if (dir === root) return (0, node_path.join)((0, node_os.tmpdir)(), "saykit", "config");
45
- dir = (0, node_path.dirname)(dir);
46
- }
47
- }
48
- function transpile(path, tsConfigPath, require) {
49
- const ts = require("typescript");
50
- const { config: tsConfig, error } = tsConfigPath ? ts.readConfigFile(tsConfigPath, ts.sys.readFile) : {
51
- config: {},
52
- error: null
53
- };
54
- if (error) throw error;
55
- tsConfig.compilerOptions = {
56
- ...tsConfig.compilerOptions,
57
- allowJs: true,
58
- esModuleInterop: true,
59
- module: ts.ModuleKind.CommonJS,
60
- moduleResolution: ts.ModuleResolutionKind.NodeJs,
61
- target: ts.ScriptTarget.ES2022,
62
- noEmit: false
63
- };
64
- return ts.transpileModule((0, node_fs.readFileSync)(path, "utf8"), tsConfig).outputText;
65
- }
66
- function loadWithCache(path) {
67
- const require = (0, node_module.createRequire)(path);
68
- const mtimeMs = (0, node_fs.statSync)(path).mtimeMs;
69
- const tsConfigPath = findNearestTsConfig(path);
70
- const tsConfigMtimeMs = tsConfigPath ? (0, node_fs.statSync)(tsConfigPath).mtimeMs : 0;
71
- const hash = (0, node_crypto.createHash)("sha1").update(`${path}\0${tsConfigPath ?? ""}\0${tsConfigMtimeMs}`).digest("hex").slice(0, 16);
72
- const cacheDir = findCacheDir(path);
73
- const cachePath = (0, node_path.join)(cacheDir, `${hash}.${mtimeMs}.cjs`);
74
- try {
75
- if (!(0, node_fs.existsSync)(cachePath)) {
76
- (0, node_fs.mkdirSync)(cacheDir, { recursive: true });
77
- (0, node_fs.writeFileSync)(cachePath, transpile(path, tsConfigPath, require));
78
- if ((0, node_fs.existsSync)(cacheDir)) {
79
- for (const entry of (0, node_fs.readdirSync)(cacheDir)) if (entry.startsWith(`${hash}.`) && entry !== `${hash}.${mtimeMs}.cjs`) try {
80
- (0, node_fs.unlinkSync)((0, node_path.join)(cacheDir, entry));
81
- } catch {}
82
- }
83
- }
84
- const resolved = require.resolve(cachePath);
85
- delete require.cache[resolved];
86
- const module = require(cachePath);
87
- delete require.cache[resolved];
88
- return module?.default ?? module;
89
- } catch (error) {
90
- throw new Error("Failed to import module", { cause: error });
91
- }
92
- }
93
- const js = (path) => loadWithCache(path);
94
- const ts = (path) => loadWithCache(path);
95
- const configLoaders = Object.freeze({
96
- ".js": js,
97
- ".mjs": js,
98
- ".cjs": js,
99
- ".ts": ts,
100
- ".mts": ts,
101
- ".cts": ts
102
- });
103
- //#endregion
104
- //#region src/features/loader/resolve.ts
105
- function resolveConfig(name = "saykit") {
106
- const file = findConfigFile(name, process.cwd());
107
- if (!file) throw new Error(`Could not find config file for "${name}"`);
108
- const ext = (0, node_path.extname)(file.id).toLowerCase();
109
- const load = ext in configLoaders ? configLoaders[ext] : null;
110
- if (!load) throw new Error(`Unsupported config file type "${ext}" for "${name}"`);
111
- const config = load(file.id);
112
- if (!config || typeof config !== "object") throw new Error(`Invalid config file for "${name}"`);
113
- return config;
114
- }
115
- //#endregion
116
- Object.defineProperty(exports, "resolveConfig", {
117
- enumerable: true,
118
- get: function() {
119
- return resolveConfig;
120
- }
121
- });
@@ -1,115 +0,0 @@
1
- import { createRequire } from "node:module";
2
- import { dirname, extname, join, parse } from "node:path";
3
- import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, unlinkSync, writeFileSync } from "node:fs";
4
- import { createHash } from "node:crypto";
5
- import { tmpdir } from "node:os";
6
- //#region src/features/loader/files.ts
7
- const SUPPORTED_CONFIG_FILES = [
8
- "saykit.config.js",
9
- "saykit.config.cjs",
10
- "saykit.config.mjs",
11
- "saykit.config.ts",
12
- "saykit.config.mts",
13
- "saykit.config.cts"
14
- ];
15
- function getConfigFileCandidates(name) {
16
- return SUPPORTED_CONFIG_FILES.map((file) => file.replace("saykit", name));
17
- }
18
- function findConfigFile(moduleName, projectDir) {
19
- for (const fileName of getConfigFileCandidates(moduleName)) {
20
- const id = join(projectDir, fileName);
21
- if (existsSync(id)) return { id };
22
- }
23
- return null;
24
- }
25
- //#endregion
26
- //#region src/features/loader/module.ts
27
- function findNearestTsConfig(fromPath) {
28
- let dir = dirname(fromPath);
29
- const { root } = parse(dir);
30
- while (true) {
31
- const candidate = join(dir, "tsconfig.json");
32
- if (existsSync(candidate)) return candidate;
33
- if (dir === root) return null;
34
- dir = dirname(dir);
35
- }
36
- }
37
- function findCacheDir(fromPath) {
38
- let dir = dirname(fromPath);
39
- const { root } = parse(dir);
40
- while (true) {
41
- const nm = join(dir, "node_modules");
42
- if (existsSync(nm)) return join(nm, ".cache", "saykit", "config");
43
- if (dir === root) return join(tmpdir(), "saykit", "config");
44
- dir = dirname(dir);
45
- }
46
- }
47
- function transpile(path, tsConfigPath, require) {
48
- const ts = require("typescript");
49
- const { config: tsConfig, error } = tsConfigPath ? ts.readConfigFile(tsConfigPath, ts.sys.readFile) : {
50
- config: {},
51
- error: null
52
- };
53
- if (error) throw error;
54
- tsConfig.compilerOptions = {
55
- ...tsConfig.compilerOptions,
56
- allowJs: true,
57
- esModuleInterop: true,
58
- module: ts.ModuleKind.CommonJS,
59
- moduleResolution: ts.ModuleResolutionKind.NodeJs,
60
- target: ts.ScriptTarget.ES2022,
61
- noEmit: false
62
- };
63
- return ts.transpileModule(readFileSync(path, "utf8"), tsConfig).outputText;
64
- }
65
- function loadWithCache(path) {
66
- const require = createRequire(path);
67
- const mtimeMs = statSync(path).mtimeMs;
68
- const tsConfigPath = findNearestTsConfig(path);
69
- const tsConfigMtimeMs = tsConfigPath ? statSync(tsConfigPath).mtimeMs : 0;
70
- const hash = createHash("sha1").update(`${path}\0${tsConfigPath ?? ""}\0${tsConfigMtimeMs}`).digest("hex").slice(0, 16);
71
- const cacheDir = findCacheDir(path);
72
- const cachePath = join(cacheDir, `${hash}.${mtimeMs}.cjs`);
73
- try {
74
- if (!existsSync(cachePath)) {
75
- mkdirSync(cacheDir, { recursive: true });
76
- writeFileSync(cachePath, transpile(path, tsConfigPath, require));
77
- if (existsSync(cacheDir)) {
78
- for (const entry of readdirSync(cacheDir)) if (entry.startsWith(`${hash}.`) && entry !== `${hash}.${mtimeMs}.cjs`) try {
79
- unlinkSync(join(cacheDir, entry));
80
- } catch {}
81
- }
82
- }
83
- const resolved = require.resolve(cachePath);
84
- delete require.cache[resolved];
85
- const module = require(cachePath);
86
- delete require.cache[resolved];
87
- return module?.default ?? module;
88
- } catch (error) {
89
- throw new Error("Failed to import module", { cause: error });
90
- }
91
- }
92
- const js = (path) => loadWithCache(path);
93
- const ts = (path) => loadWithCache(path);
94
- const configLoaders = Object.freeze({
95
- ".js": js,
96
- ".mjs": js,
97
- ".cjs": js,
98
- ".ts": ts,
99
- ".mts": ts,
100
- ".cts": ts
101
- });
102
- //#endregion
103
- //#region src/features/loader/resolve.ts
104
- function resolveConfig(name = "saykit") {
105
- const file = findConfigFile(name, process.cwd());
106
- if (!file) throw new Error(`Could not find config file for "${name}"`);
107
- const ext = extname(file.id).toLowerCase();
108
- const load = ext in configLoaders ? configLoaders[ext] : null;
109
- if (!load) throw new Error(`Unsupported config file type "${ext}" for "${name}"`);
110
- const config = load(file.id);
111
- if (!config || typeof config !== "object") throw new Error(`Invalid config file for "${name}"`);
112
- return config;
113
- }
114
- //#endregion
115
- export { resolveConfig as t };