@utoo/lint 0.3.8 → 0.4.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.
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+
3
+ // Loads a `utlint.config.{ts,js,mjs,cjs,mts,cts}` module in-process so that
4
+ // live plugin objects (with their `create()` functions) are available to the
5
+ // ESLint plugin runner. The native engine keeps receiving the serialized JSON
6
+ // form of the same config through `config-loader`.
7
+ //
8
+ // CommonJS on purpose: shared by the ESM and CommonJS entry points.
9
+
10
+ const { statSync } = require("node:fs");
11
+ const { resolve: resolvePath } = require("node:path");
12
+
13
+ const cache = new Map();
14
+
15
+ function configModuleValue(loaded) {
16
+ if (loaded && typeof loaded === "object" && !Array.isArray(loaded) && "default" in loaded) {
17
+ return loaded.default;
18
+ }
19
+ return loaded;
20
+ }
21
+
22
+ function readConfigModule(path, cwd) {
23
+ const configPath = resolvePath(cwd ?? process.cwd(), path);
24
+ let mtimeMs = 0;
25
+ try {
26
+ mtimeMs = statSync(configPath).mtimeMs;
27
+ } catch {
28
+ // Let jiti surface the actual filesystem error below.
29
+ }
30
+ const cached = cache.get(configPath);
31
+ if (cached && cached.mtimeMs === mtimeMs) {
32
+ return cached.value;
33
+ }
34
+
35
+ const { createJiti } = require("jiti");
36
+ const jiti = createJiti(configPath, { interopDefault: true });
37
+ let value;
38
+ try {
39
+ value = configModuleValue(jiti(configPath));
40
+ } catch (error) {
41
+ throw new Error(`utoo-lint unable to load config ${configPath}: ${error?.message ?? error}`, { cause: error });
42
+ }
43
+ cache.set(configPath, { mtimeMs, value });
44
+ return value;
45
+ }
46
+
47
+ function clearConfigModuleCache() {
48
+ cache.clear();
49
+ }
50
+
51
+ module.exports = {
52
+ clearConfigModuleCache,
53
+ readConfigModule
54
+ };