Skip to content

Commit

Permalink
refactor: split existsTempFile
Browse files Browse the repository at this point in the history
  • Loading branch information
hal-shu-sato committed Feb 11, 2024
1 parent acc9ba8 commit 6cc241d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/lib/convertId.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs-extra';
import path from 'path';
import * as apmJson from './apmJson';
import { download, existsTempFile } from './ipcWrapper';
import { download, existsFile, getTempFilePath } from './ipcWrapper';
import * as modList from './modList';

/**
Expand All @@ -20,12 +20,12 @@ export async function getIdDict(
});
return convertJson ? await fs.readJson(convertJson) : {};
} else {
const convertJson = await existsTempFile(
const convertJsonPath = await getTempFilePath(
path.join('package', path.basename(dictUrl)),
dictUrl,
);
if (convertJson.exists) {
return await fs.readJson(convertJson.path);
if (existsFile(convertJsonPath)) {
return await fs.readJson(convertJsonPath);
} else {
return {};
}
Expand Down
20 changes: 15 additions & 5 deletions src/lib/ipcWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,29 @@ export async function openPath(relativePath: string) {
}

/**
* Returns whether the temporary file exists and the path.
* Returns the temporary file path.
* @param {string} relativePath - A relative path from the data directory.
* @param {string} [keyText] - String used to generate the hash.
* @returns {Promise<string>} The temporary file path.
*/
export async function existsTempFile(
export async function getTempFilePath(
relativePath: string,
keyText: string = undefined,
) {
return (await ipcRenderer.invoke(
'exists-temp-file',
return ipcRenderer.invoke(
'get-temp-file-path',
relativePath,
keyText,
)) as { exists: boolean; path: string };
) as Promise<string>;
}

/**
* Returns whether the file exists.
* @param {string} filePath - A file path
* @returns {Promise<boolean>} Whether the file exists
*/
export async function existsFile(filePath: string) {
return ipcRenderer.invoke('exists-file', filePath) as Promise<boolean>;
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/lib/modList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'fs-extra';
import * as os from 'os';
import path from 'path';
import { isParent } from './apmPath';
import { download, existsTempFile } from './ipcWrapper';
import { download, existsFile, getTempFilePath } from './ipcWrapper';
import * as parseJson from './parseJson';
const store = new Store();

Expand Down Expand Up @@ -68,14 +68,14 @@ export async function updateInfo() {
* @returns {Promise<object>} - An object parsed from list.json.
*/
export async function getInfo() {
const modFile = await existsTempFile('list.json');
if (modFile.exists) {
return await parseJson.getMod(modFile.path).catch((): null => null);
const modFilePath = await getTempFilePath('list.json');
if (existsFile(modFilePath)) {
return await parseJson.getMod(modFilePath).catch((): null => null);
} else {
await updateInfo();
const downloadedModFile = await existsTempFile('list.json');
const downloadedModFilePath = await getTempFilePath('list.json');
return await parseJson
.getMod(downloadedModFile.path)
.getMod(downloadedModFilePath)
.catch((): null => null);
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import log from 'electron-log';
import prompt from 'electron-prompt';
import Store from 'electron-store';
import windowStateKeeper from 'electron-window-state';
import fs, { mkdir, readJsonSync } from 'fs-extra';
import fs, { exists, mkdir, readJsonSync } from 'fs-extra';
import path from 'path';
import 'source-map-support/register';
import { updateElectronApp } from 'update-electron-app';
Expand Down Expand Up @@ -205,17 +205,19 @@ ipcMain.handle('open-path', (event, relativePath) => {
return folderExists;
});

ipcMain.handle('exists-temp-file', (event, relativePath, keyText) => {
let filePath = path.join(app.getPath('userData'), 'Data/', relativePath);
ipcMain.handle('get-temp-file-path', (event, relativePath, keyText) => {
const filePath = path.join(app.getPath('userData'), 'Data/', relativePath);
if (keyText) {
filePath = path.join(
return path.join(
path.dirname(filePath),
getHash(keyText) + '_' + path.basename(filePath),
);
}
return { exists: fs.existsSync(filePath), path: filePath };
return filePath;
});

ipcMain.handle('exists-file', async (event, filePath) => exists(filePath));

ipcMain.handle('open-dir-dialog', async (event, title, defaultPath) => {
const win = BrowserWindow.getFocusedWindow();
const dir = await dialog.showOpenDialog(win, {
Expand Down
9 changes: 5 additions & 4 deletions src/renderer/main/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { checkIntegrity, verifyFile } from '../../lib/integrity';
import {
app,
download,
existsTempFile,
existsFile,
getTempFilePath,
openDialog,
openDirDialog,
openYesNoDialog,
Expand Down Expand Up @@ -133,13 +134,13 @@ async function displayInstalledVersion(instPath: string) {
* @returns {Promise<Core>} - An object parsed from core.json.
*/
async function getCoreInfo() {
const coreFile = await existsTempFile(
const coreFilePath = await getTempFilePath(
path.join('core', path.basename(await modList.getCoreDataUrl())),
);
if (!coreFile.exists) return null;
if (!existsFile(coreFilePath)) return null;

try {
return await parseJson.getCore(coreFile.path);
return await parseJson.getCore(coreFilePath);
} catch (e) {
log.error(e);
return null;
Expand Down
13 changes: 9 additions & 4 deletions src/renderer/main/packageUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import log from 'electron-log';
import * as fs from 'fs-extra';
import path from 'path';
import * as apmJson from '../../lib/apmJson';
import { download, existsTempFile, openDialog } from '../../lib/ipcWrapper';
import {
download,
existsFile,
getTempFilePath,
openDialog,
} from '../../lib/ipcWrapper';
import * as parseJson from '../../lib/parseJson';
import { ApmJsonObject } from '../../types/apmJson';
import { PackageItem } from '../../types/packageItem';
Expand Down Expand Up @@ -107,13 +112,13 @@ async function getPackages(packageDataUrls: string[]) {
const jsonList = [];

for (const packageRepository of packageDataUrls) {
const packagesListFile = await existsTempFile(
const packagesListFilePath = await getTempFilePath(
`package/${path.basename(packageRepository)}`,
packageRepository,
);
if (packagesListFile.exists) {
if (existsFile(packagesListFilePath)) {
try {
jsonList.push(await parseJson.getPackages(packagesListFile.path));
jsonList.push(await parseJson.getPackages(packagesListFilePath));
} catch {
log.error('Failed data processing.');
await openDialog(
Expand Down

0 comments on commit 6cc241d

Please sign in to comment.