-
Notifications
You must be signed in to change notification settings - Fork 181
/
generateColormapUnitDictionary.js
70 lines (60 loc) · 2.24 KB
/
generateColormapUnitDictionary.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
const fs = require('fs')
const path = require('path')
const SOURCE_DIR = `${path.resolve(__dirname)}/../config/default/release/gc/colormaps/`
const OUTPUT_DIR = process.argv[2] || './'
const OUTPUT_FILENAME = '_COLORMAP_UNIT_DICT_.json'
// HELPERS
// https://gist.github.com/nrkn/c7a89bb7039182314415 JSON FORMATTING
const isPrimitive = (obj) => obj === null ||
['string', 'number', 'boolean'].includes(typeof obj)
const isArrayOfPrimitive = (obj) => Array.isArray(obj) && obj.every(isPrimitive)
const format = (arr) => `^^^[ ${
arr.map((val) => JSON.stringify(val)).join(', ')
} ]`
const replacer = (key, value) => (isArrayOfPrimitive(value) ? format(value) : value)
const expand = (str) => str.replace(
/(?:"\^\^\^)(\[ .* \])(?:")/g, (match, a) => a.replace(/\\"/g, '"')
)
const readDirPromise = (dirname) => new Promise((resolve, reject) => {
fs.readdir(dirname,
(err, filenames) => (err ? reject(err) : resolve(filenames)))
})
// MAIN COLORMAP UNIT DICTIONARY GENERATION
async function generateColormapUnitDictionary (dirname) {
const data = {}
const filenames = await readDirPromise(dirname)
try {
return Promise.all(filenames.map(async (filename) => {
const content = await new Promise((resolve, reject) => {
fs.readFile(dirname + filename, 'utf8',
(err, content) => (err ? reject(err) : resolve(content)))
})
const parseUnits = (fileContent) => {
const colorMapName = filename.split('.xml')[0]
data[colorMapName] = []
const lines = fileContent.split(/\r?\n/)
lines.forEach((line) => {
if (line.includes('units')) {
const unitsString = line.split('units=')[1]
const units = unitsString.split('"')[1]
data[colorMapName].push(units)
}
})
}
parseUnits(content)
})).then(() => data)
} catch (error) {
return Promise.reject(error)
}
}
async function main () {
const data = await generateColormapUnitDictionary(SOURCE_DIR)
fs.writeFile(`${OUTPUT_DIR}${OUTPUT_FILENAME}`, expand(JSON.stringify(data, replacer, 2)), {
encoding: 'utf8',
flag: 'w'
}, (err) => {
if (err) throw err
console.log(`Done writing colormap unit dictionary to ${OUTPUT_DIR}${OUTPUT_FILENAME}.`)
})
}
main()