Skip to content

Commit

Permalink
feat(file_server): add help & switch to flags (denoland#3489)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhmushan authored and ry committed Dec 14, 2019
1 parent 8cf4704 commit d8e6030
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
51 changes: 36 additions & 15 deletions std/http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
// TODO Add tests like these:
// https://github.com/indexzero/http-server/blob/master/test/http-server-test.js

const { ErrorKind, DenoError, cwd, args, stat, readDir, open } = Deno;
const { ErrorKind, DenoError, args, stat, readDir, open, exit } = Deno;
import { posix } from "../path/mod.ts";
import {
listenAndServe,
ServerRequest,
setContentLength,
Response
} from "./server.ts";
import { parse } from "../flags/mod.ts";

interface EntryInfo {
mode: string;
Expand All @@ -22,22 +23,42 @@ interface EntryInfo {
name: string;
}

interface FileServerArgs {
_: string[];
// -p --port
p: number;
port: number;
// --cors
cors: boolean;
// -h --help
h: boolean;
help: boolean;
}

const encoder = new TextEncoder();
const serverArgs = args.slice();
let CORSEnabled = false;
// TODO: switch to flags if we later want to add more options
for (let i = 0; i < serverArgs.length; i++) {
if (serverArgs[i] === "--cors") {
CORSEnabled = true;
serverArgs.splice(i, 1);
break;
}

const serverArgs = parse(args) as FileServerArgs;

const CORSEnabled = serverArgs.cors ? true : false;
const target = posix.resolve(serverArgs._[1] || "");
const addr = `0.0.0.0:${serverArgs.port || serverArgs.p || 4500}`;

if (serverArgs.h || serverArgs.help) {
console.log(`Deno File Server
Serves a local directory in HTTP.
INSTALL:
deno install file_server https://deno.land/std/http/file_server.ts --allow-net --allow-read
USAGE:
file_server [path] [options]
OPTIONS:
-h, --help Prints help information
-p, --port <PORT> Set port
--cors Enable CORS via the "Access-Control-Allow-Origin" header`);
exit();
}
const targetArg = serverArgs[1] || "";
const target = posix.isAbsolute(targetArg)
? posix.normalize(targetArg)
: posix.join(cwd(), targetArg);
const addr = `0.0.0.0:${serverArgs[2] || 4500}`;

function modeToString(isDir: boolean, maybeMode: number | null): string {
const modeMap = ["---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"];
Expand Down
12 changes: 12 additions & 0 deletions std/http/file_server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,15 @@ test(async function servePermissionDenied(): Promise<void> {
deniedServer.stderr!.close();
}
});

test(async function printHelp(): Promise<void> {
const helpProcess = Deno.run({
args: [Deno.execPath(), "run", "http/file_server.ts", "--help"],
stdout: "piped"
});
const r = new TextProtoReader(new BufReader(helpProcess.stdout!));
const s = await r.readLine();
assert(s !== Deno.EOF && s.includes("Deno File Server"));
helpProcess.close();
helpProcess.stdout!.close();
});

0 comments on commit d8e6030

Please sign in to comment.