diff options
author | 木杉 <zhmushan@qq.com> | 2019-12-14 10:01:32 +0800 |
---|---|---|
committer | Ry Dahl <ry@tinyclouds.org> | 2019-12-14 10:01:32 +0800 |
commit | d8e60309d2eed482a3a4c896e68a4a97e7f7b0d7 (patch) | |
tree | 3f948a4426bd9c1173dc9db0a23a516e8aa68035 | |
parent | 8cf470474f34102871051e75ad5cb3bb457e74d1 (diff) |
feat(file_server): add help & switch to flags (#3489)
-rwxr-xr-x | std/http/file_server.ts | 51 | ||||
-rw-r--r-- | std/http/file_server_test.ts | 12 |
2 files changed, 48 insertions, 15 deletions
diff --git a/std/http/file_server.ts b/std/http/file_server.ts index 40a18bd8a..729aa9725 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; } +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"]; diff --git a/std/http/file_server_test.ts b/std/http/file_server_test.ts index 85785d923..b274b3f08 100644 --- a/std/http/file_server_test.ts +++ b/std/http/file_server_test.ts @@ -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(); +}); |