-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathserver.js
86 lines (74 loc) · 1.95 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
'use strict'
const fs = require('fs')
const sass = require('node-sass')
const express = require('express')
const bodyParser = require('body-parser')
const THEME = 'node_modules/reveal.js/css/theme/'
const SOURCE = THEME + 'source/'
const TEMPLATE = THEME + 'template/'
const mixins = fs.readFileSync(TEMPLATE + 'mixins.scss')
const settings = fs.readFileSync(TEMPLATE + 'settings.scss')
const theme = fs.readFileSync(TEMPLATE + 'theme.scss')
const app = express()
app.set('port', process.env.PORT || 8080)
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static(__dirname))
// generated css
var custom = ''
// called by the angular app
app.post('/convert', (req, res) => {
const scss = mixins + settings + req.body.scss + theme
sass.render(
{
data: scss,
},
(err, result) => {
if (err) {
console.error('sass error', err)
res.send('sass error')
} else {
custom = result.css
res.send(result.css)
}
}
)
})
// called by the reveal iframe
app.get('/custom.css', (req, res) => {
res.type('css')
res.send(custom)
})
app.get('/themes/:id', (req, res) => {
fs.readFile(SOURCE + req.params.id + '.scss', 'utf-8', (err, file) => {
if (err) console.error(err)
res.send(mapScss(file))
})
})
app.get('/themes', (req, res) => {
fs.readdir(SOURCE, (err, files) => {
if (err) console.error(err)
// extension irrelevant
files = files.map(file => file.split('.')[0])
res.send(files)
})
})
app.listen(app.get('port'), () => {
console.log(`Express server listening on port ${app.get('port')}`)
})
// convert a scss into a map of vars/value
const mapScss = file => {
return (
file
.split('\n')
// only vars
.filter(line => line[0] === '$')
// trim $ and ;
.map(line => line.slice(1, -1))
.reduce((o, v) => {
v = v.split(':')
o[v[0].trim()] = v[1].trim()
return o
}, {})
)
}