forked from tovyblox/tovy1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.js
82 lines (72 loc) · 2.84 KB
/
start.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
79
80
81
const { exec, fork } = require("child_process");
const ex = require('util').promisify(exec);
const express = require('express');
const app = express();
const path = require('path')
const history = require('connect-history-api-fallback');
require('dotenv').config({ path: './backend/.env' });
const inquirer = require('inquirer');
const ora = require('ora');
const envfile = require('envfile');
const staticFileMiddleware = express.static(path.join(__dirname, 'dist'));
const fs = require('fs');
const spinner = ora({ text: 'Building app..', spinner: 'dots', color: 'red' })
if (!process.env.MONGO_URI) {
inquirer.prompt([
{
type: 'input',
name: 'MongoDB',
message: 'Please provide a mongodb link (learn more @ https://tovy.gitbook.io/tovy/hosting/creating-a-mongodb-database)',
validatingText: 'Checking URI',
validate: async (answer) => {
if (!answer.includes('mongodb+srv://') && !answer.includes('mongodb://')) return 'Please enter a valid mongodb srv URI '
return true
},
},
{
type: 'input',
name: 'port',
message: 'If you would like to run tovy on a custom port please enter that port below!',
default: 8080
}
]).then(answers => {
const envobj = {
MONGO_URI: answers.MongoDB,
PORT: answers.port || 8080
}
fs.writeFile('./backend/.env', envfile.stringify(envobj), (err) => {
if (err) throw err;
console.log('The file has been saved!');
runBuild();
})
})
} else {
runBuild();
}
function runBuild() {
spinner.start();
let chield = ex('npm run build').then(async (result) => {
setTimeout(() => {
startbackend();
}, 1000)
spinner.stop();
console.log(`
████████╗░█████╗░██╗░░░██╗██╗░░░██╗
╚══██╔══╝██╔══██╗██║░░░██║╚██╗░██╔╝
░░░██║░░░██║░░██║╚██╗░██╔╝░╚████╔╝░
░░░██║░░░██║░░██║░╚████╔╝░░░╚██╔╝░░
░░░██║░░░╚█████╔╝░░╚██╔╝░░░░░██║░░░
░░░╚═╝░░░░╚════╝░░░░╚═╝░░░░░░╚═╝░░░`)
console.log(`Tovy has been successfully built`)
});
}
function startbackend() {
const child = fork('./backend/index.js', []);
child.on('error', message => {
console.log('message from child:', message);
});
child.on('close',(code) => {
console.log(`child process exited with ${code}`);
startbackend();
})
}