-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
115 lines (104 loc) · 3.19 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
let express = require('express')
let os = require('os')
let fs = require('fs')
let path = require('path')
let getImageSize = require('image-size')
let plist = require('plist')
const relativePath = 'images/tilesets/transparent'
const tilesetDirectory = path.resolve(__dirname, `public/${relativePath}`)
const colorThemeDirectory = path.resolve(__dirname, `public/themes`)
let storedTilesets = []
let storedColorThemes = []
function cacheTilesetsByDimension() {
// ASCII IBM CODE PAGE 437 is always 16x16
const columns = 16
const rows = 16
try {
const files = fs.readdirSync(tilesetDirectory)
for (let file of files) {
if (file !== '.DS_Store') {
const { width, height } = getImageSize(path.resolve(tilesetDirectory, file))
storedTilesets.push({
name: file,
url: relativePath + `/${file}`,
imageWidth: width,
imageHeight: height,
spriteWidth: width / columns,
spriteHeight: height / rows
})
}
}
} catch (err) {
console.error(err)
process.exit(1)
}
}
function cacheColorThemes() {
// Loop through all of the OSX term themes, and load them as JSON
try {
const files = fs.readdirSync(colorThemeDirectory)
for (let file of files) {
let properties = plist.parse(fs.readFileSync(path.resolve(colorThemeDirectory, file), 'utf8'))
let theme = { name: properties.name }
for (let property in properties) {
if ((property.startsWith('ANSI') || property.startsWith('Background')) && property.includes('Color')) {
let formattedColor = property.replace('ANSI', '').replace('Color', '')
let regexp = /\d(?:\.?\d+)? \d(?:\.?\d+)? \d(?:\.?\d+)?/
let parsedBase64Buffer = properties[property].toString()
let matches = parsedBase64Buffer.match(regexp)
if (matches && matches.length === 1) {
let colors = matches[0].split(' ')
if (colors.length === 3) {
let hex = colors
.map(c => {
let hex = (~~(parseFloat(c) * 255)).toString(16)
if (hex.length < 2) hex = '0' + hex
return hex
})
.join('')
theme[formattedColor] = '#' + hex
} else {
console.log(`Failed to parse ${property} for RGB - ${parsedBase64Buffer}`)
}
} else {
console.log(`Failed to parse ${property} for RGB - ${parsedBase64Buffer}`)
}
}
}
storedColorThemes.push(theme)
}
} catch (err) {
console.error(err.stack)
}
}
cacheTilesetsByDimension()
cacheColorThemes()
let app = express()
let port = process.env.PORT || 5000
// Allow CORS for my webpack server
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
next()
})
app.get('/tilesets', (request, response) => {
response.send({
tilesetPath: relativePath,
tilesets: storedTilesets
})
})
app.get('/color-themes', (request, response) => {
response.send({
themes: storedColorThemes
})
})
app.get('/themes-and-tilesets', (request, response) => {
response.send({
tilesetPath: relativePath,
tilesets: storedTilesets,
themes: storedColorThemes
})
})
app.use('/', express.static(path.join(__dirname, 'dist')))
app.listen(port)
console.log('Serving extended-ascii-previewer on port: ' + port)