Skip to content

Commit 236d560

Browse files
committed
Merge branch 'dev-cli' into trunk
Signed-off-by: Hrishikesh Patil <[email protected]>
2 parents 850bb0c + 2e46ada commit 236d560

File tree

3 files changed

+245
-2
lines changed

3 files changed

+245
-2
lines changed

.cli-dev.js

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
const http = require('http');
8+
const path = require('path');
9+
const broadcastemCore = require('./index');
10+
const fs = require('fs');
11+
const os = require('os');
12+
13+
let server, interfaceTimer, addressCount;
14+
15+
/**
16+
* Accept CLI arguments
17+
*/
18+
const yargs = require('yargs');
19+
const argv = yargs
20+
.options({
21+
port: {
22+
alias: 'p',
23+
describe: 'the port to bind to on this machine',
24+
default: process.env.PORT || 3000,
25+
group: 'Configuration:',
26+
type: 'number',
27+
requiresArg: true,
28+
},
29+
list: {
30+
alias: 'L',
31+
describe:
32+
'a list file which has the paths to the files you want to share',
33+
group: 'Configuration:',
34+
type: 'string',
35+
requiresArg: true,
36+
},
37+
destination: {
38+
alias: 'd',
39+
describe: 'folder where incoming files will be saved',
40+
default: path.resolve(
41+
os.homedir(),
42+
'Downloads',
43+
'Broadcastem Received'
44+
),
45+
group: 'Configuration:',
46+
type: 'string',
47+
requiresArg: true,
48+
},
49+
'logging-level': {
50+
alias: ['l', 'log'],
51+
describe: `one of the following logging levels
52+
0 - do not log anything
53+
1 - log only errors (Response codes > 400)
54+
2 - log all requests`,
55+
default: 0,
56+
group: 'Configuration:',
57+
type: 'number',
58+
requiresArg: true,
59+
},
60+
'core-version': {
61+
alias: 'c',
62+
describe: `print version of the core module and exit`,
63+
type: 'boolean',
64+
},
65+
})
66+
.check(argv => {
67+
if (isNaN(argv.port)) throw new Error('Port should be a number');
68+
else if (argv.port <= 0) throw new Error('Port should be positive');
69+
else if (argv.list && !fs.existsSync(argv.list))
70+
throw new Error(`Specified file ${argv.list} doesnt exist`);
71+
else if (isNaN(argv.loggingLevel))
72+
throw new Error('Logging level should be a number');
73+
else if (argv.loggingLevel < 0 || argv.loggingLevel > 2)
74+
throw new Error('Invalid logging level');
75+
else if (!argv._.every(path => fs.existsSync(path)))
76+
throw new Error(
77+
'All the files to be shared must be present on disk'
78+
);
79+
return true;
80+
})
81+
.wrap(yargs.terminalWidth()).argv;
82+
// Since we are taking file paths as the remaining input, pass it on to files property for easy reading
83+
argv.files = argv._;
84+
argv.restart = true;
85+
86+
if (argv.coreVersion) {
87+
console.log(
88+
require(path.resolve(
89+
require.resolve('broadcastem-core'),
90+
'..',
91+
'package.json'
92+
)).version
93+
);
94+
process.exit(0);
95+
}
96+
97+
/**
98+
* Event listener for HTTP server "error" event.
99+
*/
100+
101+
function onError(error) {
102+
switch (error.code) {
103+
case 'EACCES':
104+
console.error(argv.port + ' requires elevated privileges');
105+
process.exit(1);
106+
break;
107+
case 'EADDRINUSE':
108+
console.error(argv.port + ' is already in use');
109+
process.exit(1);
110+
break;
111+
default:
112+
throw error;
113+
}
114+
}
115+
116+
/**
117+
* Print the port to the console
118+
*/
119+
function onListening() {
120+
console.log(`Listening on ${argv.port}`);
121+
}
122+
123+
/**
124+
* Start Express app from the CLI flags
125+
*/
126+
127+
broadcastemCore
128+
.init(argv)
129+
.then(app => {
130+
/**
131+
* Create HTTP server.
132+
*/
133+
134+
server = http.createServer(app);
135+
136+
/**
137+
* Listen on provided port, on all network interfaces.
138+
*/
139+
140+
server.listen(argv.port);
141+
server.on('error', onError);
142+
server.on('listening', onListening);
143+
})
144+
.catch(err => {
145+
console.error(err);
146+
process.exit(1);
147+
})
148+
.finally(() => {
149+
process.on('SIGINT', function () {
150+
if (server.listening) {
151+
clearTimeout(interfaceTimer);
152+
server.close();
153+
}
154+
console.log('Shutting down server');
155+
process.exit();
156+
});
157+
});

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
],
1212
"main": "index.js",
1313
"scripts": {
14+
"dev:cli": "node .cli-dev.js",
1415
"test:Startup": "node tests/Startup/index.js",
1516
"test:API": "node tests/API/index.js"
1617
},
@@ -36,6 +37,7 @@
3637
"husky": "^4.3.6",
3738
"mocha": "^8.2.1",
3839
"mocha-junit-reporter": "^2.0.0",
39-
"prettier": "^2.2.1"
40+
"prettier": "^2.2.1",
41+
"yargs": "^16.2.0"
4042
}
4143
}

yarn.lock

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,25 @@ ansi-regex@^4.1.0:
8484
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-4.1.0.tgz#8b9f8f08cf1acb843756a839ca8c7e3168c51997"
8585
integrity sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==
8686

87+
ansi-regex@^5.0.0:
88+
version "5.0.0"
89+
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-5.0.0.tgz#388539f55179bf39339c81af30a654d69f87cb75"
90+
integrity sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg==
91+
8792
ansi-styles@^3.2.0, ansi-styles@^3.2.1:
8893
version "3.2.1"
8994
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d"
9095
integrity sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==
9196
dependencies:
9297
color-convert "^1.9.0"
9398

99+
ansi-styles@^4.0.0:
100+
version "4.3.0"
101+
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-4.3.0.tgz#edd803628ae71c04c85ae7a0906edad34b648937"
102+
integrity sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==
103+
dependencies:
104+
color-convert "^2.0.1"
105+
94106
ansi-styles@^4.1.0:
95107
version "4.2.1"
96108
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-4.2.1.tgz#90ae75c424d008d2624c5bf29ead3177ebfcf359"
@@ -340,6 +352,15 @@ cliui@^5.0.0:
340352
strip-ansi "^5.2.0"
341353
wrap-ansi "^5.1.0"
342354

355+
cliui@^7.0.2:
356+
version "7.0.4"
357+
resolved "https://registry.yarnpkg.com/cliui/-/cliui-7.0.4.tgz#a0265ee655476fc807aea9df3df8df7783808b4f"
358+
integrity sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ==
359+
dependencies:
360+
string-width "^4.2.0"
361+
strip-ansi "^6.0.0"
362+
wrap-ansi "^7.0.0"
363+
343364
color-convert@^1.9.0:
344365
version "1.9.3"
345366
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.3.tgz#bb71850690e1f136567de629d2d5471deda4c1e8"
@@ -544,6 +565,11 @@ emoji-regex@^7.0.1:
544565
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-7.0.3.tgz#933a04052860c85e83c122479c4748a8e4c72156"
545566
integrity sha512-CwBLREIQ7LvYFB0WyRvwhq5N5qPhc6PMjD6bYggFlI5YyDgl+0vxq5VHbMOFqLg7hfWzmu8T5Z1QofhmTIhItA==
546567

568+
emoji-regex@^8.0.0:
569+
version "8.0.0"
570+
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-8.0.0.tgz#e818fd69ce5ccfcb404594f842963bf53164cc37"
571+
integrity sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==
572+
547573
encodeurl@~1.0.2:
548574
version "1.0.2"
549575
resolved "https://registry.yarnpkg.com/encodeurl/-/encodeurl-1.0.2.tgz#ad3ff4c86ec2d029322f5a02c3a9a606c95b3f59"
@@ -561,6 +587,11 @@ es6-promise@^3.1.2:
561587
resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-3.3.1.tgz#a08cdde84ccdbf34d027a1451bc91d4bcd28a613"
562588
integrity sha1-oIzd6EzNvzTQJ6FFG8kdS80ophM=
563589

590+
escalade@^3.1.1:
591+
version "3.1.1"
592+
resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.1.1.tgz#d8cfdc7000965c5a0174b4a82eaa5c0552742e40"
593+
integrity sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==
594+
564595
escape-html@~1.0.3:
565596
version "1.0.3"
566597
resolved "https://registry.yarnpkg.com/escape-html/-/escape-html-1.0.3.tgz#0258eae4d3d0c0974de1c169188ef0051d1d1988"
@@ -739,7 +770,7 @@ fsevents@~2.1.2:
739770
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.1.3.tgz#fb738703ae8d2f9fe900c33836ddebee8b97f23e"
740771
integrity sha512-Auw9a4AxqWpa9GUfj370BMPzzyncfBABW8Mab7BGWBYDj4Isgq+cDKtx0i6u9jcX9pQDnswsaaOTgTmA5pEjuQ==
741772

742-
get-caller-file@^2.0.1:
773+
get-caller-file@^2.0.1, get-caller-file@^2.0.5:
743774
version "2.0.5"
744775
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-2.0.5.tgz#4f94412a82db32f36e3b0b9741f8a97feb031f7e"
745776
integrity sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==
@@ -906,6 +937,11 @@ is-fullwidth-code-point@^2.0.0:
906937
resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz#a3b30a5c4f199183167aaab93beefae3ddfb654f"
907938
integrity sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=
908939

940+
is-fullwidth-code-point@^3.0.0:
941+
version "3.0.0"
942+
resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz#f116f8064fe90b3f7844a38997c0b75051269f1d"
943+
integrity sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==
944+
909945
is-glob@^4.0.1, is-glob@~4.0.1:
910946
version "4.0.1"
911947
resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-4.0.1.tgz#7567dbe9f2f5e2467bc77ab83c4a29482407a5dc"
@@ -1487,6 +1523,15 @@ string-width@^3.0.0, string-width@^3.1.0:
14871523
is-fullwidth-code-point "^2.0.0"
14881524
strip-ansi "^5.1.0"
14891525

1526+
string-width@^4.1.0, string-width@^4.2.0:
1527+
version "4.2.0"
1528+
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.0.tgz#952182c46cc7b2c313d1596e623992bd163b72b5"
1529+
integrity sha512-zUz5JD+tgqtuDjMhwIg5uFVV3dtqZ9yQJlZVfq4I01/K5Paj5UHj7VyrQOJvzawSVlKpObApbfD0Ed6yJc+1eg==
1530+
dependencies:
1531+
emoji-regex "^8.0.0"
1532+
is-fullwidth-code-point "^3.0.0"
1533+
strip-ansi "^6.0.0"
1534+
14901535
string_decoder@~1.1.1:
14911536
version "1.1.1"
14921537
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.1.1.tgz#9cf1611ba62685d7030ae9e4ba34149c3af03fc8"
@@ -1508,6 +1553,13 @@ strip-ansi@^5.0.0, strip-ansi@^5.1.0, strip-ansi@^5.2.0:
15081553
dependencies:
15091554
ansi-regex "^4.1.0"
15101555

1556+
strip-ansi@^6.0.0:
1557+
version "6.0.0"
1558+
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.0.tgz#0b1571dd7669ccd4f3e06e14ef1eed26225ae532"
1559+
integrity sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==
1560+
dependencies:
1561+
ansi-regex "^5.0.0"
1562+
15111563
15121564
version "3.1.1"
15131565
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-3.1.1.tgz#31f1281b3832630434831c310c01cccda8cbe006"
@@ -1649,6 +1701,15 @@ wrap-ansi@^5.1.0:
16491701
string-width "^3.0.0"
16501702
strip-ansi "^5.0.0"
16511703

1704+
wrap-ansi@^7.0.0:
1705+
version "7.0.0"
1706+
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
1707+
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
1708+
dependencies:
1709+
ansi-styles "^4.0.0"
1710+
string-width "^4.1.0"
1711+
strip-ansi "^6.0.0"
1712+
16521713
wrappy@1:
16531714
version "1.0.2"
16541715
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
@@ -1664,6 +1725,11 @@ y18n@^4.0.0:
16641725
resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.0.tgz#95ef94f85ecc81d007c264e190a120f0a3c8566b"
16651726
integrity sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==
16661727

1728+
y18n@^5.0.5:
1729+
version "5.0.5"
1730+
resolved "https://registry.yarnpkg.com/y18n/-/y18n-5.0.5.tgz#8769ec08d03b1ea2df2500acef561743bbb9ab18"
1731+
integrity sha512-hsRUr4FFrvhhRH12wOdfs38Gy7k2FFzB9qgN9v3aLykRq0dRcdcpz5C9FxdS2NuhOrI/628b/KSTJ3rwHysYSg==
1732+
16671733
yaml@^1.10.0:
16681734
version "1.10.0"
16691735
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.0.tgz#3b593add944876077d4d683fee01081bd9fff31e"
@@ -1677,6 +1743,11 @@ [email protected], yargs-parser@^13.1.2:
16771743
camelcase "^5.0.0"
16781744
decamelize "^1.2.0"
16791745

1746+
yargs-parser@^20.2.2:
1747+
version "20.2.4"
1748+
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.4.tgz#b42890f14566796f85ae8e3a25290d205f154a54"
1749+
integrity sha512-WOkpgNhPTlE73h4VFAFsOnomJVaovO8VqLDzy5saChRBFQFBoMYirowyW+Q9HB4HFF4Z7VZTiG3iSzJJA29yRA==
1750+
16801751
16811752
version "2.0.0"
16821753
resolved "https://registry.yarnpkg.com/yargs-unparser/-/yargs-unparser-2.0.0.tgz#f131f9226911ae5d9ad38c432fe809366c2325eb"
@@ -1703,6 +1774,19 @@ [email protected]:
17031774
y18n "^4.0.0"
17041775
yargs-parser "^13.1.2"
17051776

1777+
yargs@^16.2.0:
1778+
version "16.2.0"
1779+
resolved "https://registry.yarnpkg.com/yargs/-/yargs-16.2.0.tgz#1c82bf0f6b6a66eafce7ef30e376f49a12477f66"
1780+
integrity sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==
1781+
dependencies:
1782+
cliui "^7.0.2"
1783+
escalade "^3.1.1"
1784+
get-caller-file "^2.0.5"
1785+
require-directory "^2.1.1"
1786+
string-width "^4.2.0"
1787+
y18n "^5.0.5"
1788+
yargs-parser "^20.2.2"
1789+
17061790
zip-stream@^1.0.0:
17071791
version "1.2.0"
17081792
resolved "https://registry.yarnpkg.com/zip-stream/-/zip-stream-1.2.0.tgz#a8bc45f4c1b49699c6b90198baacaacdbcd4ba04"

0 commit comments

Comments
 (0)