Skip to content

Commit

Permalink
redid build sys
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Oct 1, 2021
1 parent 3160d14 commit c954562
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 126 deletions.
114 changes: 0 additions & 114 deletions bin/main.js

This file was deleted.

54 changes: 54 additions & 0 deletions bin/srvd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env node

const http = require("http");

const serveStatic = require("serve-static");
const finalhandler = require("finalhandler");

const DEFAULT_PORT = 8080;

function serve ({ acceptRanges = true, debug = false, root, port } = { acceptRanges: true }) {
// console.log(arguments);
if (!root) {
root = process.cwd();
if (debug) console.log(`[srvd] root not set so using current working directory "${root}"`);
}

if (!port) port = process.env.SRVD_DEFAULT_PORT ? process.env.SRVD_DEFAULT_PORT : DEFAULT_PORT;
port = parseInt(port);

const serve = serveStatic(root, {
acceptRanges
});

const server = http.createServer(function onRequest(req, res) {
serve(req, res, finalhandler(req, res));
});

server.listen(port);
if (debug) console.log("[srvd] serving on port " + port);

function checkForCloseRequest() {
if (["TRUE","True","true"].includes(process.env.SRVD_PLZ_CLOSE)) {
server.close();
} else {
setTimeout(() => checkForCloseRequest(), 500);
}
}
setTimeout(() => checkForCloseRequest(), 500);

return { acceptRanges, debug, server, port, root };
};

module.exports = { serve };

if (require.main === module) {
const args = Array.from(process.argv);
const str = args.join(" ");

serve({
debug: !!str.match(/-?-debug((=|== )(true|True|TRUE))?/),
port: Array.prototype.slice.call(str.match(/-?-port(?:=|== )(\d+)/) || [], 1)[0],
root: Array.prototype.slice.call(str.match(/-?-root(?:=|== )([^ ]+)/) || [], 1)[0]
});
}
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{
"name": "srvd",
"version": "0.0.2",
"version": "0.0.1",
"description": "Another Development Server. Supports Range Requests. Configure through Environmental Variables.",
"main": "srvd.js",
"bin": {
"srvd": "bin/main.js"
"srvd": "bin/srvd.js"
},
"files": [
"bin/main.js",
"srvd.js"
"srvd.js",
"bin/srvd.js"
],
"scripts": {
"build": "npx webpack build --entry=$PWD/srvd.js --mode=production --no-devtool --output-path=bin --target=node",
"build": "mkdir -p bin && echo \"#!/usr/bin/env node\n\" > ./bin/srvd.js && cat srvd.js >> ./bin/srvd.js",
"format": "npx prettier --arrow-parens=avoid --print-width=120 --trailing-comma=none --write srvd.js test.js",
"test": "node test.js"
},
"repository": {
Expand Down
8 changes: 4 additions & 4 deletions srvd.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const finalhandler = require("finalhandler");

const DEFAULT_PORT = 8080;

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

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

return { acceptRanges, debug, server, port, root };
};
}

module.exports = { serve };

if (require.main === module) {
const args = Array.from(process.argv);
const str = args.join(" ");
const str = args.join(" ");

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

0 comments on commit c954562

Please sign in to comment.