@hkdigital/lib-sveltekit 0.0.96 → 0.0.97
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 @@
|
|
1
|
+
export * from "./route-folders/index.js";
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './route-folders/index.js';
|
@@ -0,0 +1,23 @@
|
|
1
|
+
/**
|
2
|
+
* Validates if a path is within the project's src/routes directory
|
3
|
+
* @param {string} path - Path to validate
|
4
|
+
* @returns {boolean}
|
5
|
+
*/
|
6
|
+
export function isValidRoutePath(path: string): boolean;
|
7
|
+
/**
|
8
|
+
* Scans route folders recursively, only including folders with +page.svelte
|
9
|
+
* @param {Object} options - Scan options
|
10
|
+
* @param {string} options.dirPath - Directory path to scan
|
11
|
+
* @param {number} [options.maxDepth=1] - Maximum depth to scan
|
12
|
+
* @param {Set<string>} [options.skipFolders=new Set(['assets'])] - Folders to skip
|
13
|
+
* @returns {Promise<Array<{displayName: string, path: string}>>}
|
14
|
+
* @throws {Error} If path is outside project routes directory
|
15
|
+
*/
|
16
|
+
export function scanRouteFolders({ dirPath, maxDepth, skipFolders }: {
|
17
|
+
dirPath: string;
|
18
|
+
maxDepth?: number;
|
19
|
+
skipFolders?: Set<string>;
|
20
|
+
}): Promise<Array<{
|
21
|
+
displayName: string;
|
22
|
+
path: string;
|
23
|
+
}>>;
|
@@ -0,0 +1,82 @@
|
|
1
|
+
import { readdir } from 'node:fs/promises';
|
2
|
+
import { join, resolve } from 'node:path';
|
3
|
+
|
4
|
+
/**
|
5
|
+
* Validates if a path is within the project's src/routes directory
|
6
|
+
* @param {string} path - Path to validate
|
7
|
+
* @returns {boolean}
|
8
|
+
*/
|
9
|
+
export function isValidRoutePath(path) {
|
10
|
+
const normalizedPath = resolve(path);
|
11
|
+
const routesPath = resolve(process.cwd(), 'src/routes');
|
12
|
+
return normalizedPath.startsWith(routesPath);
|
13
|
+
}
|
14
|
+
|
15
|
+
/**
|
16
|
+
* Scans route folders recursively, only including folders with +page.svelte
|
17
|
+
* @param {Object} options - Scan options
|
18
|
+
* @param {string} options.dirPath - Directory path to scan
|
19
|
+
* @param {number} [options.maxDepth=1] - Maximum depth to scan
|
20
|
+
* @param {Set<string>} [options.skipFolders=new Set(['assets'])] - Folders to skip
|
21
|
+
* @returns {Promise<Array<{displayName: string, path: string}>>}
|
22
|
+
* @throws {Error} If path is outside project routes directory
|
23
|
+
*/
|
24
|
+
export async function scanRouteFolders({
|
25
|
+
dirPath,
|
26
|
+
maxDepth = 1,
|
27
|
+
skipFolders = new Set(['assets'])
|
28
|
+
}) {
|
29
|
+
if (!isValidRoutePath(dirPath)) {
|
30
|
+
throw new Error('Invalid path: Must be within src/routes directory');
|
31
|
+
}
|
32
|
+
|
33
|
+
if (maxDepth < 1) return [];
|
34
|
+
|
35
|
+
try {
|
36
|
+
const entries = await readdir(dirPath, { withFileTypes: true });
|
37
|
+
const results = [];
|
38
|
+
|
39
|
+
for (const entry of entries) {
|
40
|
+
if (
|
41
|
+
!entry.isDirectory() ||
|
42
|
+
skipFolders.has(entry.name) ||
|
43
|
+
entry.name.startsWith('.')
|
44
|
+
) {
|
45
|
+
continue;
|
46
|
+
}
|
47
|
+
|
48
|
+
const fullPath = join(dirPath, entry.name);
|
49
|
+
const currentPath = entry.name;
|
50
|
+
|
51
|
+
const dirContents = await readdir(fullPath);
|
52
|
+
const hasPageFile = dirContents.includes('+page.svelte');
|
53
|
+
|
54
|
+
if (hasPageFile) {
|
55
|
+
results.push({
|
56
|
+
displayName: entry.name,
|
57
|
+
path: currentPath
|
58
|
+
});
|
59
|
+
}
|
60
|
+
|
61
|
+
if (maxDepth > 1) {
|
62
|
+
const subFolders = await scanRouteFolders({
|
63
|
+
dirPath: fullPath,
|
64
|
+
maxDepth: maxDepth - 1,
|
65
|
+
skipFolders
|
66
|
+
});
|
67
|
+
|
68
|
+
for (const subFolder of subFolders) {
|
69
|
+
results.push({
|
70
|
+
displayName: `${entry.name}/${subFolder.displayName}`,
|
71
|
+
path: `${currentPath}/${subFolder.path}`
|
72
|
+
});
|
73
|
+
}
|
74
|
+
}
|
75
|
+
}
|
76
|
+
|
77
|
+
return results;
|
78
|
+
} catch (err) {
|
79
|
+
console.error(err);
|
80
|
+
throw new Error('Failed to scan directory');
|
81
|
+
}
|
82
|
+
}
|