|
1 | 1 | import { mkdir, readdir, readFile, rm, writeFile } from 'node:fs/promises'; |
2 | 2 | import path from 'node:path'; |
3 | 3 |
|
4 | | -const GROUPS_FILE = path.join('data', 'meetup_groups.json'); |
| 4 | +const USER_GROUPS_DIR = path.join('content', 'user-groups'); |
5 | 5 | const CALENDAR_DIR = path.join('content', 'calendar'); |
6 | 6 | const DRY_RUN = process.argv.includes('--dry-run'); |
7 | 7 |
|
@@ -105,11 +105,23 @@ function eventFile(event, metadata, groupName) { |
105 | 105 | } |
106 | 106 |
|
107 | 107 | async function groups() { |
108 | | - const parsed = JSON.parse(await readFile(GROUPS_FILE, 'utf8')); |
109 | | - if (!Array.isArray(parsed) || !parsed.every((group) => group && typeof group === 'object' && typeof group.urlname === 'string' && group.urlname)) { |
110 | | - throw new Error(`${GROUPS_FILE} must be an array of Meetup group objects with a urlname.`); |
111 | | - } |
112 | | - return parsed; |
| 108 | + const files = await readdir(USER_GROUPS_DIR, { withFileTypes: true }); |
| 109 | + const urlnames = new Set(); |
| 110 | + |
| 111 | + await Promise.all(files |
| 112 | + .filter((file) => file.isFile() && file.name !== '_index.md' && file.name.endsWith('.md')) |
| 113 | + .map(async (file) => { |
| 114 | + const content = await readFile(path.join(USER_GROUPS_DIR, file.name), 'utf8'); |
| 115 | + const frontMatter = content.match(/^---\r?\n([\s\S]*?)\r?\n---/); |
| 116 | + if (!frontMatter) return; |
| 117 | + |
| 118 | + for (const [, url] of frontMatter[1].matchAll(/^\s*url:\s*["']?(https?:\/\/[^\s"']+)["']?\s*$/gmi)) { |
| 119 | + const match = url.match(/^https:\/\/(?:www\.)?meetup\.com\/([^\/?#]+)(?:[\/?#]|$)/i); |
| 120 | + if (match) urlnames.add(match[1]); |
| 121 | + } |
| 122 | + })); |
| 123 | + |
| 124 | + return [...urlnames]; |
113 | 125 | } |
114 | 126 |
|
115 | 127 | async function fetchGroupEvents(urlname) { |
@@ -141,7 +153,10 @@ async function calendarFiles() { |
141 | 153 |
|
142 | 154 | async function main() { |
143 | 155 | const configuredGroups = await groups(); |
144 | | - const groupEvents = await Promise.all(configuredGroups.map(({ urlname }) => fetchGroupEvents(urlname))); |
| 156 | + if (configuredGroups.length === 0) { |
| 157 | + throw new Error(`No Meetup group links found in ${USER_GROUPS_DIR}.`); |
| 158 | + } |
| 159 | + const groupEvents = await Promise.all(configuredGroups.map(fetchGroupEvents)); |
145 | 160 | await mkdir(CALENDAR_DIR, { recursive: true }); |
146 | 161 |
|
147 | 162 | const calendar = await calendarFiles(); |
|
0 commit comments