From d8e60309d2eed482a3a4c896e68a4a97e7f7b0d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=A8=E6=9D=89?= Date: Sat, 14 Dec 2019 10:01:32 +0800 Subject: [PATCH] feat(file_server): add help & switch to flags (#3489) --- std/http/file_server.ts | 53 +++++++++++++++++++++++++----------- std/http/file_server_test.ts | 12 ++++++++ 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/std/http/file_server.ts b/std/http/file_server.ts index 40a18bd8a5..729aa97250 100755 --- a/std/http/file_server.ts +++ b/std/http/file_server.ts @@ -6,7 +6,7 @@ // 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, @@ -14,6 +14,7 @@ import { setContentLength, Response } from "./server.ts"; +import { parse } from "../flags/mod.ts"; interface EntryInfo { mode: string; @@ -22,22 +23,42 @@ interface EntryInfo { name: string; } -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; - } +interface FileServerArgs { + _: string[]; + // -p --port + p: number; + port: number; + // --cors + cors: boolean; + // -h --help + h: boolean; + help: boolean; +} + +const encoder = new TextEncoder(); + +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 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"]; diff --git a/std/http/file_server_test.ts b/std/http/file_server_test.ts index 85785d923e..b274b3f08e 100644 --- a/std/http/file_server_test.ts +++ b/std/http/file_server_test.ts @@ -119,3 +119,15 @@ test(async function servePermissionDenied(): Promise { deniedServer.stderr!.close(); } }); + +test(async function printHelp(): Promise { + 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(); +});