battlelog/startDev.ts

42 lines
1.3 KiB
TypeScript

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();
for await (const dirEntry of Deno.readDir(Deno.cwd())) {
if (!(dirEntry.isDirectory && dirEntry.name.includes('-service'))) continue;
const filename = dirEntry.name + '/main.ts';
const permfile = dirEntry.name + '/perms';
const prefixfile = dirEntry.name + '/prefix';
const portfile = dirEntry.name + '/port';
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 port = await Deno.readTextFile(portfile);
const p = new Deno.Command(Deno.execPath(), {
args: [
'run',
...perms,
'--watch',
filename,
port
]
}).spawn()
const prefix = Deno.readTextFileSync(prefixfile);
const routes = new Router()
.all(prefix ? `/${prefix}/(.*)` : '/(.*)', proxy((ctx: Context) => `http://localhost:${port}${ctx.request.url.pathname}`))
app.use(routes.allowedMethods());
app.use(routes.routes());
}
app.listen({
port: 3000
})