1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-29 10:39:10 -05:00

feat(file_server): add help & switch to flags (#3489)

This commit is contained in:
木杉 2019-12-14 10:01:32 +08:00 committed by Ry Dahl
parent 8cf470474f
commit d8e60309d2
2 changed files with 49 additions and 16 deletions

View file

@ -6,7 +6,7 @@
// TODO Add tests like these: // TODO Add tests like these:
// https://github.com/indexzero/http-server/blob/master/test/http-server-test.js // 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 { posix } from "../path/mod.ts";
import { import {
listenAndServe, listenAndServe,
@ -14,6 +14,7 @@ import {
setContentLength, setContentLength,
Response Response
} from "./server.ts"; } from "./server.ts";
import { parse } from "../flags/mod.ts";
interface EntryInfo { interface EntryInfo {
mode: string; mode: string;
@ -22,22 +23,42 @@ interface EntryInfo {
name: string; name: string;
} }
const encoder = new TextEncoder(); interface FileServerArgs {
const serverArgs = args.slice(); _: string[];
let CORSEnabled = false; // -p --port
// TODO: switch to flags if we later want to add more options p: number;
for (let i = 0; i < serverArgs.length; i++) { port: number;
if (serverArgs[i] === "--cors") { // --cors
CORSEnabled = true; cors: boolean;
serverArgs.splice(i, 1); // -h --help
break; 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 <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 { function modeToString(isDir: boolean, maybeMode: number | null): string {
const modeMap = ["---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"]; const modeMap = ["---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"];

View file

@ -119,3 +119,15 @@ test(async function servePermissionDenied(): Promise<void> {
deniedServer.stderr!.close(); 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();
});