@1money/react-ui 1.14.3 → 1.14.5

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,102 @@
1
+ /* eslint-env node */
2
+ import fs from 'fs/promises';
3
+ import path from 'path';
4
+ import { fileURLToPath, pathToFileURL } from 'url';
5
+
6
+ const __filename = fileURLToPath(import.meta.url);
7
+ const __dirname = path.dirname(__filename);
8
+
9
+ const repoRoot = path.resolve(__dirname, '../../');
10
+ const componentsRoot = path.join(repoRoot, 'src/components');
11
+
12
+ let cachedResources = null;
13
+
14
+ function isWithinComponents(targetPath) {
15
+ const absolute = path.resolve(targetPath);
16
+ return absolute.startsWith(componentsRoot);
17
+ }
18
+
19
+ function detectMimeType(filePath) {
20
+ const ext = path.extname(filePath).toLowerCase();
21
+ if (ext === '.ts') return 'text/typescript';
22
+ if (ext === '.tsx') return 'text/tsx';
23
+ if (ext === '.js') return 'text/javascript';
24
+ if (ext === '.jsx') return 'text/jsx';
25
+ if (ext === '.scss' || ext === '.css' || ext === '.less') {
26
+ return 'text/css';
27
+ }
28
+ if (ext === '.md' || ext === '.mdx') return 'text/markdown';
29
+ return 'text/plain';
30
+ }
31
+
32
+ async function walkFiles(dir) {
33
+ const entries = await fs.readdir(dir, { withFileTypes: true });
34
+ const files = [];
35
+ for (const entry of entries) {
36
+ if (entry.name.startsWith('.')) continue;
37
+ const fullPath = path.join(dir, entry.name);
38
+ if (entry.isDirectory()) {
39
+ files.push(...(await walkFiles(fullPath)));
40
+ } else {
41
+ files.push(fullPath);
42
+ }
43
+ }
44
+ return files;
45
+ }
46
+
47
+ async function buildResourceCache() {
48
+ try {
49
+ const files = await walkFiles(componentsRoot);
50
+ return files.map(filePath => {
51
+ const uri = pathToFileURL(filePath).href;
52
+ return {
53
+ uri,
54
+ filePath,
55
+ mimeType: detectMimeType(filePath),
56
+ name: path.relative(repoRoot, filePath)
57
+ };
58
+ });
59
+ } catch (error) {
60
+ throw new Error(
61
+ `Failed to scan components under ${componentsRoot}: ${error}`
62
+ );
63
+ }
64
+ }
65
+
66
+ async function ensureCache() {
67
+ if (!cachedResources) {
68
+ cachedResources = await buildResourceCache();
69
+ }
70
+ return cachedResources;
71
+ }
72
+
73
+ async function readFile(entry) {
74
+ if (!isWithinComponents(entry.filePath)) {
75
+ throw new Error('Resource not allowed');
76
+ }
77
+
78
+ const text = await fs.readFile(entry.filePath, 'utf8');
79
+ return {
80
+ contents: [
81
+ { uri: entry.uri, mimeType: entry.mimeType, text }
82
+ ]
83
+ };
84
+ }
85
+
86
+ export async function registerComponentResources(server) {
87
+ const resources = await ensureCache();
88
+
89
+ resources.forEach(entry => {
90
+ server.registerResource(
91
+ entry.name,
92
+ entry.uri,
93
+ {
94
+ description: 'src/components resource',
95
+ mimeType: entry.mimeType
96
+ },
97
+ async () => readFile(entry)
98
+ );
99
+ });
100
+
101
+ return resources.length;
102
+ }