-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimize.js
50 lines (46 loc) · 1.24 KB
/
optimize.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
'use strcit'
const fs = require('fs')
const path = require('path')
const noop = () => {}
const sort = (object) => {
// Adapted from https://whitfin.io/sorting-object-recursively-node-jsjavascript/
let sorted = {}
let keys = Object.keys(object)
keys.sort((a, b) => {
if (a < b) return -1
if (a > b) return 1
return 0
})
keys.forEach((key) => {
if (typeof object[key] === 'object' && !(object[key] instanceof Array)) {
sorted[key] = sort(object[key])
} else {
sorted[key] = object[key]
}
})
return sorted
}
const read = (filename) => {
fs.accessSync(path.join(__dirname, './data/', filename), fs.R_OK | fs.W_OK)
delete require.cache[require.resolve(`./data/${filename}`)]
return require(`./data/${filename}`)
}
const save = (filename, data) => {
fs.writeFile(path.join(__dirname, './data', filename), JSON.stringify(sort(data), null, 2) + '\n', noop)
}
fs.readdir('./data/game/', (err, filenames) => {
if (err) {
return
}
let games = []
filenames.forEach((filename) => {
let game = read(`game/${filename}`)
games.push({
guid: game.guid,
title: game.title,
platform: game.platform
})
save(`game/${filename}`, game)
})
save('games.json', { games: games })
})