25332b0555da3d735621f694964696efb8227e16
2 * @file Establishes runtime options from args and config files
6 * @copyright jk software 2017
8 const fs
= require('fs')
9 const path
= require('path')
12 * @summary Parse arguments with minimist
14 exports
= require('minimist')(process
.argv
.slice(2), {
15 string
: [ 'config', 'client-js', 'host-js', 'ca-cert', 'ca-key',
16 'port', 'index', 'bind', 'electron', 'dedicated'
18 boolean: [ 'no-tls', 'legacy-socket' ],
33 console
.log('Unknown operator: ' + opt
)
34 console
.log(process
.argv
.join(' '))
35 console
.log(fs
.readFileSync('./usage', { encoding
: 'utf8' }))
43 exports
['defaults'] = {
44 config
: '/etc/strapp.conf:~/.strapp/strapp.conf:./strapp.conf',
45 'client-js': './client.js',
46 'host-js': './host.js',
48 'ca-cert': '../certs/cert.pem',
49 'ca-key': '../certs/key.pem',
51 index
: './www/index.html',
55 'legacy-socket': false
58 exports
= Object
.assign({}, exports
['defaults'], exports
)
61 * @summary Parse config files
64 exports
['config'].split(':').forEach((fileName
) => {
65 let filePath
= path
.resolve(fileName
)
66 if (fs
.existsSync(filePath
)){
68 String(fs
.readFileSync(path
.resolve(fileName
))).split('\n').forEach((line
) => {
71 if (!line
.match(/[\s]*#/)) {
72 let kv
= line
.replace(/#.*/g,'').replace(/\s/g,'').split('=')
73 if (kv
.length
== 2 && kv
[0] in exports
['defaults']) {
74 /* Multiple "bind=..." lines in are additive */
75 if (kv
[0] === 'bind' && 'bind' in exports
['conf']) {
76 kv
[1] = exports
['conf']['bind'] + ',' + kv
[1]
78 exports
['conf'][kv
[0]] = kv
[1]
81 console
.log(`WARN: Bad option ${line} in ${fileName}:${lineNo}`)
85 else if (exports
['config'] !== exports
['defaults']['config'])
86 console
.log(`WARN: opt '-c${exports['config']}', ${fileName} inaccessible`)
89 exports
= Object
.assign({}, exports
['defaults'], exports
['conf'], exports
)
92 * @summary Parse bindings
94 exports
['bindings'] = {}
95 exports
['bind'].replace(/\s/g,'').split(',').forEach((kvp
) => {
96 let kv
= kvp
.split(':')
97 kv
[1] = path
.resolve(kv
[1])
98 if (fs
.existsSync(kv
[1])) {
99 if (kv
.length
== 2 && /^[-_.A-Za-z0-9]/g.test(kv
[0]))
100 exports
['bindings'][kv
[0]] = kv
[1]
102 console
.log(`WARN: Invalid binding: ${kvp}`)
105 console
.log(`WARN: Binding not made, directory ${kvp} inaccessible`)
108 module
.exports
= exports