|
| 1 | +const fs = require('fs'); |
| 2 | +const axios = require('axios'); |
| 3 | + |
| 4 | +const eventsTimeline = {}; |
| 5 | + |
| 6 | +function getYearFromFile(fileName) { |
| 7 | + return fileName.replace('.json', ''); |
| 8 | +} |
| 9 | + |
| 10 | +function getGetOrdinal(n) { |
| 11 | + const s = ['th', 'st', 'nd', 'rd'], |
| 12 | + v = n % 100; |
| 13 | + |
| 14 | + return `${n}${(s[(v-20)%10]||s[v]||s[0])}`; |
| 15 | +} |
| 16 | + |
| 17 | +function addEvent(event) { |
| 18 | + const date = new Date(event.startDate); |
| 19 | + |
| 20 | + const year = date.getFullYear(); |
| 21 | + const locale = 'en-us'; |
| 22 | + const month = date.toLocaleString(locale, { month: 'long' }); |
| 23 | + |
| 24 | + if (!eventsTimeline[year][month]) { |
| 25 | + eventsTimeline[year][month] = []; |
| 26 | + } |
| 27 | + |
| 28 | + eventsTimeline[year][month].push(event); |
| 29 | +} |
| 30 | + |
| 31 | +function removeVueVixensEvents(staticEventsData) { |
| 32 | + for (const fileName of staticEventsData) { |
| 33 | + const year = getYearFromFile(fileName); |
| 34 | + |
| 35 | + Object.keys(eventsTimeline[year]).forEach((month) => { |
| 36 | + const vvEvents = eventsTimeline[year][month] |
| 37 | + .filter((event) => event.tag && event.tag === 'vuevixens'); |
| 38 | + if (vvEvents) { |
| 39 | + vvEvents.forEach((event) => { |
| 40 | + const existingIndex = eventsTimeline[year][month].findIndex((item) => item.id === event.id); |
| 41 | + eventsTimeline[year][month].splice(existingIndex, 1); |
| 42 | + }); |
| 43 | + } |
| 44 | + }); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +async function getVueVixensEvents() { |
| 49 | + let response = await axios.get(`https://api.storyblok.com/v1/cdn/stories/upcoming?version=published&cv=1541163074263&token=${process.env.VV_TOKEN}`); |
| 50 | + |
| 51 | + return response.data.story.content.body.map((event) => ({ |
| 52 | + id: `vm-${event._uid}`, |
| 53 | + date: getGetOrdinal(new Date(event.date).getDate()), |
| 54 | + startDate: event.date, |
| 55 | + endDate: event.date, |
| 56 | + organiser: 'Vue Vixens', |
| 57 | + organiserLink: 'https://vuevixens.org', |
| 58 | + name: event.name, |
| 59 | + eventLink: `https://vuevixens.org/${event.link.cached_url}`, |
| 60 | + type: 'workshop', |
| 61 | + tag: 'vuevixens', |
| 62 | + location: event.location, |
| 63 | + })); |
| 64 | +} |
| 65 | + |
| 66 | +function getEvents(asyncEvents) { |
| 67 | + asyncEvents.forEach((event) => { |
| 68 | + addEvent(event) |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +function readEventsFromFile(staticEventsData) { |
| 73 | + for (const fileName of staticEventsData) { |
| 74 | + const year = getYearFromFile(fileName); |
| 75 | + const fileNamePath = `./docs/.vuepress/data/${fileName}`; |
| 76 | + const data = fs.readFileSync(fileNamePath); |
| 77 | + eventsTimeline[year] = JSON.parse(data); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function writeEventsToFile(staticEventsData) { |
| 82 | + for (const fileName of staticEventsData) { |
| 83 | + const year = getYearFromFile(fileName); |
| 84 | + const fileNamePath = `./docs/.vuepress/data/${fileName}`; |
| 85 | + |
| 86 | + fs.writeFile(fileNamePath, JSON.stringify(eventsTimeline[year], null, 2), () => {}); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +async function main() { |
| 91 | + const staticEventsData = fs.readdirSync('./docs/.vuepress/data/').filter((fileName) => /.+\.json$/.test(fileName)); |
| 92 | + |
| 93 | + console.log('Generating events...') |
| 94 | + let asyncEvents = await getVueVixensEvents(); |
| 95 | + |
| 96 | + readEventsFromFile(staticEventsData); |
| 97 | + removeVueVixensEvents(staticEventsData); |
| 98 | + getEvents(asyncEvents); |
| 99 | + writeEventsToFile(staticEventsData); |
| 100 | + console.log('All events saved to JSON'); |
| 101 | +} |
| 102 | + |
| 103 | +main(); |
0 commit comments