-
Notifications
You must be signed in to change notification settings - Fork 3
/
opdmctl
executable file
·133 lines (121 loc) · 4.26 KB
/
opdmctl
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env node
const fs = require('fs');
const commander = require('commander');
const child_process = require('child_process');
const sass = require('node-sass');
commander
.version('0.0.1-alpha')
.description('Server for OpenDataMap');
// development server
commander
.command('serve')
.option('-p, --port [port]', 'Port')
.option('-w, --watch', 'Watch for filechanges')
.description('Start the server')
.action((options) => {
// build the assets
console.log('Please wait! The assets were built');
buildAssets();
// start server
const server = require('./backend/server');
server.start(options.port || process.env.PORT || 4000);
if (options.watch) {
const watch = require('recursive-watch');
console.log('Start watching');
watch('./src/', (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');
});
// update
commander
.command('update')
.description('Update the OpenDataMap from the current version')
.action(() => {
const inquirer = require('inquirer');
inquirer.prompt({
type: 'confirm',
name: 'continue',
message: 'You you really want to update the OpenDataMap? You will LOSE all changed files since your last commit except the config file.',
default: false
}).then((answers) => {
if (answers.continue) {
child_process.spawnSync("git", ['reset', '--hard', 'HEAD']);
child_process.spawnSync("git", ['pull', 'origin', 'master']);
child_process.spawnSync('npm', ['install']);
child_process.spawnSync('pm2', ['restart', 'opendatamap']);
} else {
console.error("Quit");
}
});
});
// 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;
});
})
}