-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (70 loc) · 1.98 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/** ****************************************************************************************************
* File: index.js
* Project: LightDB
* @author Nick Soggin <[email protected]> on 30-May-2018
*******************************************************************************************************/
'use strict';
const
{
description,
name,
version
} = require( './package' );
( function() {
if( process.argv.includes( '-v' ) || process.argv.includes( '--version' ) ) {
return console.log( `v${ version }` );
} else if( process.argv.includes( '-h' ) || process.argv.includes( '--help' ) ) {
return console.log( [
`Welcome to ${ name }`,
` Version: v${ version }`,
'',
description,
'',
`Usage: ${ name } [options]`,
'',
' -h, --help help menu',
` -v, --version print ${ name } version`,
` -s, --silent run ${ name } in silent mode`,
` -p, --port [port] set the port to start ${ name } on`
].join( '\n' ) );
}
function parseArguments( args, defaults = {} ) {
args = args.splice( 2 );
return args.reduce(
( r, item, i ) => {
if( /(-s)|(--silent)/i.test( item ) ) {
r.silent = true;
} else if( /(-d)|(--data)/i.test( item ) ) {
const arg = args[ i + 1 ];
if( !arg ) {
console.error( 'Argument Error: -d, --data option must be specified' );
process.exit( 1 );
} else {
r.data = arg;
}
} else if( /(-p)|(--port)/i.test( item ) ) {
const arg = +args[ i + 1 ];
if( !arg ) {
console.error( 'Argument Error: -p, --port option must be a number' );
process.exit( 1 );
} else {
r.port = arg;
}
}
return r;
}, defaults
);
}
const
args = parseArguments( process.argv, {
silent: false,
port: 23000
} );
Object.keys( args )
.forEach(
k => process.env[ k ] = process.env[ k ] || args[ k ]
);
require( './server' )( require( './config' ) )
.initialize()
.then( inst => inst.start() );
} )();