Skip to content

Commit

Permalink
Merge pull request #27 from dsaltares/22/ensure-data-dir-exists
Browse files Browse the repository at this point in the history
fix: #22 ensure data folder exists at startup
  • Loading branch information
dsaltares authored Oct 18, 2024
2 parents a247428 + a4b3d76 commit 066c688
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
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 });
}
}

0 comments on commit 066c688

Please sign in to comment.