-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathserver.ts
More file actions
104 lines (85 loc) · 2.83 KB
/
Copy pathserver.ts
File metadata and controls
104 lines (85 loc) · 2.83 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
import { glob } from 'glob';
import apiRoutes, { updateHtmlFiles } from './routes.js';
import { ProjectFile } from './types.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Server configuration
const app = express();
const PORT = process.env.PORT || 3333;
// Serve static files from the current directory
app.use(express.static(__dirname));
// Allow CORS for development
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
// Use API routes
app.use('/api', apiRoutes);
// Function to extract project and stage info from filename
function extractFileInfo(filePath: string): ProjectFile | null {
// Extract filename from path
const fileName = path.basename(filePath);
// Match pattern like: 1_256_set_up_backend.html
const stageMatch = fileName.match(/^(\d+)_(\d+)_(.+)\.html$/);
if (stageMatch) {
const order = parseInt(stageMatch[1], 10);
const stageId = stageMatch[2];
const title = stageMatch[3].replace(/_/g, ' ');
// Extract project info from directory path
const dirName = path.dirname(filePath);
const projectMatch = path.basename(dirName).match(/^project_(\d+)_(.+)$/);
if (projectMatch) {
const projectId = projectMatch[1];
const projectName = projectMatch[2].replace(/_/g, ' ');
return {
path: filePath,
project: `${projectId}: ${projectName}`,
stage: `${stageId}: ${title}`,
order,
title
};
}
}
return null;
}
// Scan for HTML files
export async function scanHtmlFiles() {
try {
const files = await glob('project_*/**/*.html', { ignore: 'node_modules/**' });
const projectFiles = files
.map(file => extractFileInfo(file))
.filter((file): file is ProjectFile => file !== null)
.sort((a, b) => {
// First sort by project
if (a.project !== b.project) {
return a.project.localeCompare(b.project);
}
// Then by order
return a.order - b.order;
});
// Update the shared file list
updateHtmlFiles(projectFiles);
console.log(`Found ${projectFiles.length} HTML files`);
return projectFiles;
} catch (error) {
console.error('Error scanning HTML files:', error);
return [];
}
}
// Serve index.html for all routes (SPA handling)
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
// Initial scan of HTML files
async function startServer() {
// Perform initial scan
await scanHtmlFiles();
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});
}
startServer();