diff options
author | 2022-07-10 16:07:45 +0200 | |
---|---|---|
committer | 2022-07-11 17:47:53 +0200 | |
commit | fa1a7037b47f2e0114d8abc5a99d29239bd3637b (patch) | |
tree | 83404acf711aa8976dce47950edcca64836e0cd8 /src/internal-server/start.ts | |
parent | 6.0.0-nightly.96 [skip ci] (diff) | |
download | ferdium-app-fa1a7037b47f2e0114d8abc5a99d29239bd3637b.tar.gz ferdium-app-fa1a7037b47f2e0114d8abc5a99d29239bd3637b.tar.zst ferdium-app-fa1a7037b47f2e0114d8abc5a99d29239bd3637b.zip |
refactor: local server import/export
Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'src/internal-server/start.ts')
-rw-r--r-- | src/internal-server/start.ts | 41 |
1 files changed, 32 insertions, 9 deletions
diff --git a/src/internal-server/start.ts b/src/internal-server/start.ts index 62311b21e..ae28e3313 100644 --- a/src/internal-server/start.ts +++ b/src/internal-server/start.ts | |||
@@ -16,36 +16,59 @@ | |||
16 | */ | 16 | */ |
17 | 17 | ||
18 | import fold from '@adonisjs/fold'; | 18 | import fold from '@adonisjs/fold'; |
19 | import { Ignitor } from '@adonisjs/ignitor'; | 19 | import { Ignitor, hooks } from '@adonisjs/ignitor'; |
20 | import { existsSync, readFile, statSync, chmodSync, writeFile } from 'fs-extra'; | 20 | import { readFile, stat, chmod, writeFile } from 'fs-extra'; |
21 | import { join } from 'path'; | 21 | import { join } from 'path'; |
22 | import { LOCAL_HOSTNAME } from '../config'; | 22 | import { LOCAL_HOSTNAME } from '../config'; |
23 | import { isWindows } from '../environment'; | 23 | import { isWindows } from '../environment'; |
24 | 24 | ||
25 | process.env.ENV_PATH = join(__dirname, 'env.ini'); | 25 | process.env.ENV_PATH = join(__dirname, 'env.ini'); |
26 | 26 | ||
27 | export const server = async (userPath: string, port: number) => { | 27 | async function ensureDB(dbPath: string): Promise<void> { |
28 | const dbPath = join(userPath, 'server.sqlite'); | 28 | try { |
29 | const dbTemplatePath = join(__dirname, 'database', 'template.sqlite'); | 29 | await stat(dbPath); |
30 | 30 | } catch { | |
31 | if (!existsSync(dbPath)) { | 31 | // Database does not exist. |
32 | // Manually copy file | 32 | // Manually copy file |
33 | // We can't use copyFile here as it will cause the file to be readonly on Windows | 33 | // We can't use copyFile here as it will cause the file to be readonly on Windows |
34 | const dbTemplatePath = join(__dirname, 'database', 'template.sqlite'); | ||
34 | const dbTemplate = await readFile(dbTemplatePath); | 35 | const dbTemplate = await readFile(dbTemplatePath); |
35 | await writeFile(dbPath, dbTemplate); | 36 | await writeFile(dbPath, dbTemplate); |
36 | 37 | ||
37 | // Change permissions to ensure to file is not read-only | 38 | // Change permissions to ensure to file is not read-only |
38 | if (isWindows) { | 39 | if (isWindows) { |
40 | const stats = await stat(dbPath); | ||
39 | // eslint-disable-next-line no-bitwise | 41 | // eslint-disable-next-line no-bitwise |
40 | chmodSync(dbPath, statSync(dbPath).mode | 146); | 42 | await chmod(dbPath, stats.mode | 146); |
41 | } | 43 | } |
42 | } | 44 | } |
45 | } | ||
46 | |||
47 | export const server = async (userPath: string, port: number, token: string) => { | ||
48 | const dbPath = join(userPath, 'server.sqlite'); | ||
49 | await ensureDB(dbPath); | ||
43 | 50 | ||
44 | // Note: These env vars are used by adonis as env vars | 51 | // Note: These env vars are used by adonis as env vars |
45 | process.env.DB_PATH = dbPath; | 52 | process.env.DB_PATH = dbPath; |
46 | process.env.USER_PATH = userPath; | 53 | process.env.USER_PATH = userPath; |
47 | process.env.HOST = LOCAL_HOSTNAME; | 54 | process.env.HOST = LOCAL_HOSTNAME; |
48 | process.env.PORT = port.toString(); | 55 | process.env.PORT = port.toString(); |
56 | process.env.FERDIUM_LOCAL_TOKEN = token; | ||
49 | 57 | ||
50 | new Ignitor(fold).appRoot(__dirname).fireHttpServer().catch(console.error); | 58 | return new Promise<void>((resolve, reject) => { |
59 | let returned = false; | ||
60 | hooks.after.httpServer(() => { | ||
61 | if (!returned) { | ||
62 | resolve(); | ||
63 | returned = true; | ||
64 | } | ||
65 | }); | ||
66 | new Ignitor(fold).appRoot(__dirname).fireHttpServer().catch((error) => { | ||
67 | console.error(error); | ||
68 | if (!returned) { | ||
69 | returned = true; | ||
70 | reject(error); | ||
71 | } | ||
72 | }); | ||
73 | }); | ||
51 | }; | 74 | }; |