|
1 | | -import fs from "node:fs"; |
2 | | -import path from "node:path"; |
3 | 1 | import { globalIgnores } from "eslint/config"; |
4 | 2 | import semver from "semver"; |
5 | 3 | import configs from "./configs/index.js"; |
6 | 4 | import { typescriptExtensions } from "./configs/utils/extensions.js"; |
| 5 | +import getJsonFile from "./configs/utils/get-json-file.js"; |
| 6 | +import isTypescriptInstalled from "./configs/utils/is-typescript-installed.js"; |
7 | 7 | import ignorePaths from "./ignore-paths.js"; |
8 | 8 |
|
9 | | -const SKIP_TIME = 5000; |
10 | | - |
11 | | -class Cache { |
12 | | - /** |
13 | | - * Initialize this cache instance. |
14 | | - */ |
15 | | - constructor() { |
16 | | - this.map = new Map(); |
17 | | - } |
18 | | - |
19 | | - /** |
20 | | - * Get the cached value of the given key. |
21 | | - * @param {string} key The key to get. |
22 | | - * @returns {import('type-fest').JsonObject} The cached value or null. |
23 | | - */ |
24 | | - get(key) { |
25 | | - const entry = this.map.get(key); |
26 | | - const now = Date.now(); |
27 | | - |
28 | | - if (entry) { |
29 | | - if (entry.expire > now) { |
30 | | - entry.expire = now + SKIP_TIME; |
31 | | - return entry.value; |
32 | | - } |
33 | | - this.map.delete(key); |
34 | | - } |
35 | | - return null; |
36 | | - } |
37 | | - |
38 | | - /** |
39 | | - * Set the value of the given key. |
40 | | - * @param {string} key The key to set. |
41 | | - * @param {import('type-fest').JsonObject} value The value to set. |
42 | | - * @returns {void} |
43 | | - */ |
44 | | - set(key, value) { |
45 | | - const entry = this.map.get(key); |
46 | | - const expire = Date.now() + SKIP_TIME; |
47 | | - |
48 | | - if (entry) { |
49 | | - entry.value = value; |
50 | | - entry.expire = expire; |
51 | | - } else { |
52 | | - this.map.set(key, { value, expire }); |
53 | | - } |
54 | | - } |
55 | | -} |
56 | | - |
57 | | -const cache = new Cache(); |
58 | | - |
59 | | -/** |
60 | | - * Reads the `package.json` data in a given path. |
61 | | - * |
62 | | - * Don't cache the data. |
63 | | - * @param {string} dir The path to a directory to read. |
64 | | - * @param {string} filename The filename. |
65 | | - * @returns {import('type-fest').JsonObject|null} The read `package.json` data, or null. |
66 | | - */ |
67 | | -function readJsonFile(dir, filename) { |
68 | | - const filePath = path.join(dir, filename); |
69 | | - try { |
70 | | - const text = fs.readFileSync(filePath, "utf8"); |
71 | | - const data = JSON.parse(text); |
72 | | - |
73 | | - if ( |
74 | | - data !== null && |
75 | | - typeof data === "object" && |
76 | | - Array.isArray(data) === false |
77 | | - ) { |
78 | | - data.filePath = filePath; |
79 | | - return data; |
80 | | - } |
81 | | - // eslint-disable-next-line unicorn/prefer-optional-catch-binding |
82 | | - } catch (_err) { |
83 | | - // do nothing. |
84 | | - } |
85 | | - |
86 | | - return null; |
87 | | -} |
88 | | - |
89 | | -/** |
90 | | - * Gets a `package.json` data. |
91 | | - * The data is cached if found, then it's used after. |
92 | | - * @param {string} filename The filename. |
93 | | - * @param {string=} startPath A file path to lookup. |
94 | | - * @returns {import('type-fest').JsonObject | null} A found `package.json` data or `null`. |
95 | | - * This object have additional property `filePath`. |
96 | | - */ |
97 | | -function getJsonFile(filename, startPath = "a.js") { |
98 | | - const startDir = path.dirname(path.resolve(startPath)); |
99 | | - let dir = startDir; |
100 | | - let prevDir = ""; |
101 | | - let data = null; |
102 | | - |
103 | | - do { |
104 | | - data = cache.get(dir + filename); |
105 | | - if (data) { |
106 | | - if (dir !== startDir) { |
107 | | - cache.set(startDir + filename, data); |
108 | | - } |
109 | | - return data; |
110 | | - } |
111 | | - |
112 | | - data = readJsonFile(dir, filename); |
113 | | - if (data) { |
114 | | - cache.set(dir + filename, data); |
115 | | - cache.set(startDir + filename, data); |
116 | | - return data; |
117 | | - } |
118 | | - |
119 | | - // Go to next. |
120 | | - prevDir = dir; |
121 | | - dir = path.resolve(dir, ".."); |
122 | | - } while (dir !== prevDir); |
123 | | - |
124 | | - cache.set(startDir + filename, null); |
125 | | - return null; |
126 | | -} |
127 | | - |
128 | 9 | const packageJson = getJsonFile("package.json"); |
129 | 10 | const isModule = |
130 | 11 | packageJson !== null && |
@@ -199,23 +80,17 @@ function getJavascriptConfig() { |
199 | 80 | * @returns {Promise<Record<string, string>>} config |
200 | 81 | */ |
201 | 82 | function getTypescriptJSdocConfig() { |
202 | | - if (packageJson === null) { |
203 | | - return []; |
204 | | - } |
205 | | - |
206 | | - const dependencies = packageJson.dependencies || []; |
207 | | - const devDependencies = packageJson.devDependencies || []; |
208 | | - |
209 | | - return typeof dependencies.typescript !== "undefined" || |
210 | | - typeof devDependencies.typescript !== "undefined" |
211 | | - ? configs["typescript/jsdoc"] |
212 | | - : []; |
| 83 | + return isTypescriptInstalled() ? configs["typescript/jsdoc"] : []; |
213 | 84 | } |
214 | 85 |
|
215 | 86 | /** |
216 | 87 | * @returns {Promise<Record<string, string>>} config |
217 | 88 | */ |
218 | 89 | function getTypescriptConfig() { |
| 90 | + if (!isTypescriptInstalled()) { |
| 91 | + return {}; |
| 92 | + } |
| 93 | + |
219 | 94 | const tsconfigJson = getJsonFile("tsconfig.json"); |
220 | 95 |
|
221 | 96 | const isNoEmitEnabled = |
|
0 commit comments