forked from OpenDataMap/opendatamap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
118 lines (108 loc) · 3.63 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const fs = require('fs');
const commander = require('commander');
const child_process = require('child_process');
const sass = require('node-sass');
commander
.version('0.4.6')
.description('Server for OpenDataMap');
// development server
commander
.command('development [port]')
.alias('dev')
.description('server for development use')
.action(function (portInput) {
const watch = require('recursive-watch');
// build the assets
console.log('Please wait! The assets were built');
buildAssets();
// start server
const server = require('./backend/server');
server.start(portInput || process.env.PORT || 4000);
console.log('start watching')
watch('./src/', function (filename) {
if (filename) {
const filetype = filename.substring(filename.lastIndexOf('.') + 1);
switch (filetype) {
case "ts":
case "html":
case "json":
buildWebpack();
break;
case "scss":
buildSass();
break;
}
}
})
});
// test server
commander
.command('test')
.description('test')
.action(function () {
buildAssets();
console.log('Passed');
})
// server for production use
commander
.command('production [port]')
.alias('prod')
.description('server for production use')
.action(function (portInput) {
// build the assets
console.log('Please wait! The assets were built');
buildAssets();
const server = require('./backend/server');
// start server
server.start(portInput || process.env.PORT || 4000);
});
// show help if no command was entered
commander.parse(process.argv)
if (commander.args.length === 0) {
commander.help();
}
function buildAssets() {
buildWebpack();
buildSass();
if (!fs.existsSync('dist/data')) {
fs.mkdirSync('dist/data')
}
if (!fs.existsSync('backend/dataSources')) {
fs.mkdirSync('backend/dataSources')
}
console.log('The assets finished building!');
}
function buildWebpack() {
//typescript server
const backendBuild = child_process.spawnSync('node_modules/typescript/bin/tsc', ['backend/server.ts', '--lib', 'es2015,dom,esnext.asynciterable', '-m', 'commonjs']);
if (backendBuild.stderr.toString()) throw backendBuild.stderr.toString();
if (backendBuild.stdout.toString().includes('Error') || backendBuild.stdout.toString().includes('ERROR')) throw backendBuild.stdout.toString();
// webpack-build
const webpackBuild = child_process.spawnSync('node_modules/webpack/bin/webpack.js');
if (webpackBuild.stderr.toString()) throw webpackBuild.stderr.toString();
if (webpackBuild.stdout.toString().includes('Error') || webpackBuild.stdout.toString().includes('ERROR')) throw webpackBuild.stdout.toString();
}
function buildSass() {
// sass build
if (!fs.existsSync('dist/css')) {
fs.mkdirSync('dist/css')
}
sass.render({
file: 'src/scss/light/main.scss',
outputStyle: "compressed"
}, function (err, result) {
if (err) throw err;
fs.writeFile('dist/css/light.css', result.css, (error) => {
if (error) throw error;
});
});
sass.render({
file: 'src/scss/night/main.scss',
outputStyle: "compressed"
}, function (err, result) {
if (err) throw err;
fs.writeFile("dist/css/night.css", result.css, (error) => {
if (error) throw error;
});
})
}