aboutsummaryrefslogtreecommitdiffstats
path: root/src/internal-server/start.js
blob: 683f24651565e933b3f458ef834f9995ea94a119 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
|--------------------------------------------------------------------------
| Http server
|--------------------------------------------------------------------------
|
| This file bootstraps Adonisjs to start the HTTP server. You are free to
| customize the process of booting the http server.
|
| """ Loading ace commands """
|     At times you may want to load ace commands when starting the HTTP server.
|     Same can be done by chaining `loadCommands()` method after
|
| """ Preloading files """
|     Also you can preload files by calling `preLoad('path/to/file')` method.
|     Make sure to pass a relative path from the project root.
*/

const fold = require('@adonisjs/fold');
const { Ignitor } = require('@adonisjs/ignitor');
const fs = require('fs-extra');
const os = require('os');
const path = require('path');
const packageJSON = require('../../package.json');

process.env.FERDI_VERSION = packageJSON.version;
process.env.ENV_PATH = path.join(__dirname, 'env.ini');

module.exports = async (userPath, port) => {
  const dbPath = path.join(userPath, 'server.sqlite');
  const dbTemplatePath = path.join(__dirname, 'database', 'template.sqlite');

  if (!fs.existsSync(dbPath)) {
    // Manually copy file
    // We can't use copyFile here as it will cause the file to be readonly on Windows
    const dbTemplate = await fs.readFile(dbTemplatePath);
    await fs.writeFile(dbPath, dbTemplate);

    // Change permissions to ensure to file is not read-only
    if (os.platform() === 'win32') {
      // eslint-disable-next-line no-bitwise
      fs.chmodSync(dbPath, fs.statSync(dbPath).mode | 146);
    }
  }

  process.env.DB_PATH = dbPath;
  process.env.USER_PATH = userPath;
  process.env.PORT = port;

  new Ignitor(fold).appRoot(__dirname).fireHttpServer().catch(console.error); // eslint-disable-line no-console
};