able-baker/cli.tsx

247 lines
6.9 KiB
TypeScript
Raw Normal View History

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 22:42:23 -04:00
type ConfigCheck = {path?:string, text?:string, json?:Record<string, string|Record<string, string|string[]>>};
type ConfigCheckPair = [config:ConfigCheck, imports:ConfigCheck];
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 22:42:23 -04:00
path = RootFile + "/" + path
resp = await fetch(path);
2023-08-01 10:19:39 -04:00
text = await resp.text();
}
}
catch(e)
{
path = "";
}
}
}
if(text)
{
try
{
json = JSONC(text);
}
catch(e)
{
2023-08-01 22:42:23 -04:00
// malformed config
2023-08-01 10:19:39 -04:00
}
}
2023-08-01 22:42:23 -04:00
let imports:ConfigCheck = {};
if(json && json.imports)
{
imports.json = json;
imports.text = JSON.stringify(json.imports);
imports.path = path;
}
else if(json && !json.imports && json.importMap)
{
try
{
imports.path = RootFile + "/" + json.importMap;
resp = await fetch(imports.path);
imports.text = await resp.text();
imports.json = JSONC(text);
}
catch(e)
{
// malformed import map
}
}
2023-08-01 17:23:19 -04:00
2023-08-01 22:42:23 -04:00
return [{path, text, json}, imports] as ConfigCheckPair
2023-08-01 17:23:19 -04:00
}
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);
2023-08-02 06:07:34 -04:00
const replace = confirm(`⚠️🚧 The file "${file}" already exists. Replace it?`);
if(replace)
2023-08-01 17:23:19 -04:00
{
throw("")
}
2023-08-02 06:07:34 -04:00
console.log(`Using pre-existing "${file}" for now.`);
2023-08-01 17:23:19 -04:00
}
catch(e)
{
const resp = await fetch(pathFile);
const text = await resp.text();
await Deno.writeTextFile(Deno.cwd()+"/"+file, handler(text));
}
}
2023-08-02 06:07:34 -04:00
console.info(`👷 Checking your project`)
2023-08-01 22:42:23 -04:00
try
2023-08-01 15:30:31 -04:00
{
2023-08-01 22:42:23 -04:00
let [config, imports] = await HuntConfig();
if(!config.path)
2023-08-01 15:30:31 -04:00
{
2023-08-02 06:07:34 -04:00
//const resp1 = await Prompt(" ! No Deno configuration found. Create one? [y/n]");
const resp1 = confirm("🚨🚧 No Deno configuration found. Create one?");
if(resp1)
2023-08-01 15:30:31 -04:00
{
2023-08-02 06:07:34 -04:00
const resp2 = confirm("⚠️🚧 Do you also want to add starter files?");
2023-08-01 17:23:19 -04:00
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>";
2023-08-02 06:07:34 -04:00
if(resp2)
2023-08-01 17:23:19 -04:00
{
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 22:42:23 -04:00
else
{
// config initialized with no app or api
}
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 22:42:23 -04:00
[config, imports] = await HuntConfig();
2023-08-01 15:30:31 -04:00
}
else
{
2023-08-02 06:07:34 -04:00
throw("⛔ Config is required.");
2023-08-01 15:30:31 -04:00
}
2023-08-01 22:42:23 -04:00
2023-08-01 15:30:31 -04:00
}
2023-08-01 22:42:23 -04:00
/*
const inputString = `Some text 'imports' more text { some content }`;
const regex = /(?<=(['"`])imports\1[^{}]*{)/;
const match = inputString.search(regex);
if (match !== -1) {
console.log("Index of '{':", match);
} else {
console.log("'{': Not found.");
}
*/
if(config.json && imports.json?.imports)
2023-08-01 15:30:31 -04:00
{
2023-08-01 22:42:23 -04:00
const importMap = imports.json.imports as Record<string, string>;
2023-08-02 06:07:34 -04:00
let changes = ``;
2023-08-01 22:42:23 -04:00
if(!importMap["react"])
{
2023-08-02 06:07:34 -04:00
const resp = confirm(`🚨🔧 Import map has no specifier for React ("react"). Fix it now? (Will use Preact compat)`);
if(resp)
2023-08-01 22:42:23 -04:00
{
importMap["react"] = "https://esm.sh/preact@10.16.0/compat";
2023-08-02 06:07:34 -04:00
changes += `"react": "https://esm.sh/preact@10.16.0/compat",\n`;
2023-08-01 22:42:23 -04:00
}
else
{
2023-08-02 06:07:34 -04:00
throw(`⛔ A React import ("react") is required.`);
2023-08-01 22:42:23 -04:00
}
}
if(!importMap[">able/"])
{
2023-08-02 06:07:34 -04:00
const resp = confirm(`🚨🔧 Import map has no specifier for Able (">able/"). Fix it now?`);
if(resp)
2023-08-01 22:42:23 -04:00
{
importMap[">able/"] = RootHost;
2023-08-02 06:07:34 -04:00
changes += `">able": "${RootHost}",\n`;
2023-08-01 22:42:23 -04:00
}
else
{
2023-08-02 06:07:34 -04:00
throw(`⛔ The Able import (">able/") is required.`);
2023-08-01 22:42:23 -04:00
}
}
const compOpts = imports.json.compilerOptions as Record<string, string>;
if(compOpts)
{
const compJSX = compOpts["jsx"];
const compJSXImportSource = compOpts["jsxImportSource"]
if(compJSX || compJSXImportSource)
{
if(!importMap["react/"])
{
//const resp = await Prompt(` ! Import map has no specifier for React ("react"). Add it now? [y/n]`);
}
}
}
2023-08-01 15:30:31 -04:00
}
2023-08-01 17:23:19 -04:00
}
2023-08-01 22:42:23 -04:00
catch(e)
2023-08-01 17:23:19 -04:00
{
2023-08-01 22:42:23 -04:00
console.log(e, "\n (Able Exiting...)");
Deno.exit();
2023-08-01 15:30:31 -04:00
}
2023-08-02 06:07:34 -04:00
console.log(`🚗 Good to go!`);