Skip to content

Commit 8cbd8ac

Browse files
authored
Merge pull request #1 from xenikopa/server
add server script
2 parents a0d6cfb + 9add4cf commit 8cbd8ac

File tree

5 files changed

+152
-9
lines changed

5 files changed

+152
-9
lines changed

choose-script.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ const path = require('path');
44
const fs = require('fs');
55
const scriptsDir = path.join(__dirname, 'scripts');
66
const readline = require('readline');
7+
const color = require('./scripts/helpers/getColor');
78

8-
const colorCyan = "\x1b[36m%s\x1b[0m";
9-
const colorPurple = "\x1b[35m%s\x1b[0m";
10-
const colorRed = "\x1b[31m%s\x1b[0m";
119

1210
void function(){
1311
let messageEmptyDir = () => {
14-
console.log(colorRed, 'Sorry, there are no scripts in "scripts" directory.')
15-
console.log(colorCyan, 'Change it and create first script on javascript in "scripts" dir!');
12+
console.log(color('red'), 'Sorry, there are no scripts in "scripts" directory.')
13+
console.log(color('cyan'), 'Change it and create first script on javascript in "scripts" dir!');
1614
}
1715

1816
fs.readdir(scriptsDir, (err, files) => {
@@ -25,12 +23,12 @@ void function(){
2523
if (jsFiles.length === 0) {
2624
messageEmptyDir();
2725
} else {
28-
console.log(colorPurple, '> Choose script to run:');
26+
console.log(color('purple'), '> Choose script to run:');
2927

3028
jsFiles.forEach((file, index) => {
31-
console.log(colorCyan, `[${index}]: ${file}`);
29+
console.log(color('cyan'), `[${index}]: ${file}`);
3230
});
33-
console.log(colorRed, `[!]: exit`)
31+
console.log(color('red'), `[!]: exit`)
3432

3533
execScript(jsFiles);
3634
}
@@ -63,7 +61,7 @@ let execScript = (files) => {
6361
try {
6462
require(`${__dirname}/scripts/${fileName}`)
6563
} catch (err) {
66-
console.log(colorRed, err);
64+
console.log(color('red'), err.message);
6765
}
6866
}
6967

scripts/helpers/getColor.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"use strict";
2+
3+
let pipe = require('./pipe.js')
4+
5+
/**
6+
*
7+
* @param {string} colorCode
8+
*/
9+
let getColor = (colorCode) => `\x1b[${colorCode}%s\x1b[0m`;
10+
11+
/**
12+
*
13+
* @param {string} colorName
14+
*/
15+
let getColorCode = (colorName) => {
16+
switch(colorName) {
17+
case 'cyan': return '36m';
18+
case 'purple': return '35m';
19+
case 'red': return '31m';
20+
default: return '30m'; //black
21+
}
22+
}
23+
24+
25+
26+
module.exports = pipe(
27+
getColorCode,
28+
getColor
29+
)

scripts/helpers/pipe.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
module.exports = (...funcs) => v => {
3+
return funcs.reduce((res, func) => {
4+
return func(res);
5+
}, v);
6+
};

scripts/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
Test
10+
</body>
11+
</html>

scripts/server.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
"use strict";
2+
3+
const http = require('http');
4+
const fs = require('fs');
5+
const pipe = require('./helpers/pipe');
6+
const color = require('./helpers/getColor');
7+
8+
/**
9+
*
10+
* @param {function} callback
11+
* @param {number} port
12+
*/
13+
let createServer = (callback) => (port) => http
14+
.createServer(callback)
15+
.on('error', (err) => console.log(color('red'), `Error! ${err}`))
16+
.on('listening', () => {
17+
console.log(`Listening on ${port}`);
18+
console.log(`Click to open to link http://localhost:${port}`)
19+
});
20+
21+
/**
22+
*
23+
* @param {http responce} response
24+
*/
25+
let getStream = response => filename => fs
26+
.createReadStream(filename)
27+
.on('error', () => {
28+
console.log(color('red'), "Error! Can't read file 'index.html'. Check, that file is exists & correct.");
29+
response.end();
30+
});
31+
32+
let serverCallback = (filename) => (request, response) => getStream(response)(filename).pipe(response);
33+
34+
let showHelp = () => void console.log(`
35+
Usage: node server [options]
36+
37+
Options:
38+
39+
--port set port for server. Default value: 8080. Example: --port 3000.
40+
--file set file name for start page. Default value: index.html. Example: --file start.html
41+
--help, -h output usage information
42+
`);
43+
44+
/**
45+
*
46+
* @param {process.argv} agrv
47+
*/
48+
let getPort = pipe(
49+
agrv => agrv.findIndex(x => x === '--port'),
50+
index => index === -1 ? null : Number(agrv[index + 1]),
51+
port => {
52+
if (isNaN(port)){
53+
console.error(color('red'), 'Port must be a Number. For more information use --help');
54+
process.exit(1);
55+
}
56+
return port || 3000;
57+
}
58+
);
59+
60+
/**
61+
*
62+
* @param {process.argv} agrv
63+
*/
64+
let getFile = pipe(
65+
argv => argv.findIndex(x => x === '--file'),
66+
index => index === -1 ? null : argv[index + 1],
67+
filename => {
68+
if (filename != null && !new RegExp(/\.html/g).test(filename)) {
69+
console.error(color('red'), 'Needs `html` file. For more information use --help.');
70+
process.exit(1);
71+
}
72+
return filename || 'index.html'
73+
}
74+
);
75+
76+
let getHelp = pipe(
77+
argv => argv.findIndex(x => x === '--help' || x === '-h'),
78+
index => {
79+
if (index === -1) { return; }
80+
showHelp();
81+
process.exit(0);
82+
}
83+
);
84+
85+
void function() {
86+
let argv = process.argv.slice(2);
87+
88+
getHelp(argv);
89+
90+
let port = getPort(argv);
91+
92+
let server = pipe(
93+
getFile,
94+
serverCallback,
95+
createServer,
96+
)(argv)
97+
98+
server(port).listen(port)
99+
}()

0 commit comments

Comments
 (0)