Skip to content

Commit 6f98bb7

Browse files
committed
refactor: move folder handling to their respective modules
1 parent 153770e commit 6f98bb7

File tree

5 files changed

+27
-47
lines changed

5 files changed

+27
-47
lines changed

source/lib/all-events.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {readFile} from 'node:fs/promises';
2-
import {EVENT_FILES_DIR, pullEventFiles} from './git.ts';
2+
import {pull} from './git.ts';
33
import {typedEntries} from './javascript-helper.ts';
4-
import type {EventDirectory, EventId} from './types.ts';
4+
import type {EventDirectory, EventEntry, EventId} from './types.ts';
5+
6+
const DIRECTORY = 'eventfiles';
57

68
let directory: EventDirectory = {};
79
let namesOfEvents: Readonly<Record<EventId, string>> = {};
@@ -11,11 +13,8 @@ await update();
1113
console.log(new Date(), 'eventfiles loaded');
1214

1315
async function update() {
14-
await pullEventFiles();
15-
const directoryString = await readFile(
16-
`${EVENT_FILES_DIR}/directory.json`,
17-
'utf8',
18-
);
16+
await pull(DIRECTORY, 'https://github.com/HAWHHCalendarBot/eventfiles.git');
17+
const directoryString = await readFile(`${DIRECTORY}/directory.json`, 'utf8');
1918
directory = JSON.parse(directoryString) as EventDirectory;
2019
namesOfEvents = await generateMapping();
2120
}
@@ -106,3 +105,8 @@ export function find(
106105
events: Object.fromEntries(typedEntries(accumulator).sort((a, b) => a[1].localeCompare(b[1]))),
107106
};
108107
}
108+
109+
export async function loadEvents(eventId: EventId): Promise<EventEntry[]> {
110+
const content = await readFile(`${DIRECTORY}/events/${eventId}.json`, 'utf8');
111+
return JSON.parse(content) as EventEntry[];
112+
}

source/lib/change-helper.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import {readFile} from 'node:fs/promises';
21
import {html as format} from 'telegram-format';
3-
import type {
4-
Change, EventEntry, EventId, NaiveDateTime,
5-
} from './types.ts';
62
import {getEventName} from './all-events.ts';
7-
import {EVENT_FILES_DIR} from './git.ts';
3+
import type {Change, EventId, NaiveDateTime} from './types.ts';
84

95
export function generateChangeDescription(change: Change): string {
106
let text = '';
@@ -68,16 +64,3 @@ export function generateShortChangeText(
6864
): string {
6965
return `${getEventName(eventId)} ${date}`;
7066
}
71-
72-
export async function loadEvents(eventId: EventId): Promise<EventEntry[]> {
73-
try {
74-
const content = await readFile(
75-
`${EVENT_FILES_DIR}/events/${eventId}.json`,
76-
'utf8',
77-
);
78-
return JSON.parse(content) as EventEntry[];
79-
} catch (error) {
80-
console.error('ERROR while loading events for change date picker', error);
81-
return [];
82-
}
83-
}

source/lib/git.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,15 @@ import {exec} from 'node:child_process';
22
import {existsSync} from 'node:fs';
33
import {promisify} from 'node:util';
44

5-
export const EVENT_FILES_DIR = 'eventfiles';
6-
export const MENSA_DIR = 'mensa-data';
7-
85
const run = promisify(exec);
96

10-
async function pull(directory: string, remoteUrl: string): Promise<void> {
7+
export async function pull(
8+
directory: string,
9+
remoteUrl: string,
10+
): Promise<void> {
1111
try {
1212
await (existsSync(`${directory}/.git`)
1313
? run(`git -C ${directory} pull`)
1414
: run(`git clone -q --depth 1 ${remoteUrl} ${directory}`));
1515
} catch {}
1616
}
17-
18-
export async function pullEventFiles(): Promise<void> {
19-
await pull(
20-
EVENT_FILES_DIR,
21-
'https://github.com/HAWHHCalendarBot/eventfiles.git',
22-
);
23-
}
24-
25-
export async function pullMensaData(): Promise<void> {
26-
await pull(MENSA_DIR, 'https://github.com/HAWHHCalendarBot/mensa-data.git');
27-
}

source/lib/mensa-meals.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import {readdir, readFile} from 'node:fs/promises';
2-
import {MENSA_DIR, pullMensaData} from './git.ts';
2+
import {pull} from './git.ts';
33
import type {Meal} from './meal.ts';
44

5+
const DIRECTORY = 'mensa-data';
6+
57
setInterval(async () => pullMensaData(), 1000 * 60 * 30); // Every 30 minutes
68
await pullMensaData();
79
console.log(new Date(), 'mensa-data loaded');
810

11+
async function pullMensaData(): Promise<void> {
12+
await pull(DIRECTORY, 'https://github.com/HAWHHCalendarBot/mensa-data.git');
13+
}
14+
915
export async function getCanteenList(): Promise<string[]> {
10-
const found = await readdir(MENSA_DIR, {withFileTypes: true});
16+
const found = await readdir(DIRECTORY, {withFileTypes: true});
1117
const dirs = found
1218
.filter(o => o.isDirectory())
1319
.map(o => o.name)
@@ -27,7 +33,7 @@ function getFilename(
2733
});
2834
const m = month.toLocaleString(undefined, {minimumIntegerDigits: 2});
2935
const d = day.toLocaleString(undefined, {minimumIntegerDigits: 2});
30-
return `${MENSA_DIR}/${mensa}/${y}/${m}/${d}.json`;
36+
return `${DIRECTORY}/${mensa}/${y}/${m}/${d}.json`;
3137
}
3238

3339
export async function getMealsOfDay(

source/menu/events/changes/add/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ import {
77
MenuTemplate,
88
replyMenuToContext,
99
} from 'grammy-inline-menu';
10-
import {
11-
generateChangeText,
12-
loadEvents,
13-
} from '../../../../lib/change-helper.ts';
10+
import {loadEvents} from '../../../../lib/all-events.ts';
11+
import {generateChangeText} from '../../../../lib/change-helper.ts';
1412
import {typedKeys} from '../../../../lib/javascript-helper.ts';
1513
import type {
1614
EventId,

0 commit comments

Comments
 (0)