Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #22 ensure data folder exists at startup #27

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions src/server/attachments/deleteAttachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { type Procedure, procedure } from '@server/trpc';
import db from '@server/db';
import { getUserSettings } from '@server/userSettings/store';
import { getAttachmentsPath } from '@server/userSettings/utils';
import { fileExists } from '@server/utils';
import { DeleteAttachmentInput, DeleteAttachmentOutput } from './types';

const deleteAttachment: Procedure<
Expand Down Expand Up @@ -41,12 +42,3 @@ export default procedure
.input(DeleteAttachmentInput)
.output(DeleteAttachmentOutput)
.mutation(deleteAttachment);

async function fileExists(filePath: string) {
try {
await fs.stat(filePath);
return true;
} catch (_e) {
return false;
}
}
10 changes: 10 additions & 0 deletions src/server/db/createDb.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import path from 'path';
import SQLite from 'better-sqlite3';
import { Kysely, ParseJSONResultsPlugin, SqliteDialect } from 'kysely';
import { ensureFolderExistsSync } from '@server/utils';
import type { Database } from './types';

export default function createDb(dbPath: string) {
ensureDataFolderExists(dbPath);

console.log(`Using database at ${dbPath}`);
return new Kysely<Database>({
dialect: new SqliteDialect({
Expand Down Expand Up @@ -41,3 +45,9 @@ export default function createDb(dbPath: string) {
},
});
}

function ensureDataFolderExists(dbPath: string) {
const dir = path.dirname(dbPath);
console.log('Ensuring data directory exists', dir);
ensureFolderExistsSync(dir);
}
2 changes: 2 additions & 0 deletions src/server/userSettings/store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { app } from 'electron';
import fs from 'fs';
import path from 'path';
import { ensureFolderExistsSync } from '@server/utils';
import { UserSettings, UserSettingsFile } from './types';
import UserSettingsEventEmitter from './UserSettingsEventEmitter';

Expand Down Expand Up @@ -72,6 +73,7 @@ function createNewSettings(): UserSettings {
dataPath: path.join(app.getPath('userData'), 'data'),
},
};
ensureFolderExistsSync(app.getPath('userData'));
fs.writeFileSync(getUserSettingsPath(), JSON.stringify(file, null, 2));
return UserSettings.parse(file.settings);
}
Expand Down
26 changes: 26 additions & 0 deletions src/server/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import fs from 'fs';
import fsPromises from 'fs/promises';

export async function fileExists(filePath: string) {
try {
await fsPromises.stat(filePath);
return true;
} catch (_e) {
return false;
}
}

export async function fileExistsSync(filePath: string) {
try {
fs.statSync(filePath);
return true;
} catch (_e) {
return false;
}
}

export function ensureFolderExistsSync(dirPath: string) {
if (!fileExistsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
}