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', 'bind', 'electron', 'file-dir', 'dedicated'
18 boolean: [ 'no-tls', 'legacy-socket' ],
32 console
.log('Unknown operator: ' + opt
)
33 console
.log(process
.argv
.join(' '))
34 console
.log(fs
.readFileSync('./usage', { encoding
: 'utf8' }))
42 exports
['defaults'] = {
43 config
: '/etc/strapp.conf:~/.strapp/strapp.conf:./strapp.conf',
44 'client-js': './client-test.js',
45 'host-js': './host-test.js',
47 'ca-cert': '../certs/cert.pem',
48 'ca-key': '../certs/key.pem',
52 'file-dir': undefined,
54 'legacy-socket': false
57 exports
= Object
.assign({}, exports
['defaults'], exports
)
60 * @summary Parse config files
63 exports
['config'].split(':').forEach((fileName
) => {
64 let filePath
= path
.resolve(fileName
)
65 if (fs
.existsSync(filePath
)){
67 String(fs
.readFileSync(path
.resolve(fileName
))).split('\n').forEach((line
) => {
70 if (!line
.match(/[\s]*#/)) {
71 let kv
= line
.replace(/#.*/g,'').replace(/\s/g,'').split('=')
72 if (kv
.length
== 2 && kv
[0] in exports
['defaults']) {
73 /* Multiple "bind=..." lines in are additive */
74 if (kv
[0] === 'bind' && 'bind' in exports
['conf']) {
75 kv
[1] = exports
['conf']['bind'] + ',' + kv
[1]
77 exports
['conf'][kv
[0]] = kv
[1]
80 console
.log(`WARN: Bad option ${line} in ${fileName}:${lineNo}`)
84 else if (exports
['config'] !== exports
['defaults']['config'])
85 console
.log(`WARN: opt '-c${exports['config']}', ${fileName} inaccessible`)
88 exports
= Object
.assign({}, exports
['defaults'], exports
['conf'], exports
)
91 * @summary Parse bindings
93 exports
['bindings'] = {}
94 exports
['bind'].replace(/\s/g,'').split(',').forEach((kvp
) => {
95 let kv
= kvp
.split(':')
96 kv
[1] = path
.resolve(kv
[1])
97 if (fs
.existsSync(kv
[1])) {
98 if (kv
.length
== 2 && /^[-_.A-Za-z0-9]/g.test(kv
[0]))
99 exports
['bindings'][kv
[0]] = path
.normalize(kv
[1])
101 console
.log(`WARN: Invalid binding: ${kvp}`)
104 console
.log(`WARN: Binding not made, directory ${kvp} inaccessible`)
107 module
.exports
= exports