forked from hyperskill/enlighter-content
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.ts
More file actions
88 lines (73 loc) · 2.42 KB
/
Copy pathroutes.ts
File metadata and controls
88 lines (73 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import express from 'express';
import fs from 'fs/promises';
import { ProjectFile } from './types.js';
import { scanHtmlFiles } from './server.js';
const router = express.Router();
// Store for HTML files
let htmlFiles: ProjectFile[] = [];
// Update the HTML files list
export const updateHtmlFiles = (files: ProjectFile[]) => {
htmlFiles = files;
};
// API endpoint to get all HTML files
router.get('/files', async (req, res) => {
try {
// Scan for files on demand
const projectFiles = await scanHtmlFiles();
// Group files by project
const projectsMap = new Map<string, ProjectFile[]>();
projectFiles.forEach(file => {
if (!projectsMap.has(file.project)) {
projectsMap.set(file.project, []);
}
projectsMap.get(file.project)!.push(file);
});
// Convert to array
const projects = Array.from(projectsMap.entries()).map(([project, files]) => ({
name: project,
stages: files.sort((a, b) => a.order - b.order)
}));
res.json(projects);
} catch (error) {
console.error('Error processing files:', error);
res.status(500).json({ error: 'Failed to scan files' });
}
});
// Get specific file info
router.get('/file/:path', async (req, res) => {
try {
// Scan for files on demand to ensure up-to-date info
const projectFiles = await scanHtmlFiles();
const filePath = decodeURIComponent(req.params.path);
const file = projectFiles.find(f => f.path === filePath);
if (file) {
res.json(file);
} else {
res.status(404).json({ error: 'File not found' });
}
} catch (error) {
console.error('Error processing file request:', error);
res.status(500).json({ error: 'Failed to scan files' });
}
});
// Get HTML content for a specific file
router.get('/content', async (req, res) => {
try {
const filePath = req.query.path as string;
if (!filePath) {
return res.status(400).json({ error: 'File path is required' });
}
try {
// Read the file content
const content = await fs.readFile(filePath, 'utf-8');
res.send(content);
} catch (error) {
console.error(`Error reading file ${filePath}:`, error);
res.status(404).json({ error: 'File not found or could not be read' });
}
} catch (error) {
console.error('Error processing content request:', error);
res.status(500).json({ error: 'Server error' });
}
});
export default router;