battlelog/startDev.ts

42 lines
1.3 KiB
TypeScript

import { copy } from "https://deno.land/std@0.104.0/io/util.ts";
import { proxy } from "https://deno.land/x/oak_http_proxy@2.1.0/mod.ts";
import { fileExists } from "./lib/fileExists.ts";
import { Application, Context, Router } from "oak";
const app = new Application();
let port = 3001;
for await (const dirEntry of Deno.readDir(Deno.cwd())) {
if (!(dirEntry.isDirectory && dirEntry.name.includes('-service'))) continue;
const filename = dirEntry.name + '/index.ts';
const permfile = dirEntry.name + '/perms';
const prefixfile = dirEntry.name + '/prefix';
if (!(await fileExists(filename))) continue;
if (!(await fileExists(prefixfile))) continue;
const perms = (await fileExists(permfile)) ? await Deno.readTextFileSync(permfile).split('\n') : []
// successful, file or directory must exist
const p = Deno.run({
cmd: ['deno', 'run', ...perms, '--watch', filename, port.toString()],
stdout: "piped",
stderr: "piped",
})
const prefix = Deno.readTextFileSync(prefixfile);
const fixedPort = port;
const routes = new Router()
.all(`/${prefix}/(.*)`, proxy((ctx: Context) => `http://localhost:${fixedPort}${ctx.request.url.pathname}`, {}))
app.use(routes.routes());
app.use(routes.allowedMethods());
port++;
copy(p.stdout, Deno.stdout);
copy(p.stderr, Deno.stderr);
}
app.listen({
port: 3000
})