mirror of
https://github.com/denoland/deno.git
synced 2024-12-28 18:19:08 -05:00
feat(file_server): add help & switch to flags (#3489)
This commit is contained in:
parent
8cf470474f
commit
d8e60309d2
2 changed files with 49 additions and 16 deletions
|
@ -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 <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"];
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue