-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (53 loc) · 1.66 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
const path = require("path")
const fs = require("fs")
const glob = require('glob')
var parseArgs = require('minimist')
const argv = parseArgs(process.argv.slice(2));
const sourcePath = argv['source']
const config = {
input: path.join(__dirname, sourcePath),
output: path.join(__dirname, 'dist', 'found-names.json'),
}
// Delete the previous output file
// Temporary indicator of failure, since error handling isn't working correctly
try {
fs.unlinkSync(config.output)
} catch (_) {
// Don't error if the file isn't there
}
let worker, flags
try {
worker = require('./dist/worker')
} catch (_) {
console.error('No ./dist/worker.js file found. Make sure to run "npm run build" first!')
process.exit(1)
}
try {
const input = glob.sync(config.input + '/**/*.elm', {}).map(file => {
return fs.readFileSync(file, { encoding: 'utf-8' })
})
flags = { files: input }
} catch (err) {
console.error('Could not read file: ' + config.input + 'Error: ' + err)
process.exit(1)
}
const app = worker.Elm.Worker.init({ flags })
app.ports.outgoing.subscribe(message => {
const handler = handlers[message.tag]
if (!handler) return console.warn('UNKNOWN', message)
handler(message.payload)
})
const handlers = {
'Ok': (payload) => {
console.log('Read ' + config.input + ' successfully.')
console.log('Processing output file...')
try {
fs.writeFileSync(config.output, JSON.stringify(payload, null, ' '), { encoding: 'utf-8' })
} catch (err) {
console.error("Couldn't write file. Error: " + err)
process.exit(1)
}
console.log('Wrote ' + config.output + ' successfully.')
},
'Err': (payload) => console.warn('ERR', payload),
}