1import path from 'node:path';
2import fs from 'node:fs';
3import os from 'node:os';
4import constants from '../constants';
5import AppDB from '../classes/AppDB';
6
7const PIC_DIR: string | null = readDirFile();
8
9export function getAppDataDir(): string {
10 let appDataDir = '';
11 const username = os.userInfo().username;
12
13 if (os.platform() === 'win32') {
14 appDataDir = path.join(
15 `C:\\Users\\${username}\\AppData\\Roaming\\${constants.APP_ID}`,
16 );
17 } else if (os.platform() === 'darwin') {
18 appDataDir = path.join(
19 `/Users/${username}/Library/Application Support/${constants.APP_ID}`,
20 );
21 } else {
22 appDataDir = path.join(`/home/${username}/.config/${constants.APP_ID}`);
23 }
24
25 return appDataDir;
26}
27
28function readDirFile() {
29 const dir = getAppDataDir();
30
31 const LOOKUP = path.join(dir, 'path.txt');
32
33 if (fs.existsSync(LOOKUP)) {
34 const data = fs.readFileSync(LOOKUP, 'utf-8');
35
36 const lines = data.split('\n');
37
38 if (lines.length > 0) {
39 return lines[0];
40 } else {
41 return null;
42 }
43 } else {
44 return null;
45 }
46}
47
48export async function firstTimeSetup(): Promise<void> {
49 try {
50 const appDataDir = getAppDataDir();
51
52 if (!fs.existsSync(appDataDir)) {
53 fs.mkdirSync(appDataDir, { recursive: true });
54 }
55
56 if (!fs.existsSync(path.join(appDataDir, 'path.txt'))) {
57 fs.writeFileSync(path.join(appDataDir, 'path.txt'), '');
58 }
59
60 const db = AppDB.getInstance();
61
62 db.createDatabase()
63 .then(() => {
64 console.log('Database created successfully');
65 })
66 .catch((error) => {
67 db.close();
68 console.error('An error occurred while creating the database:', error);
69 process.exit(1);
70 });
71 } catch (error) {
72 console.error('An error occurred while setting up the app:', error);
73 process.exit(1);
74 }
75}
76
77export function isFirstTimeSetup(): boolean {
78 return !fs.existsSync(path.join(getAppDataDir(), 'app.db'));
79}
80
81export function getPictureDirs(): {
82 avatar: string;
83 video: string;
84 image: string;
85 base: string;
86} {
87 if (PIC_DIR) {
88 const base = path.resolve(PIC_DIR);
89
90 const dirs = {
91 avatar: path.join(base, 'avatars'),
92 video: path.join(base, 'videos'),
93 image: path.join(base, 'images'),
94 base,
95 };
96
97 for (const dir of Object.values(dirs)) {
98 if (!fs.existsSync(dir)) {
99 fs.mkdirSync(dir, { recursive: true });
100 }
101 }
102
103 return dirs;
104 } else {
105 const homeDir = os.homedir();
106 const dirs = {
107 avatar: path.join(homeDir, 'Pictures', 'Avatars'),
108 video: path.join(homeDir, 'Pictures', 'Videos'),
109 image: path.join(homeDir, 'Pictures', 'Images'),
110 base: path.join(homeDir, 'Pictures'),
111 };
112
113 for (const dir of Object.values(dirs)) {
114 if (!fs.existsSync(dir)) {
115 fs.mkdirSync(dir, { recursive: true });
116 }
117 }
118
119 return dirs;
120 }
121}
122
123export function mkdir(dir: string): void {
124 if (!fs.existsSync(dir)) {
125 fs.mkdirSync(dir, { recursive: true });
126 }
127}
128
129export function dirDoesExist(dir: string): boolean {
130 return fs.existsSync(dir);
131}
132