Support config flags

This commit is contained in:
Michaël Lemaire 2021-09-07 00:33:45 +02:00
parent 0a9301037e
commit 30bc3f689a
2 changed files with 25 additions and 5 deletions

View file

@ -7,3 +7,4 @@ export {
export type { Response as ServerResponse } from "https://deno.land/std@0.106.0/http/server.ts";
export { serveFile } from "https://deno.land/std@0.106.0/http/file_server.ts";
export { posix } from "https://deno.land/std@0.106.0/path/mod.ts";
export { readAll } from "https://deno.land/std@0.106.0/io/util.ts";

View file

@ -2,6 +2,7 @@
import {
listenAndServe,
posix,
readAll,
serveFile,
ServerRequest,
ServerResponse,
@ -117,17 +118,35 @@ async function run(...cmd: string[]): Promise<boolean> {
return result.success;
}
async function deno(cmd: string, ...args: string[]): Promise<boolean> {
const flags = await readConfigFlags(cmd);
return await run("deno", cmd, ...flags, ...args);
}
async function readConfigFlags(cmd: string): Promise<string[]> {
const path = `config/${cmd}.flags`;
try {
const f = await Sys.open(path);
const content = await readAll(f);
return new TextDecoder()
.decode(content)
.split(" ")
.filter((part) => part && part.startsWith("--"));
} catch {
}
return [];
}
async function bundle(
source: string,
destination: string,
): Promise<void> {
// TODO add config flags
if (
await run("deno", "bundle", source, destination) &&
await run("deno", "fmt", destination)
await deno("bundle", "-q", source, destination) &&
await deno("fmt", "-q", destination)
) {
return;
console.log(`Bundled : ${source} -> ${destination}`);
} else {
console.log(`Bundle failed for: ${source}`);
console.error(`Bundle failed for: ${source}`);
}
}