-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
178 lines (144 loc) · 5.35 KB
/
build.js
File metadata and controls
178 lines (144 loc) · 5.35 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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
/**
* Build script to generate HTML page with embedded image list
*/
function generateImageList() {
const imagesDir = path.join(__dirname, 'images');
const htmlTemplate = path.join(__dirname, 'index.html');
const outputFile = path.join(__dirname, 'test.html');
console.log('🔍 Scanning images directory...');
if (!fs.existsSync(imagesDir)) {
console.error('❌ Images directory not found!');
process.exit(1);
}
// Get all image files
const imageFiles = fs.readdirSync(imagesDir)
.filter(file => /\.(jpg|jpeg|png|gif|webp)$/i.test(file))
.sort();
console.log(`📸 Found ${imageFiles.length} images`);
if (imageFiles.length === 0) {
console.error('❌ No image files found in images directory!');
process.exit(1);
}
// Generate image list with relative paths
const imageList = imageFiles.map((file, index) => ({
id: `images/${file}`,
src: `images/${file}`,
name: file,
index: index
}));
console.log('📝 Reading HTML template...');
if (!fs.existsSync(htmlTemplate)) {
console.error('❌ HTML template (index.html) not found!');
process.exit(1);
}
let htmlContent = fs.readFileSync(htmlTemplate, 'utf8');
// Replace the placeholder with actual image data
const imageListJs = `const GENERATED_IMAGE_LIST = ${JSON.stringify(imageList, null, 4)};`;
htmlContent = htmlContent.replace('const GENERATED_IMAGE_LIST = [];', imageListJs);
// Add build timestamp
const buildInfo = `
<!-- Generated on ${new Date().toISOString()} -->
<!-- Images found: ${imageFiles.length} -->
`;
htmlContent = htmlContent.replace('</head>', ` ${buildInfo}</head>`);
console.log('💾 Writing output file...');
fs.writeFileSync(outputFile, htmlContent);
console.log('✅ Build completed successfully!');
console.log(`📄 Output: ${outputFile}`);
console.log(`🖼️ Images included: ${imageFiles.length}`);
console.log('');
console.log('🚀 To test:');
console.log(` Open: file://${outputFile}`);
console.log(' Or run a local server in this directory');
return {
outputFile,
imageCount: imageFiles.length,
images: imageList
};
}
// Also create a simple HTTP server for testing
function createTestServer() {
const http = require('http');
const url = require('url');
const mime = {
'.html': 'text/html',
'.js': 'application/javascript',
'.css': 'text/css',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.png': 'image/png',
'.gif': 'image/gif',
'.webp': 'image/webp'
};
const server = http.createServer((req, res) => {
const pathname = url.parse(req.url).pathname;
let filePath = pathname === '/' ? '/test.html' : pathname;
filePath = path.join(__dirname, filePath);
// Security check
if (!filePath.startsWith(__dirname)) {
res.writeHead(403);
res.end('Forbidden');
return;
}
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404);
res.end('Not Found');
return;
}
const ext = path.extname(filePath);
const contentType = mime[ext] || 'application/octet-stream';
res.writeHead(200, { 'Content-Type': contentType });
res.end(data);
});
});
return server;
}
// Command line interface
if (require.main === module) {
const args = process.argv.slice(2);
if (args.includes('--help') || args.includes('-h')) {
console.log(`
📸 Image Similarity Test Builder
Usage:
node build.js [options]
Options:
--serve, -s Start a local test server after building
--port, -p Server port (default: 8080)
--help, -h Show this help
Examples:
node build.js # Build test.html
node build.js --serve # Build and serve on http://localhost:8080
node build.js -s -p 3000 # Build and serve on port 3000
`);
process.exit(0);
}
try {
const result = generateImageList();
if (args.includes('--serve') || args.includes('-s')) {
const portIndex = Math.max(args.indexOf('--port'), args.indexOf('-p'));
const port = portIndex >= 0 && args[portIndex + 1] ? parseInt(args[portIndex + 1]) : 8080;
const server = createTestServer();
server.listen(port, () => {
console.log('');
console.log('🌐 Test server started!');
console.log(`📍 Open: http://localhost:${port}`);
console.log(' Press Ctrl+C to stop');
});
process.on('SIGINT', () => {
console.log('\n👋 Stopping server...');
server.close(() => {
console.log('✅ Server stopped');
process.exit(0);
});
});
}
} catch (error) {
console.error('❌ Build failed:', error.message);
process.exit(1);
}
}
module.exports = { generateImageList, createTestServer };