File size: 1,435 Bytes
3866bf7
 
7ee9dd5
 
 
 
 
 
 
 
 
3866bf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ee9dd5
 
 
 
 
 
3866bf7
3d1bdb6
3866bf7
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
const fs = require('fs');
const path = require('path');
const { spawn } = require('child_process');

function run(cmd, args) {
  return new Promise((resolve, reject) => {
    const p = spawn(cmd, args, { stdio: 'inherit', shell: process.platform === 'win32' });
    p.on('close', (code) => (code === 0 ? resolve() : reject(new Error(`${cmd} exited ${code}`))));
  });
}

function resolveSqlitePath(databaseUrl) {
  if (typeof databaseUrl !== 'string' || !databaseUrl.startsWith('file:')) {
    return null;
  }

  const rawPath = databaseUrl.slice(5).split('?')[0].split('#')[0];
  if (!rawPath) {
    return null;
  }

  return path.isAbsolute(rawPath) ? rawPath : path.resolve(process.cwd(), rawPath);
}

function ensureSqliteDirectory(databaseUrl) {
  const sqlitePath = resolveSqlitePath(databaseUrl);
  if (!sqlitePath) {
    return null;
  }

  fs.mkdirSync(path.dirname(sqlitePath), { recursive: true });
  return sqlitePath;
}

async function main() {
  const { CONFIG } = require('../src/config');
  const { startServer } = require('../src/server');

  ensureSqliteDirectory(CONFIG.DATABASE_URL);

  try {
    await run('npx', ['prisma', 'db', 'push']);
  } catch (e) {
    // eslint-disable-next-line no-console
    console.error('Prisma db push failed:', e.message);
  }

  await startServer();
}

if (require.main === module) {
  main();
}

module.exports = {
  ensureSqliteDirectory,
  main,
  resolveSqlitePath,
  run
};