-
Notifications
You must be signed in to change notification settings - Fork 254
/
index.js
112 lines (93 loc) · 2.99 KB
/
index.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
#!/usr/bin/env node
var livePort = Number(process.env.GLSLIFY_LIVE_PORT = 12491)
var answers = require('./lib/create-answers')
var exmap = require('./lib/exercise-map')
var live = require('glslify-live/server')
var exercises = require('./exercises')
var styles = require('./style')
var opener = require('opener')
var beefy = require('beefy')
var chalk = require('chalk')
var http = require('http')
var path = require('path')
var url = require('url')
var fs = require('fs')
var mainPort = 12492
var closeWindow = fs.readFileSync(
path.join(__dirname, 'lib/close-window.html')
)
module.exports = createServer
createServer(process.cwd())
function createServer(root) {
console.error(fs.readFileSync(
__dirname + '/intro.txt', 'utf8'
))
live().listen(livePort, function(err) {
if (err) throw err
answers(root, function(err) {
if (err) throw err
console.error('Done!')
console.error('Booting up the workshop in your browser in just a second...')
console.error('')
setTimeout(loadedAnswers, 1000)
})
})
function loadedAnswers(err) {
if (err) throw err
var exNames = Object.keys(exercises)
var exLinks = exNames.map(function(k) { return exercises[k] })
var exSlugs = exLinks.map(function(k) { return exmap[k] })
var exFiles = exSlugs.map(function(link, i) {
var dir = path.resolve(root, link)
return fs.readdirSync(dir).map(function(name) {
return path.resolve(dir, name)
})
})
var exRoutes = exLinks.map(function(link, i) {
return require('./exercises/' + link + '/server.js')(exFiles[i])
})
var menu = beefy({
cwd: path.join(__dirname, 'menu')
, entries: ['index.js']
, quiet: false
, watchify: false
})
http.createServer(function(req, res) {
// Force the addition of a query string. This
// works around an issue in beefy until it gets fixed:
// https://github.com/stackgl/shader-school/issues/107
req.url = req.url.indexOf('?') === -1
? req.url + '?'
: req.url
var uri = url.parse(req.url).pathname
var paths = uri.split('/').filter(Boolean)
if (uri === '/style.css') {
res.setHeader('content-type', 'text/css')
res.end(styles())
return
}
if (paths[0] === 'open') {
var exPath = exmap[paths[1]]
if (exPath) {
opener(path.join(root, exPath))
}
return res.end(closeWindow)
}
for (var i = 0; i < exLinks.length; i++) {
if (uri.indexOf(exLinks[i]) === 1) {
req.url = req.url
.replace(exLinks[i], '')
.replace(/\/+/g, '/')
return exRoutes[i](req, res)
}
}
return menu(req, res)
}).listen(mainPort, function(err) {
if (err) throw err
var url = 'http://localhost:'+mainPort
opener(url)
console.log(chalk.yellow('WORKSHOP URL:'), chalk.underline.blue(url))
console.log()
})
}
}