-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathreadProjectTexts.ts
More file actions
31 lines (25 loc) · 1.54 KB
/
readProjectTexts.ts
File metadata and controls
31 lines (25 loc) · 1.54 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
import type { IpcMainEvent } from 'electron';
import log from 'electron-log';
import path from 'path';
import { ProjectText } from '@src/GlobalStateProvider';
import { checkTextFileReserved, getTextFileList, getTextPath, loadCSV } from '@utils/textManagement';
import { defineBackendServiceFunction } from './defineBackendServiceFunction';
import { ChannelNames, sendProgress } from '@utils/BackendTask';
export type ReadProjectTextInput = { path: string };
const readProjectTexts = async (payload: ReadProjectTextInput, event: IpcMainEvent, channels: ChannelNames) => {
log.info('read-project-texts');
const textFileList = getTextFileList(payload.path, true);
const reservedFileUsed = await checkTextFileReserved(textFileList, payload.path);
if (reservedFileUsed) {
throw 'A reserved text file has been found in Data/Text/Dialogs. The range 200.000 to 299.999 is reserved for the application and must not be used.';
}
const projectTexts = await textFileList.reduce(async (prev, curr, index) => {
const previousResult = await prev;
sendProgress(event, channels, { step: index + 1, total: textFileList.length, stepText: `${curr}.csv` });
const currentText = await loadCSV(path.join(payload.path, getTextPath(curr), `${curr}.csv`));
return { ...previousResult, [curr]: currentText };
}, Promise.resolve({} as ProjectText));
log.info('read-project-texts/success');
return projectTexts as unknown as Record<string, string>;
};
export const registerReadProjectTexts = defineBackendServiceFunction('read-project-texts', readProjectTexts);