2023-08-01 10:19:39 -04:00
|
|
|
import { parse as JSONC } from "https://deno.land/x/jsonct@v0.1.0/mod.ts";
|
2023-08-01 17:23:19 -04:00
|
|
|
const RootFile = new URL(`file://${Deno.cwd().replaceAll("\\", "/")}`).toString();
|
|
|
|
const RootHost = import.meta.resolve("./");
|
|
|
|
|
2023-08-01 10:19:39 -04:00
|
|
|
export async function HuntConfig()
|
|
|
|
{
|
|
|
|
let path:string, resp:Response, text="", json;
|
|
|
|
try
|
|
|
|
{
|
2023-08-01 15:30:31 -04:00
|
|
|
path = "deno.json"
|
2023-08-01 17:23:19 -04:00
|
|
|
resp = await fetch(RootFile + "/" + path);
|
2023-08-01 10:19:39 -04:00
|
|
|
text = await resp.text();
|
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2023-08-01 15:30:31 -04:00
|
|
|
path = "deno.jsonc";
|
2023-08-01 17:23:19 -04:00
|
|
|
resp = await fetch(RootFile + "/" + path);
|
2023-08-01 10:19:39 -04:00
|
|
|
text = await resp.text();
|
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2023-08-01 17:23:19 -04:00
|
|
|
path = RootFile+"/.vscode/settings.json"
|
2023-08-01 10:19:39 -04:00
|
|
|
resp = await fetch(path);
|
|
|
|
json = await resp.json();
|
|
|
|
path = json["deno.config"];
|
2023-08-01 15:30:31 -04:00
|
|
|
json = undefined;
|
2023-08-01 10:19:39 -04:00
|
|
|
if(path)
|
|
|
|
{
|
2023-08-01 17:23:19 -04:00
|
|
|
resp = await fetch(RootFile + "/" + path);
|
2023-08-01 10:19:39 -04:00
|
|
|
text = await resp.text();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
|
|
|
path = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(text)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
json = JSONC(text);
|
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
|
|
|
// malformed json
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-01 15:30:31 -04:00
|
|
|
return {path, text, json};
|
|
|
|
}
|
|
|
|
|
2023-08-01 17:23:19 -04:00
|
|
|
export async function HuntImport()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-01 15:30:31 -04:00
|
|
|
async function Prompt(question: string):Promise<string>
|
|
|
|
{
|
|
|
|
const buf = new Uint8Array(1024);
|
|
|
|
await Deno.stdout.write(new TextEncoder().encode(question));
|
|
|
|
const bytes = await Deno.stdin.read(buf);
|
|
|
|
if (bytes) {
|
|
|
|
return new TextDecoder().decode(buf.subarray(0, bytes)).trim();
|
|
|
|
}
|
|
|
|
throw new Error("Unexpected end of input");
|
2023-08-01 10:19:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function SubProcess(args:string[])
|
|
|
|
{
|
|
|
|
const command = new Deno.Command(
|
|
|
|
`deno`,
|
|
|
|
{
|
|
|
|
args,
|
|
|
|
stdin: "piped",
|
|
|
|
stdout: "piped"
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const child = command.spawn();
|
|
|
|
|
|
|
|
// open a file and pipe the subprocess output to it.
|
|
|
|
const writableStream = new WritableStream({
|
|
|
|
write(chunk: Uint8Array): Promise<void> {
|
|
|
|
Deno.stdout.write(chunk);
|
|
|
|
return Promise.resolve();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
child.stdout.pipeTo(writableStream);
|
|
|
|
|
|
|
|
// manually close stdin
|
|
|
|
child.stdin.close();
|
|
|
|
const status = await child.status;
|
|
|
|
}
|
|
|
|
|
2023-08-01 17:23:19 -04:00
|
|
|
export async function Install(file:string, handler:(content:string)=>string = (s)=>s)
|
|
|
|
{
|
|
|
|
const pathFile = RootHost + "install__/" + file;
|
|
|
|
|
|
|
|
try{
|
|
|
|
const check = await Deno.readTextFile(Deno.cwd()+"/"+file);
|
|
|
|
const replace = await Prompt(`The file "${file}" already exists. Replace it? [y/n]`);
|
|
|
|
if(replace == "y")
|
|
|
|
{
|
|
|
|
throw("")
|
|
|
|
}
|
|
|
|
console.log(`Skipping "${file}" for now.`);
|
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
|
|
|
const resp = await fetch(pathFile);
|
|
|
|
const text = await resp.text();
|
|
|
|
await Deno.writeTextFile(Deno.cwd()+"/"+file, handler(text));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-01 15:30:31 -04:00
|
|
|
const config = await HuntConfig();
|
|
|
|
if(!config.path)
|
|
|
|
{
|
2023-08-01 17:23:19 -04:00
|
|
|
const pathServer = import.meta.resolve("./");
|
2023-08-01 15:30:31 -04:00
|
|
|
try
|
|
|
|
{
|
2023-08-01 17:23:19 -04:00
|
|
|
const resp1 = await Prompt("No Deno configuration found. Create one? [y/n]");
|
|
|
|
if(resp1 == "y")
|
2023-08-01 15:30:31 -04:00
|
|
|
{
|
2023-08-01 17:23:19 -04:00
|
|
|
const resp2 = await Prompt("Do you also want to add starter files? [y/n]");
|
|
|
|
let replaceApp = "./path/to/app.tsx";
|
|
|
|
let replaceApi = "./path/to/api.tsx";
|
|
|
|
let replaceCommentApp = "// (required) module with default export ()=>React.JSX.Element";
|
|
|
|
let replaceCommentApi = "// (optional) module with default export (req:Request, url:URL)=>Promise<Response|false>";
|
|
|
|
if(resp2 == "y")
|
|
|
|
{
|
|
|
|
replaceApp = "./app.tsx";
|
|
|
|
replaceApi = "./api.tsx";
|
|
|
|
replaceCommentApp = "";
|
|
|
|
replaceCommentApi = "";
|
2023-08-01 15:30:31 -04:00
|
|
|
|
2023-08-01 17:23:19 -04:00
|
|
|
await Install("app.tsx");
|
|
|
|
await Install("api.tsx");
|
|
|
|
}
|
2023-08-01 15:30:31 -04:00
|
|
|
|
2023-08-01 17:23:19 -04:00
|
|
|
await Install("deno.jsonc", (s)=>s
|
|
|
|
.replace("{{server}}", RootHost)
|
|
|
|
.replace("{{app}}", replaceApp)
|
|
|
|
.replace("{{api}}", replaceApi)
|
|
|
|
.replace("{{commentApp}}", replaceCommentApp)
|
|
|
|
.replace("{{commentApi}}", replaceCommentApi)
|
|
|
|
);
|
2023-08-01 15:30:31 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-01 17:23:19 -04:00
|
|
|
throw("Config declined.");
|
2023-08-01 15:30:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
|
|
|
console.log(e, "(Exiting...)");
|
|
|
|
Deno.exit();
|
|
|
|
}
|
2023-08-01 17:23:19 -04:00
|
|
|
}
|
|
|
|
else if(config.json)
|
|
|
|
{
|
|
|
|
|
2023-08-01 15:30:31 -04:00
|
|
|
}
|
|
|
|
console.log(config);
|