-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewsHealthController.ts
More file actions
199 lines (172 loc) · 5.31 KB
/
viewsHealthController.ts
File metadata and controls
199 lines (172 loc) · 5.31 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { FastifyRequest, FastifyReply } from 'fastify';
import * as fs from 'fs';
import * as path from 'path';
interface FileSystemItem {
name: string;
type: 'file' | 'directory';
path: string;
size?: number;
lastModified?: string;
children?: FileSystemItem[];
}
interface ViewsHealthResponse {
status: 'ok' | 'error';
timestamp: string;
viewsDirectory: {
path: string;
exists: boolean;
writable: boolean;
totalFiles: number;
totalDirectories: number;
totalSize: number;
contents: FileSystemItem[];
};
message?: string;
}
/**
* Recursively scans a directory and returns its structure
*/
function scanDirectory(dirPath: string, relativePath: string = ''): FileSystemItem[] {
const items: FileSystemItem[] = [];
try {
const entries = fs.readdirSync(dirPath);
for (const entry of entries) {
// Skip hidden files and directories (like .git, .DS_Store)
if (entry.startsWith('.')) {
continue;
}
const fullPath = path.join(dirPath, entry);
const itemRelativePath = path.join(relativePath, entry);
const stats = fs.statSync(fullPath);
const item: FileSystemItem = {
name: entry,
type: stats.isDirectory() ? 'directory' : 'file',
path: itemRelativePath || entry,
lastModified: stats.mtime.toISOString()
};
if (stats.isFile()) {
item.size = stats.size;
} else if (stats.isDirectory()) {
// Recursively scan subdirectories
item.children = scanDirectory(fullPath, itemRelativePath);
}
items.push(item);
}
} catch (error) {
console.error(`Error scanning directory ${dirPath}:`, error);
}
return items.sort((a, b) => {
// Sort directories first, then files, both alphabetically
if (a.type !== b.type) {
return a.type === 'directory' ? -1 : 1;
}
return a.name.localeCompare(b.name);
});
}
/**
* Calculates statistics from the file system structure
*/
function calculateStats(items: FileSystemItem[]): { files: number; directories: number; totalSize: number } {
let files = 0;
let directories = 0;
let totalSize = 0;
for (const item of items) {
if (item.type === 'file') {
files++;
totalSize += item.size || 0;
} else if (item.type === 'directory') {
directories++;
if (item.children) {
const childStats = calculateStats(item.children);
files += childStats.files;
directories += childStats.directories;
totalSize += childStats.totalSize;
}
}
}
return { files, directories, totalSize };
}
/**
* Tests if a directory is writable
*/
function testWritePermission(dirPath: string): boolean {
try {
const testFile = path.join(dirPath, '.write-test-' + Date.now());
fs.writeFileSync(testFile, 'test');
fs.unlinkSync(testFile);
return true;
} catch {
return false;
}
}
export const viewsHealthHandler = async (request: FastifyRequest, reply: FastifyReply) => {
try {
const viewsPath = path.join(process.cwd(), 'views');
const timestamp = new Date().toISOString();
console.log(`Views health check requested at ${timestamp} for path: ${viewsPath}`);
// Check if views directory exists
const exists = fs.existsSync(viewsPath);
if (!exists) {
const response: ViewsHealthResponse = {
status: 'error',
timestamp,
viewsDirectory: {
path: viewsPath,
exists: false,
writable: false,
totalFiles: 0,
totalDirectories: 0,
totalSize: 0,
contents: []
},
message: 'Views directory does not exist'
};
reply.status(404);
return response;
}
// Test write permissions
const writable = testWritePermission(viewsPath);
// Scan directory contents
const contents = scanDirectory(viewsPath);
const stats = calculateStats(contents);
const response: ViewsHealthResponse = {
status: 'ok',
timestamp,
viewsDirectory: {
path: viewsPath,
exists: true,
writable,
totalFiles: stats.files,
totalDirectories: stats.directories,
totalSize: stats.totalSize,
contents
}
};
// Add informational messages based on the state
if (stats.files === 0 && stats.directories === 0) {
response.message = 'Views directory is empty - no GitHub repository has been synced yet';
} else {
response.message = `Found ${stats.files} files and ${stats.directories} directories (${(stats.totalSize / 1024).toFixed(2)} KB total)`;
}
console.log(`Views health check completed: ${response.message}`);
return response;
} catch (error) {
request.log.error('Error in viewsHealthHandler: %s', error instanceof Error ? error.message : String(error));
const response: ViewsHealthResponse = {
status: 'error',
timestamp: new Date().toISOString(),
viewsDirectory: {
path: path.join(process.cwd(), 'views'),
exists: false,
writable: false,
totalFiles: 0,
totalDirectories: 0,
totalSize: 0,
contents: []
},
message: `Health check failed: ${error instanceof Error ? error.message : 'Unknown error'}`
};
reply.status(500);
return response;
}
};