Skip to content

Commit c954562

Browse files
committed
redid build sys
1 parent 3160d14 commit c954562

File tree

5 files changed

+67
-126
lines changed

5 files changed

+67
-126
lines changed

bin/main.js

Lines changed: 0 additions & 114 deletions
This file was deleted.

bin/srvd.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env node
2+
3+
const http = require("http");
4+
5+
const serveStatic = require("serve-static");
6+
const finalhandler = require("finalhandler");
7+
8+
const DEFAULT_PORT = 8080;
9+
10+
function serve ({ acceptRanges = true, debug = false, root, port } = { acceptRanges: true }) {
11+
// console.log(arguments);
12+
if (!root) {
13+
root = process.cwd();
14+
if (debug) console.log(`[srvd] root not set so using current working directory "${root}"`);
15+
}
16+
17+
if (!port) port = process.env.SRVD_DEFAULT_PORT ? process.env.SRVD_DEFAULT_PORT : DEFAULT_PORT;
18+
port = parseInt(port);
19+
20+
const serve = serveStatic(root, {
21+
acceptRanges
22+
});
23+
24+
const server = http.createServer(function onRequest(req, res) {
25+
serve(req, res, finalhandler(req, res));
26+
});
27+
28+
server.listen(port);
29+
if (debug) console.log("[srvd] serving on port " + port);
30+
31+
function checkForCloseRequest() {
32+
if (["TRUE","True","true"].includes(process.env.SRVD_PLZ_CLOSE)) {
33+
server.close();
34+
} else {
35+
setTimeout(() => checkForCloseRequest(), 500);
36+
}
37+
}
38+
setTimeout(() => checkForCloseRequest(), 500);
39+
40+
return { acceptRanges, debug, server, port, root };
41+
};
42+
43+
module.exports = { serve };
44+
45+
if (require.main === module) {
46+
const args = Array.from(process.argv);
47+
const str = args.join(" ");
48+
49+
serve({
50+
debug: !!str.match(/-?-debug((=|== )(true|True|TRUE))?/),
51+
port: Array.prototype.slice.call(str.match(/-?-port(?:=|== )(\d+)/) || [], 1)[0],
52+
root: Array.prototype.slice.call(str.match(/-?-root(?:=|== )([^ ]+)/) || [], 1)[0]
53+
});
54+
}

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
{
22
"name": "srvd",
3-
"version": "0.0.2",
3+
"version": "0.0.1",
44
"description": "Another Development Server. Supports Range Requests. Configure through Environmental Variables.",
55
"main": "srvd.js",
66
"bin": {
7-
"srvd": "bin/main.js"
7+
"srvd": "bin/srvd.js"
88
},
99
"files": [
10-
"bin/main.js",
11-
"srvd.js"
10+
"srvd.js",
11+
"bin/srvd.js"
1212
],
1313
"scripts": {
14-
"build": "npx webpack build --entry=$PWD/srvd.js --mode=production --no-devtool --output-path=bin --target=node",
14+
"build": "mkdir -p bin && echo \"#!/usr/bin/env node\n\" > ./bin/srvd.js && cat srvd.js >> ./bin/srvd.js",
15+
"format": "npx prettier --arrow-parens=avoid --print-width=120 --trailing-comma=none --write srvd.js test.js",
1516
"test": "node test.js"
1617
},
1718
"repository": {

srvd.js

100644100755
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const finalhandler = require("finalhandler");
55

66
const DEFAULT_PORT = 8080;
77

8-
function serve ({ acceptRanges = true, debug = false, root, port } = { acceptRanges: true }) {
8+
function serve({ acceptRanges = true, debug = false, root, port } = { acceptRanges: true }) {
99
// console.log(arguments);
1010
if (!root) {
1111
root = process.cwd();
@@ -27,7 +27,7 @@ function serve ({ acceptRanges = true, debug = false, root, port } = { acceptRan
2727
if (debug) console.log("[srvd] serving on port " + port);
2828

2929
function checkForCloseRequest() {
30-
if (["TRUE","True","true"].includes(process.env.SRVD_PLZ_CLOSE)) {
30+
if (["TRUE", "True", "true"].includes(process.env.SRVD_PLZ_CLOSE)) {
3131
server.close();
3232
} else {
3333
setTimeout(() => checkForCloseRequest(), 500);
@@ -36,13 +36,13 @@ function serve ({ acceptRanges = true, debug = false, root, port } = { acceptRan
3636
setTimeout(() => checkForCloseRequest(), 500);
3737

3838
return { acceptRanges, debug, server, port, root };
39-
};
39+
}
4040

4141
module.exports = { serve };
4242

4343
if (require.main === module) {
4444
const args = Array.from(process.argv);
45-
const str = args.join(" ");
45+
const str = args.join(" ");
4646

4747
serve({
4848
debug: !!str.match(/-?-debug((=|== )(true|True|TRUE))?/),

0 commit comments

Comments
 (0)