-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate-venues.js
87 lines (73 loc) · 2.61 KB
/
generate-venues.js
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
const admin = require('firebase-admin');
const fs = require('fs');
const slugify = require('slugify');
const path = require('path');
require('dotenv').config();
const firebaseConfig = {
// Your Firebase project configuration
};
const serviceAccount = JSON.parse(process.env.FIREBASE_SERVICE_ACCOUNT);
// Initialize Firebase Admin SDK
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
const db = admin.firestore();
async function main() {
try {
// Clean the _venues folder
if (fs.existsSync('_venues')) {
fs.rmdirSync('_venues', { recursive: true });
}
// Create the _venues folder if it doesn't exist
if (!fs.existsSync('_venues')) {
fs.mkdirSync('_venues');
}
// Fetch the venues from Firebase
const venuesSnapshot = await db.collection('venues').get();
// Generate a markdown file for each venue
venuesSnapshot.forEach((doc) => {
const venue = doc.data();
//console.log(venue);
// Create a slug from the venue name
const slug = slugify(venue.venueName, { lower: true });
const proctors = venue.proctors==null ? null : Array.isArray(venue.proctors) ? venue.proctors : [venue.proctors];
var content = venue.__content;
// Check if content exists
if (content) {
// replace the /n with newlines in the content
content = content.replace(/\\n/g, '\n');
} else {
// Set content to an empty string if it doesn't exist
content = '';
}
// Create the frontmatter
const frontmatter = `---
venueName: "${venue.venueName}"
venueId: "${venue.id}"
location:
address: "${venue.location.address}"
city: "${venue.location.city}"
country: "${venue.location.country}"
latitude: ${venue.location.latitude}
longitude: ${venue.location.longitude}
maxParticipants: ${venue.maxParticipants}
primaryContactName: "${venue.primaryContact?.name || venue.primaryContactName}"
primaryUsername: "${venue.primaryContact?.username || venue.primaryUsername}"
secondaryContactName: "${venue.secondaryContact?.name }"
secondaryUsername: "${venue.secondaryContact?.username }"
proctors: ${JSON.stringify(proctors)}
canSignup: false
isShown: ${venue.isShown}
---
${content}
`;
// Output the markdown to the console
// console.log(frontmatter);
// Write the frontmatter to a new markdown file
fs.writeFileSync(path.join('_venues', `${slug}.md`), frontmatter);
});
} catch (error) {
console.error('[Error] Processing venues:', error.message);
}
}
main();