-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathgulpfile.cjs
More file actions
393 lines (350 loc) · 11.2 KB
/
gulpfile.cjs
File metadata and controls
393 lines (350 loc) · 11.2 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
const gulp = require('gulp');
// const autoprefixer = require('gulp-autoprefixer'); // Will be dynamically imported
const browserSync = require('browser-sync');
const concat = require('gulp-concat');
const cp = require('child_process');
const fs = require('fs');
const git = require('gulp-git');
const { mdToPdf } = require('md-to-pdf');
const path = require('path');
const plumber = require('gulp-plumber');
const sass = require('gulp-sass')(require('sass'));
const sourcemaps = require('gulp-sourcemaps');
// const zip = require('gulp-zip'); // Will be dynamically imported
async function grabEvents () {
// First try to fetch from osmcal.org
try {
console.log('Fetching events from osmcal.org...');
const fetchEvents = require('./fetch-events.cjs');
await fetchEvents();
console.log('Successfully fetched events from osmcal.org');
return;
} catch (error) {
console.error('Error fetching events:', error);
// Create empty events file on error
const eventsFile = path.join(__dirname, 'app', 'assets', 'data', 'events.json');
const emptyEvents = {
events: [],
build_time: new Date().toISOString(),
total_events: 0
};
fs.writeFileSync(eventsFile, JSON.stringify(emptyEvents, null, 2));
console.log('Created empty events file due to error');
}
}
async function clean () {
const { deleteAsync } = await import('del');
return deleteAsync(['_site', '.tmp', 'app/_data/events', 'app/_posts']);
}
exports.clean = clean;
function copyAssets () {
console.log('🔍 copyAssets: Starting asset copy process');
// Check source directory
if (!fs.existsSync('.tmp/assets')) {
console.error('❌ Source directory .tmp/assets does not exist');
return Promise.resolve();
}
// List source files
try {
const sourceFiles = fs.readdirSync('.tmp/assets', { recursive: true });
console.log(`📁 Source files found: ${sourceFiles.length}`);
sourceFiles.forEach(file => console.log(` - ${file}`));
} catch (err) {
console.error('❌ Error reading source directory:', err.message);
}
// Ensure destination directory exists
if (!fs.existsSync('_site')) {
console.log('📁 Creating _site directory');
fs.mkdirSync('_site', { recursive: true });
}
if (!fs.existsSync('_site/assets')) {
console.log('📁 Creating _site/assets directory');
fs.mkdirSync('_site/assets', { recursive: true });
}
let fileCount = 0;
return gulp.src('.tmp/assets/**', { allowEmpty: true })
.pipe(plumber({
errorHandler: function(err) {
console.error('❌ Error in copyAssets pipe:', err.message);
console.error('Stack:', err.stack);
this.emit('end');
}
}))
.on('data', function(file) {
fileCount++;
console.log(`📄 Copying file ${fileCount}: ${file.relative}`);
})
.pipe(gulp.dest('_site/assets'))
.on('end', function() {
console.log(`✅ copyAssets: Completed copying ${fileCount} files`);
// Verify destination files
try {
if (fs.existsSync('_site/assets')) {
const destFiles = fs.readdirSync('_site/assets', { recursive: true });
console.log(`📁 Destination files created: ${destFiles.length}`);
destFiles.forEach(file => console.log(` ✓ ${file}`));
} else {
console.error('❌ Destination directory _site/assets was not created');
}
} catch (err) {
console.error('❌ Error verifying destination:', err.message);
}
});
}
exports.copyAssets = copyAssets;
async function styles () {
console.log('🎨 styles: Starting CSS compilation');
const autoprefixer = (await import('gulp-autoprefixer')).default;
const sassInput = 'app/assets/styles/main.scss'; // Only compile main entry point
const sassOptions = {
includePaths: [
'app/assets/styles',
'node_modules/foundation-sites/scss',
'node_modules/@fortawesome/fontawesome-free/scss',
'.tmp/assets/styles'
],
errLogToConsole: true,
outputStyle: 'expanded',
quietDeps: true, // Suppress deprecation warnings from dependencies like Foundation Sites
verbose: false, // Reduce verbose output
silenceDeprecations: ['legacy-js-api', 'import', 'global-builtin', 'color-functions', 'slash-div']
};
let stream = gulp.src(sassInput)
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(autoprefixer({ cascade: false }))
.pipe(sourcemaps.write('.'));
// Only call browserSync.reload if browserSync is active and properly configured
try {
if (browserSync.active) {
stream = stream.pipe(browserSync.reload({stream:true}));
}
} catch (error) {
console.log('BrowserSync stream error (non-fatal):', error.message);
}
return stream
.pipe(gulp.dest('.tmp/assets/styles'))
.on('end', function() {
console.log('✅ styles: CSS compilation completed and written to .tmp/assets/styles');
});
}
exports.styles = styles;
function icons () {
return gulp.src('node_modules/@fortawesome/fontawesome-free/webfonts/**.*')
.pipe(gulp.dest('.tmp/assets/fonts'));
}
exports.icons = icons;
function javascripts () {
const javascriptPaths = [
// the order of these matter
'app/assets/scripts/*.js',
'node_modules/@fortawesome/fontawesome-free/js/all.min.js'
];
/* https://github.com/Foundation-for-Jekyll-sites/jekyll-foundation/blob/master/gulp/tasks/javascript.js */
return gulp.src(javascriptPaths)
.pipe(concat('main.min.js'))
.pipe(gulp.dest('.tmp/assets/scripts'));
}
exports.javascripts = javascripts;
async function zipMaterials () {
const zip = (await import('gulp-zip')).default;
return gulp.src('app/assets/downloads/mapathon-materials/**', { base : 'app/assets/downloads/' })
.pipe(zip('mapathon-materials.zip'))
.pipe(gulp.dest('.tmp/assets/downloads'));
}
exports.zipMaterials = zipMaterials;
async function AdvJosmPdfEN () {
try {
await mdToPdf({
path: 'app/assets/sources/JOSM_Advanced_Mapping_EN.md'
}, {
dest: '.tmp/assets/downloads/JOSM_Advanced_Mapping_EN.pdf',
stylesheet: 'app/assets/styles/github-markdown.css',
pdf_options: {
format: 'Letter'
}
});
console.log('Done converting JOSM_Advanced_Mapping_EN.md to PDF.');
} catch (error) {
console.error('Error converting PDF:', error);
throw error;
}
}
exports.AdvJosmPdfEN = AdvJosmPdfEN;
async function AdvJosmPdfFR () {
try {
await mdToPdf({
path: 'app/assets/sources/JOSM_Advanced_Mapping_FR.md'
}, {
dest: '.tmp/assets/downloads/JOSM_Advanced_Mapping_FR.pdf',
stylesheet: 'app/assets/styles/github-markdown.css',
pdf_options: {
format: 'Letter'
}
});
console.log('Done converting JOSM_Advanced_Mapping_FR.md to PDF.');
} catch (error) {
console.error('Error converting PDF:', error);
throw error;
}
}
exports.AdvJosmPdfFR = AdvJosmPdfFR;
async function AdvJosmPdfES () {
console.log('Skipping PDF generation for JOSM_Advanced_Mapping_ES.md');
}
exports.AdvJosmPdfES = AdvJosmPdfES;
async function validationPdfEN () {
console.log('Skipping PDF generation for Validating_Data_EN.md');
}
exports.validationPdfEN = validationPdfEN;
async function validationPdfFR () {
console.log('Skipping PDF generation for Validating_Data_FR.md');
}
exports.validationPdfFR = validationPdfFR;
function cloneBlog (cb) {
git.clone('https://github.com/MissingMaps/blog', {args: './app/_posts'}, (err) => {
if (err) {console.log('Blog clone error (non-fatal):', err.message);}
cb(); // Continue even if clone fails
});
}
exports.cloneBlog = cloneBlog;
/* Build the jekyll website. */
function jekyll (done) {
const args = ['exec', 'jekyll', 'build'];
switch (environment) {
case 'development':
args.push('--config=_config.yml,_config-dev.yml');
args.push('--trace');
break;
case 'production':
args.push('--config=_config.yml');
break;
}
return cp.spawn('bundle', args, {stdio: 'inherit'})
.on('close', done);
}
exports.jekyll = jekyll;
/* different build options */
/* ======================= */
function watching () {
function browserReload (cb) {
// Add error handling for browserSync reload
try {
if (browserSync.active) {
browserSync.reload();
}
} catch (error) {
console.log('BrowserSync reload error (non-fatal):', error.message);
}
cb();
}
// Improved BrowserSync configuration with stability options
browserSync({
server: {
baseDir: '_site',
serveStaticOptions: {
extensions: ['html']
}
},
port: 3000,
ui: {
port: 3001
},
open: false,
notify: false,
ghostMode: false,
reloadOnRestart: true,
injectChanges: false,
logLevel: 'info',
// Add stability options
watchOptions: {
ignoreInitial: true,
ignored: [
'node_modules/**',
'.git/**',
'.tmp/**',
'**/*.log'
]
},
// Prevent crashes on file errors
reloadDelay: 100,
reloadDebounce: 500
});
// Watch with debouncing to prevent overwhelming browserSync
gulp.watch(['./app/**/*', './_config*'], {
ignoreInitial: true,
delay: 500
}, gulp.series(
jekyll,
buildAssets,
copyAssets,
browserReload));
}
// Create a task that builds all assets and ensures they're ready before copying
const buildAssets = gulp.parallel(javascripts, styles, icons, zipMaterials);
exports.buildAssets = buildAssets;
exports.serve = gulp.series(
clean,
gulp.parallel(cloneBlog, grabEvents),
jekyll,
buildAssets,
copyAssets,
watching);
// Alternative serve command without BrowserSync (more stable)
function simpleServe () {
const http = require('http');
const fs = require('fs');
const path = require('path');
const server = http.createServer((req, res) => {
let filePath = path.join(__dirname, '_site', req.url === '/' ? 'index.html' : req.url);
// If no extension, try adding .html
if (!path.extname(filePath)) {
filePath += '.html';
}
fs.readFile(filePath, (err, content) => {
if (err) {
if (err.code === 'ENOENT') {
res.writeHead(404);
res.end('File not found');
} else {
res.writeHead(500);
res.end('Server error');
}
} else {
const ext = path.extname(filePath);
const contentType = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml'
}[ext] || 'text/plain';
res.writeHead(200, { 'Content-Type': contentType });
res.end(content);
}
});
});
server.listen(3000, () => {
console.log('Simple server running at http://localhost:3000');
});
}
exports.serve_stable = gulp.series(
clean,
gulp.parallel(cloneBlog, grabEvents),
jekyll,
buildAssets,
copyAssets,
simpleServe);
let environment = 'development';
function setProd (cb) { environment = 'production'; cb(); }
exports.prod = gulp.series(
clean,
gulp.parallel(cloneBlog, grabEvents),
setProd,
jekyll,
buildAssets,
copyAssets);