forked from chuot/rdio-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.js
78 lines (59 loc) · 2.54 KB
/
run.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'use strict';
const childProcess = require('child_process');
const fs = require('fs');
const path = require('path');
try {
const stdio = `${!!process.env.DEBUG}` === 'true' ? 'inherit' : 'ignore';
const clientPath = path.resolve(__dirname, 'client');
const serverPath = path.resolve(__dirname, 'server');
const missingModules = {
client: !fs.existsSync(path.resolve(clientPath, 'node_modules')),
server: !fs.existsSync(path.resolve(serverPath, 'node_modules')),
}
if (missingModules.client || missingModules.server) {
process.stdout.write('Installing node modules...');
if (missingModules.client) {
childProcess.execSync('npm install', { cwd: clientPath, stdio });
}
if (missingModules.server) {
childProcess.execSync('npm install', { cwd: serverPath, stdio });
}
process.stdout.write(' done\n');
}
const envFile = path.resolve(serverPath, '.env');
if (!fs.existsSync(envFile)) {
const apiKey = require(`${serverPath}/node_modules/uuid/v4`)();
const data = [
`DB_DIALECT=sqlite`,
`DB_STORAGE=database.sqlite`,
``,
`NODE_ENV=production`,
`NODE_HOST=0.0.0.0`,
`NODE_PORT=3000`,
``,
`RDIO_APIKEYS=["${apiKey}"]`,
`RDIO_PRUNEDAYS=7`,
``,
].join('\n');
fs.writeFileSync(envFile, data);
process.stdout.write(`Default configuration created at ${envFile}\n`);
process.stdout.write(`Make sure your upload scripts use this API key: ${apiKey}\n`);
}
if (!fs.existsSync(path.resolve(clientPath, 'dist/main.html'))) {
process.stdout.write('Building client app...');
childProcess.execSync('npm run build', { cwd: clientPath, stdio });
process.stdout.write(' done\n');
}
require(path.resolve(serverPath, 'node_modules/dotenv')).config({ path: envFile });
if (process.env.DB_DIALECT === 'sqlite') {
const dbFile = path.resolve(serverPath, process.env.DB_STORAGE || 'database.sqlite');
if (!fs.existsSync(dbFile)) {
process.stdout.write(`Creating SQLITE database at ${dbFile}...`);
childProcess.execSync('npm run migrate', { cwd: serverPath, stdio });
process.stdout.write(' done\n');
}
}
childProcess.execSync('node index.js', { cwd: serverPath, stdio: 'inherit' });
} catch (error) {
console.error('\n\nAn error has occured. Please re-run the command like this: \'DEBUG=true node run.js\'');
}