use deno confirm

This commit is contained in:
Seth Trowbridge 2023-08-02 06:07:34 -04:00
parent c558ecb6d2
commit 29768761cb

50
cli.tsx
View File

@ -82,17 +82,6 @@ export async function HuntConfig()
return [{path, text, json}, imports] as ConfigCheckPair return [{path, text, json}, imports] as ConfigCheckPair
} }
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");
}
export async function SubProcess(args:string[]) export async function SubProcess(args:string[])
{ {
const command = new Deno.Command( const command = new Deno.Command(
@ -126,12 +115,12 @@ export async function Install(file:string, handler:(content:string)=>string = (s
try{ try{
const check = await Deno.readTextFile(Deno.cwd()+"/"+file); const check = await Deno.readTextFile(Deno.cwd()+"/"+file);
const replace = await Prompt(`The file "${file}" already exists. Replace it? [y/n]`); const replace = confirm(`⚠️🚧 The file "${file}" already exists. Replace it?`);
if(replace == "y") if(replace)
{ {
throw("") throw("")
} }
console.log(`Skipping "${file}" for now.`); console.log(`Using pre-existing "${file}" for now.`);
} }
catch(e) catch(e)
{ {
@ -141,21 +130,22 @@ export async function Install(file:string, handler:(content:string)=>string = (s
} }
} }
console.info(`👷 Checking your project`)
try try
{ {
let [config, imports] = await HuntConfig(); let [config, imports] = await HuntConfig();
console.log(`1) Checking for Deno configuration...`)
if(!config.path) if(!config.path)
{ {
const resp1 = await Prompt(" ! No Deno configuration found. Create one? [y/n]"); //const resp1 = await Prompt(" ! No Deno configuration found. Create one? [y/n]");
if(resp1 == "y") const resp1 = confirm("🚨🚧 No Deno configuration found. Create one?");
if(resp1)
{ {
const resp2 = await Prompt(" ? Do you also want to add starter files? [y/n]"); const resp2 = confirm("⚠️🚧 Do you also want to add starter files?");
let replaceApp = "./path/to/app.tsx"; let replaceApp = "./path/to/app.tsx";
let replaceApi = "./path/to/api.tsx"; let replaceApi = "./path/to/api.tsx";
let replaceCommentApp = "// (required) module with default export ()=>React.JSX.Element"; let replaceCommentApp = "// (required) module with default export ()=>React.JSX.Element";
let replaceCommentApi = "// (optional) module with default export (req:Request, url:URL)=>Promise<Response|false>"; let replaceCommentApi = "// (optional) module with default export (req:Request, url:URL)=>Promise<Response|false>";
if(resp2 == "y") if(resp2)
{ {
replaceApp = "./app.tsx"; replaceApp = "./app.tsx";
replaceApi = "./api.tsx"; replaceApi = "./api.tsx";
@ -182,7 +172,7 @@ try
} }
else else
{ {
throw(" X Config is required."); throw(" Config is required.");
} }
} }
@ -200,35 +190,34 @@ if (match !== -1) {
} }
*/ */
console.log(`2) Verifying configuration...`)
if(config.json && imports.json?.imports) if(config.json && imports.json?.imports)
{ {
const importMap = imports.json.imports as Record<string, string>; const importMap = imports.json.imports as Record<string, string>;
let changes = false; let changes = ``;
if(!importMap["react"]) if(!importMap["react"])
{ {
const resp = await Prompt(` ! Import map has no specifier for React. Add it now? (will use Preact compat) [y/n]`); const resp = confirm(`🚨🔧 Import map has no specifier for React ("react"). Fix it now? (Will use Preact compat)`);
if(resp == "y") if(resp)
{ {
importMap["react"] = "https://esm.sh/preact@10.16.0/compat"; importMap["react"] = "https://esm.sh/preact@10.16.0/compat";
changes = true; changes += `"react": "https://esm.sh/preact@10.16.0/compat",\n`;
} }
else else
{ {
throw(" X A React import is required."); throw(`⛔ A React import ("react") is required.`);
} }
} }
if(!importMap[">able/"]) if(!importMap[">able/"])
{ {
const resp = await Prompt(` ! Import map has no specifier for Able (">able/"). Add it now? [y/n]`); const resp = confirm(`🚨🔧 Import map has no specifier for Able (">able/"). Fix it now?`);
if(resp == "y") if(resp)
{ {
importMap[">able/"] = RootHost; importMap[">able/"] = RootHost;
changes = true; changes += `">able": "${RootHost}",\n`;
} }
else else
{ {
throw(" X Able is required."); throw(`⛔ The Able import (">able/") is required.`);
} }
} }
@ -254,3 +243,4 @@ catch(e)
console.log(e, "\n (Able Exiting...)"); console.log(e, "\n (Able Exiting...)");
Deno.exit(); Deno.exit();
} }
console.log(`🚗 Good to go!`);