-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
97 lines (83 loc) · 2.77 KB
/
server.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
const express = require('express');
const path = require('path');
const fs = require('fs');
const glob = require("glob")
const app = express();
const BUNDLES_PATH = path.join(__dirname, 'apps/webpack');
const LOCALE_PATH = path.join(__dirname, 'apps/bundles');
app.set('view engine', 'ejs');
app.use('/app/apps/webpack', express.static(path.join(__dirname, 'apps/webpack')));
app.use('/app/runtime', express.static(path.join(__dirname, 'runtime')));
app.use('/app/apps/bundles', express.static(path.join(__dirname, 'apps/bundles')));
app.get('/', (req, res) => res.render(path.join(__dirname, 'app-list.ejs'), _getAppListParams(true)))
app.get('/app/:app', (req, res) => {
const app = req.params.app;
if (_isAppExists(app)) {
const params = _getParamsForApp(app);
res.render(path.join(__dirname, 'landing.ejs'), params)
} else {
const params = _getAppListParams();
res.render(path.join(__dirname, 'app-list.ejs'), params)
}
})
app.listen(3000, () => console.log('Example app listening on port 3000!'))
function _isAppExists(app) {
const bundle = _getAppBundle(app);
return !!bundle;
}
function _getParamsForApp(app) {
return {
title: app,
app: app,
styles: {
common: ['./apps/webpack', _getAppStyle('common')].join('/'),
app: ['./apps/webpack', _getAppStyle(app)].join('/')
},
runtime: './runtime/ts-9.3.11.js',
locale: ['./apps/bundles', _getAppLocale('core.locale.en_US')].join('/'),
bundles: {
common: ['./apps/webpack', _getAppBundle('common')].join('/'),
app: ['./apps/webpack', _getAppBundle(app)].join('/')
}
}
}
function _getAppListParams(isIndex) {
const files = glob.sync(path.join(BUNDLES_PATH, 'Tradeshift.*.*.js'), {});
const apps = files.map(file => {
const name = path.basename(file);
const nameParts = name.match(/(\w+).(\w+).*.*/i)
return [nameParts[1], nameParts[2]].join('.');
});
return {
apps: apps,
indexPage: isIndex
};
}
function _getAppLocale(localePattern) {
if (!app) {
return '';
}
const bundlePath = path.join(LOCALE_PATH, localePattern + '.*.js');
return _getFileByPattern(bundlePath);
}
function _getAppBundle(app) {
if (!app) {
return '';
}
const bundlePath = path.join(BUNDLES_PATH, app + '.*.js');
return _getFileByPattern(bundlePath);
}
function _getAppStyle(app) {
if (!app) {
return '';
}
const stylePath = path.join(BUNDLES_PATH, app + '.*.css');
return _getFileByPattern(stylePath);
}
function _getFileByPattern(pattern) {
if (!pattern) {
return '';
}
const files = glob.sync(pattern, {});
return files.length > 0 ? path.basename(files[0]) : '';
}