-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_compiler.ts
More file actions
205 lines (168 loc) · 5.8 KB
/
Copy pathweb_compiler.ts
File metadata and controls
205 lines (168 loc) · 5.8 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
200
201
202
203
204
205
import fs from "fs";
import path from "path";
const __dirname = path.resolve();
interface IFile {
path: string;
content: string;
name: string;
type: string; // content-type
}
// Function to get content type based on file extension
function getContentType(filePath: string): string {
const ext = path.extname(filePath).toLowerCase();
const contentTypes: { [key: string]: string } = {
".html": "text/html",
".css": "text/css",
".js": "application/javascript",
".json": "application/json",
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".gif": "image/gif",
".svg": "image/svg+xml",
".ico": "image/x-icon",
".txt": "text/plain",
".pdf": "application/pdf",
};
return contentTypes[ext] || "application/octet-stream";
}
// Function to read files recursively
function readFilesSync(dir: string, baseDir: string) {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const filePath = path.join(dir, file);
const fileStat = fs.statSync(filePath);
if (fileStat.isDirectory()) {
readFilesSync(filePath, baseDir);
} else {
// Get relative path from public folder
const relativePath = path.relative(baseDir, filePath).replace(/\\/g, "/");
// Read file content and convert everything to base64
const contentType = getContentType(filePath);
const buffer = fs.readFileSync(filePath);
const content = buffer.toString("base64");
publicFiles.push({
path: `/${relativePath}`,
content: content,
name: file,
type: contentType,
});
console.log(`Added: ${relativePath} (${contentType})`);
}
});
}
// Generate TypeScript content
const tsContent = (
files: IFile[]
) => `// Auto-generated file - DO NOT EDIT MANUALLY
// Generated at: ${new Date().toISOString()}
export interface ICompiledFile {
path: string;
content: string; // base64 encoded
name: string;
type: string;
}
export interface IDecodedFile {
path: string;
content: string | Buffer; // decoded content
name: string;
type: string;
}
export const publicFiles: ICompiledFile[] = ${JSON.stringify(files, null, 2)};
// Get file with base64 content (raw)
export function getFile(path: string): ICompiledFile | undefined {
return publicFiles.find(file => file.path === path);
}
// Get file with decoded content
export function getDecodedFile(path: string): IDecodedFile | undefined {
const file = publicFiles.find(file => file.path === path);
if (!file) return undefined;
const isTextFile = file.type.startsWith('text/') ||
file.type === 'application/javascript' ||
file.type === 'application/json';
const content = isTextFile
? Buffer.from(file.content, 'base64').toString('utf-8')
: Buffer.from(file.content, 'base64');
return {
path: file.path,
content: content,
name: file.name,
type: file.type
};
}
// Get file content as string (for text files)
export function getFileContent(path: string): string | undefined {
const decodedFile = getDecodedFile(path);
if (!decodedFile) return undefined;
return typeof decodedFile.content === 'string'
? decodedFile.content
: decodedFile.content.toString('utf-8');
}
// Get file content as Buffer (for binary files)
export function getFileBuffer(path: string): Buffer | undefined {
const file = getFile(path);
if (!file) return undefined;
return Buffer.from(file.content, 'base64');
}
export function getFilesByType(type: string): ICompiledFile[] {
return publicFiles.filter(file => file.type.startsWith(type));
}
export function getAllFiles(): ICompiledFile[] {
return publicFiles;
}
export function getAllDecodedFiles(): IDecodedFile[] {
return publicFiles.map(file => {
const decoded = getDecodedFile(file.path);
return decoded!;
});
}
`;
// -------- Main Function -------- //
const panelPath = path.join(__dirname, "panel");
const publicPath = path.join(__dirname, "public");
const publicFiles: IFile[] = [];
const panelFiles: IFile[] = [];
// Function to read files into specific array
function readFilesIntoArray(dir: string, baseDir: string, targetArray: IFile[]) {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const filePath = path.join(dir, file);
const fileStat = fs.statSync(filePath);
if (fileStat.isDirectory()) {
readFilesIntoArray(filePath, baseDir, targetArray);
} else {
// Get relative path from base folder
const relativePath = path.relative(baseDir, filePath).replace(/\\/g, "/");
// Read file content and convert everything to base64
const contentType = getContentType(filePath);
const buffer = fs.readFileSync(filePath);
const content = buffer.toString("base64");
targetArray.push({
path: `/${relativePath}`,
content: content,
name: file,
type: contentType,
});
console.log(`Added: ${relativePath} (${contentType})`);
}
});
}
// Read all files from panel directory
console.log("Reading files from panel directory...");
readFilesIntoArray(panelPath, panelPath, panelFiles);
// Read all files from public directory
console.log("Reading files from public directory...");
readFilesIntoArray(publicPath, publicPath, publicFiles);
// Save to panel_compiled.ts
const panelCompiledPath = path.join(__dirname, "panel_compiled.ts");
fs.writeFileSync(panelCompiledPath, tsContent(panelFiles), "utf-8");
// Save to public_compiled.ts
const publicCompiledPath = path.join(__dirname, "public_compiled.ts");
fs.writeFileSync(publicCompiledPath, tsContent(publicFiles), "utf-8");
console.log(`\nCompilation complete!`);
console.log(`Files compiled: ${publicFiles.length}`);
console.log(`Output: ${publicCompiledPath}`);
console.log("\nFile list:");
publicFiles.forEach((file) => {
console.log(` - ${file.path} (${file.type})`);
});