-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathbuild.lite.forprod.mjs
More file actions
302 lines (262 loc) · 8.72 KB
/
build.lite.forprod.mjs
File metadata and controls
302 lines (262 loc) · 8.72 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import chalk from 'chalk';
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const BLOG_DIR = path.join(__dirname, 'blog');
const BLOG_LITE_DIR = path.join(__dirname, 'blog-lite');
// Get all blog directories sorted by date (newest first)
const getBlogDirectories = async () => {
try {
const items = await fs.readdir(BLOG_DIR, { withFileTypes: true });
const blogDirs = items
.filter(
(item) =>
item.isDirectory() &&
!item.name.startsWith('.') &&
item.name !== 'authors.yml'
)
.map((item) => item.name)
.filter((name) => /^\d{4}-\d{2}-\d{2}-/.test(name)) // Only date-prefixed directories
.sort((a, b) => b.localeCompare(a)); // Sort newest first
return blogDirs;
} catch (error) {
console.error(chalk.red('Error reading blog directory:'), error.message);
return [];
}
};
// Create blog-lite directory with only recent blog posts
const createBlogLiteDirectory = async (keepCount = 10) => {
console.log(
chalk.blue('🚀 Creating blog-lite directory with recent posts...')
);
try {
const blogDirs = await getBlogDirectories();
if (blogDirs.length === 0) {
console.log(chalk.yellow('📂 No blog posts found'));
return;
}
// Remove existing blog-lite directory
try {
await fs.rm(BLOG_LITE_DIR, { recursive: true, force: true });
} catch (error) {
// Directory might not exist, which is fine
}
// Create fresh blog-lite directory
await fs.mkdir(BLOG_LITE_DIR, { recursive: true });
// Get the most recent blog posts to keep
const blogsToKeep = blogDirs.slice(0, keepCount);
const actualKeepCount = Math.min(keepCount, blogDirs.length);
console.log(
chalk.cyan(
`📦 Copying ${actualKeepCount} most recent blog posts to blog-lite...`
)
);
for (const blogDir of blogsToKeep) {
const sourcePath = path.join(BLOG_DIR, blogDir);
const destPath = path.join(BLOG_LITE_DIR, blogDir);
try {
await fs.cp(sourcePath, destPath, { recursive: true });
console.log(chalk.gray(` Copied: ${blogDir}`));
} catch (error) {
console.error(
chalk.red(` Failed to copy ${blogDir}:`),
error.message
);
}
}
// Copy authors.yml file if it exists
const authorsPath = path.join(BLOG_DIR, 'authors.yml');
const authorsDestPath = path.join(BLOG_LITE_DIR, 'authors.yml');
try {
await fs.access(authorsPath);
await fs.cp(authorsPath, authorsDestPath);
console.log(chalk.gray(' Copied: authors.yml'));
} catch (error) {
if (error.code !== 'ENOENT') {
console.log(
chalk.yellow('⚠️ Could not copy authors.yml:'),
error.message
);
}
}
console.log(
chalk.green(
`✅ Created blog-lite directory with ${actualKeepCount} recent posts`
)
);
console.log(
chalk.blue(
`📊 Lite mode will use ${actualKeepCount} posts instead of ${blogDirs.length} total posts`
)
);
} catch (error) {
console.error(
chalk.red('❌ Error creating blog-lite directory:'),
error.message
);
}
};
// Helper function to check if files in directories are different
const areDirectoriesDifferent = async (dir1, dir2) => {
try {
const files1 = await fs.readdir(dir1, { withFileTypes: true });
const files2 = await fs.readdir(dir2, { withFileTypes: true });
// Compare file lists
const fileNames1 = files1.map((f) => f.name).sort();
const fileNames2 = files2.map((f) => f.name).sort();
if (fileNames1.length !== fileNames2.length) return true;
if (fileNames1.some((name, i) => name !== fileNames2[i])) return true;
// Compare file contents and modification times
for (const file of files1) {
const path1 = path.join(dir1, file.name);
const path2 = path.join(dir2, file.name);
if (file.isFile()) {
const stats1 = await fs.stat(path1);
const stats2 = await fs.stat(path2);
// If modification times differ, check content
if (stats1.mtime > stats2.mtime) {
const content1 = await fs.readFile(path1, 'utf8');
const content2 = await fs.readFile(path2, 'utf8');
if (content1 !== content2) return true;
}
}
}
return false;
} catch (error) {
// If comparison fails, assume different to be safe
return true;
}
};
// Copy changed blogs from blog-lite back to main blog directory
const copyChangedBlogsFromLite = async () => {
console.log(chalk.blue('🔄 Checking for changed blogs in blog-lite...'));
try {
// Check if blog-lite directory exists
try {
await fs.access(BLOG_LITE_DIR);
} catch {
console.log(
chalk.yellow('📁 No blog-lite directory found, nothing to copy.')
);
return;
}
// Get all items in blog-lite directory
const items = await fs.readdir(BLOG_LITE_DIR, { withFileTypes: true });
const blogDirs = items
.filter(
(item) =>
item.isDirectory() &&
!item.name.startsWith('.') &&
item.name !== 'authors.yml'
)
.map((item) => item.name);
if (blogDirs.length === 0) {
console.log(chalk.yellow('📂 No blog posts found in blog-lite'));
return;
}
let copiedCount = 0;
let skippedCount = 0;
for (const blogDir of blogDirs) {
const sourcePath = path.join(BLOG_LITE_DIR, blogDir);
const destPath = path.join(BLOG_DIR, blogDir);
try {
// Check if the blog exists in main directory
const destExists = await fs
.access(destPath)
.then(() => true)
.catch(() => false);
if (destExists) {
// Compare actual file contents to determine if changed
const hasChanges = await areDirectoriesDifferent(
sourcePath,
destPath
);
if (hasChanges) {
await fs.cp(sourcePath, destPath, { recursive: true, force: true });
console.log(chalk.green(` ✅ Updated: ${blogDir}`));
copiedCount++;
} else {
console.log(chalk.gray(` ⏭️ Skipped (unchanged): ${blogDir}`));
skippedCount++;
}
} else {
// New blog post, copy it
await fs.cp(sourcePath, destPath, { recursive: true });
console.log(chalk.cyan(` ➕ Added: ${blogDir}`));
copiedCount++;
}
} catch (error) {
console.error(
chalk.red(` ❌ Failed to copy ${blogDir}:`),
error.message
);
}
}
if (copiedCount > 0) {
console.log(
chalk.green(
`✅ Copied ${copiedCount} changed/new blog posts, skipped ${skippedCount} unchanged`
)
);
} else {
console.log(
chalk.blue(
`✅ No changes detected in blog-lite (${skippedCount} blogs checked)`
)
);
}
} catch (error) {
console.error(
chalk.red('❌ Error copying blogs from blog-lite:'),
error.message
);
}
};
// Clean up blog-lite directory
const cleanupBlogLiteDirectory = async () => {
console.log(chalk.blue('🧹 Cleaning up blog-lite directory...'));
try {
// Check if blog-lite directory exists
try {
await fs.access(BLOG_LITE_DIR);
} catch {
console.log(
chalk.yellow('📁 No blog-lite directory found, nothing to clean up.')
);
return;
}
// Remove the blog-lite directory
await fs.rm(BLOG_LITE_DIR, { recursive: true, force: true });
console.log(chalk.green('✅ Successfully cleaned up blog-lite directory'));
} catch (error) {
console.error(
chalk.red('❌ Error cleaning up blog-lite directory:'),
error.message
);
}
};
// Main function that handles blog management based on mode
export const prepAndMoveFilesFromTempLocationToActual = async (
mode = 'full'
) => {
console.log(chalk.blue(`🚀 Preparing files for ${mode} mode...`));
if (mode === 'lite') {
// Create blog-lite directory with recent posts
await createBlogLiteDirectory(30);
} else if (mode === 'full') {
// Clean up blog-lite directory (full mode uses original blog directory)
await cleanupBlogLiteDirectory();
} else {
console.log(chalk.yellow(`⚠️ Unknown mode: ${mode}, defaulting to full`));
await cleanupBlogLiteDirectory();
}
};
// Export the copy function for use in hotbuild
export { copyChangedBlogsFromLite };
// If called directly from command line
if (import.meta.url === `file://${process.argv[1]}`) {
const mode = process.argv[2] || 'full';
await prepAndMoveFilesFromTempLocationToActual(mode);
}