Skip to content

Commit

Permalink
fix: ensure userData folder also exists
Browse files Browse the repository at this point in the history
  • Loading branch information
dsaltares committed Oct 18, 2024
1 parent 2302de8 commit a4b3d76
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
11 changes: 2 additions & 9 deletions src/server/db/createDb.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import fs from 'fs';
import path from 'path';
import SQLite from 'better-sqlite3';
import { Kysely, ParseJSONResultsPlugin, SqliteDialect } from 'kysely';
import { fileExistsSync } from '@server/utils';
import { ensureFolderExistsSync } from '@server/utils';
import type { Database } from './types';

export default function createDb(dbPath: string) {
Expand Down Expand Up @@ -50,11 +49,5 @@ export default function createDb(dbPath: string) {
function ensureDataFolderExists(dbPath: string) {
const dir = path.dirname(dbPath);
console.log('Ensuring data directory exists', dir);

if (!fileExistsSync(dir)) {
console.log('Creating data directory');
fs.mkdirSync(dir, { recursive: true });
} else {
console.log('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
6 changes: 6 additions & 0 deletions src/server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ export async function fileExistsSync(filePath: string) {
return false;
}
}

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

0 comments on commit a4b3d76

Please sign in to comment.