-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprebuild.mjs
More file actions
168 lines (140 loc) · 4.54 KB
/
prebuild.mjs
File metadata and controls
168 lines (140 loc) · 4.54 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
import https from 'https';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const CONFIG = {
schemaUrl: 'https://raw.githubusercontent.com/gfargo/coco/main/schema.json',
retryAttempts: 3,
retryDelay: 1000, // ms
timeout: 10000, // ms
};
// Convert URL to file path
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
function logSuccess(message) {
console.log('\x1b[32m✓\x1b[0m', message);
}
function logError(message) {
console.error('\x1b[31m❌\x1b[0m', message);
}
function logInfo(message) {
console.log('\x1b[36mℹ\x1b[0m', message);
}
function ensureDirectoryExists(filePath) {
const directory = path.dirname(filePath);
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory, { recursive: true });
logInfo(`Created directory: ${directory}`);
}
}
function validateSchema(data) {
try {
const schema = JSON.parse(data);
if (!schema || typeof schema !== 'object') {
throw new Error('Invalid schema format');
}
return true;
} catch (error) {
throw new Error(`Schema validation failed: ${error.message}`);
}
}
async function downloadSchema(url, outputPath, attempt = 1) {
logInfo(`Downloading schema from: ${url}`);
return /** @type {Promise<void>} */(new Promise((resolve, reject) => {
const tempPath = `${outputPath}.temp`;
const fileStream = fs.createWriteStream(tempPath);
let data = '';
const request = https.get(url, { timeout: CONFIG.timeout }, (response) => {
const { statusCode, headers } = response;
if (statusCode !== 200) {
fileStream.close();
fs.unlinkSync(tempPath);
if (statusCode === 404) {
reject(new Error('Schema not found (404)'));
} else if (statusCode >= 500 && attempt < CONFIG.retryAttempts) {
logInfo(`Received ${statusCode}, retrying (${attempt}/${CONFIG.retryAttempts})...`);
setTimeout(() => {
downloadSchema(url, outputPath, attempt + 1)
.then(resolve)
.catch(reject);
}, CONFIG.retryDelay);
return;
} else {
reject(new Error(`HTTP Error: ${statusCode}`));
}
return;
}
logInfo(`Content length: ${headers['content-length'] || 'unknown'} bytes`);
response.on('data', (chunk) => {
data += chunk;
});
response.pipe(fileStream);
fileStream.on('finish', () => {
fileStream.close();
try {
validateSchema(data);
fs.renameSync(tempPath, outputPath);
logSuccess(`Schema downloaded successfully to: ${outputPath}`);
resolve();
} catch (error) {
fs.unlinkSync(tempPath);
reject(error);
}
});
});
request.on('timeout', () => {
request.destroy();
fileStream.close();
fs.unlinkSync(tempPath);
if (attempt < CONFIG.retryAttempts) {
logInfo(`Request timed out, retrying (${attempt}/${CONFIG.retryAttempts})...`);
setTimeout(() => {
downloadSchema(url, outputPath, attempt + 1)
.then(resolve)
.catch(reject);
}, CONFIG.retryDelay);
} else {
reject(new Error('Request timed out'));
}
});
request.on('error', (error) => {
fileStream.close();
fs.unlinkSync(tempPath);
if (attempt < CONFIG.retryAttempts) {
logInfo(`Network error, retrying (${attempt}/${CONFIG.retryAttempts})...`);
setTimeout(() => {
downloadSchema(url, outputPath, attempt + 1)
.then(resolve)
.catch(reject);
}, CONFIG.retryDelay);
} else {
reject(error);
}
});
fileStream.on('error', (error) => {
fileStream.close();
fs.unlinkSync(tempPath);
reject(new Error(`File write error: ${error.message}`));
});
}));
}
async function main() {
const outputPath = path.join(__dirname, 'public', 'schema.json');
try {
console.log('\n\x1b[1m📥 Schema Downloader\x1b[0m\n');
ensureDirectoryExists(outputPath);
await downloadSchema(CONFIG.schemaUrl, outputPath);
console.log('\n\x1b[32m✨ Prebuild completed successfully!\x1b[0m\n');
} catch (error) {
logError(`Failed to download schema: ${error.message}`);
if (error.stack) {
console.error('\x1b[90m' + error.stack.split('\n').slice(1).join('\n') + '\x1b[0m');
}
process.exit(1);
}
}
// Execute the script
main().catch((error) => {
logError('Unexpected error:', error);
process.exit(1);
});