allow for code or config setup

This commit is contained in:
Seth Trowbridge 2023-07-30 13:04:26 -04:00
parent 6134b7be34
commit 78230a5672
10 changed files with 194 additions and 98 deletions

View File

View File

19
example-config/deno.json Normal file
View File

@ -0,0 +1,19 @@
{
"imports":
{
"react": "https://esm.sh/preact@10.15.1/compat",
">able/": "http://localhost:4507/",
">able/app.tsx": "./app.tsx"
},
"tasks":
{
"serve": "deno run -A --no-lock --config=deno.json --reload=http://localhost:4507 http://localhost:4507/run.tsx",
"local": "deno run -A --no-lock --config=deno.json --reload=http://localhost:4507 http://localhost:4507/run.tsx --dev",
"debug": "deno run -A --no-lock --config=deno.json --reload=http://localhost:4507 --inspect-wait http://localhost:4507/run.tsx",
"cloud": "deno run -A --no-lock --config=deno.json --reload=http://localhost:4507 http://localhost:4507/run.tsx --dep"
},
"compilerOptions":
{
"lib": ["deno.window", "dom"]
}
}

56
example-script/app.tsx Normal file
View File

@ -0,0 +1,56 @@
import { Router, Switch, Case } from ">able/iso-elements.tsx";
import React from "react";
const CTXString = React.createContext("lol");
type StateBinding<T> = [get:T, set:React.StateUpdater<T>];
const CTXState = React.createContext(null) as React.Context<StateBinding<number>|null>;
const Outer =(props:{children:React.JSX.Element})=>
{
const binding = React.useState(11);
return <CTXState.Provider value={binding}>
{props.children}
</CTXState.Provider>
};
const Inner =()=>
{
const [stateGet, stateSet] = React.useContext(CTXState) || ["default", ()=>{}];
return <button onClick={e=>stateSet((old)=>old+1)}>count: {stateGet} :)</button>
};
type Store = {name:string, age:number}
const reducer =(inState:Store, inAction:number)=>
{
return {...inState, age:inState.age+inAction};
}
const builder =(inState:Store):Store=>
{
inState.age = 100;
return inState;
}
export default ()=>
{
const [Store, Dispatch] = React.useReducer(reducer, {name:"seth", age:24} as Store, builder)
return <CTXString.Provider value="intradestink">
<Router.Provider>
<div class="my-4 font-sans">
<h1 class="font-black text-xl text-red-500">Title</h1>
<h2 class="font-black text-blue-500 p-4">subtitle</h2>
<p>
<button onClick={e=>Dispatch(1)}>{Store.name}|{Store.age}?</button>
</p>
</div>
<Outer>
<Inner/>
</Outer>
<Outer>
<Inner/>
</Outer>
</Router.Provider>
</CTXString.Provider>;
}

View File

@ -9,6 +9,7 @@
{
"local": "deno run -A --no-lock --reload=http://localhost:4507 run__.tsx --dev",
"serve": "deno run -A --no-lock --reload=http://localhost:4507 run__.tsx",
"debug": "deno run -A --no-lock --reload=http://localhost:4507 --inspect-wait run__.tsx"
"debug": "deno run -A --no-lock --reload=http://localhost:4507 --inspect-wait run__.tsx",
"cloud": "deno run -A --no-lock --reload=http://localhost:4507 run__.tsx --dep"
}
}

View File

@ -30,6 +30,7 @@ const collect =async(inKey:string, inArg:Record<string, string>, inEnv:Record<st
console.log("Exiting...");
Deno.exit();
}
return scanUser;
};
const prompt =async(question: string):Promise<string>=>
@ -45,8 +46,7 @@ const prompt =async(question: string):Promise<string>=>
try
{
console.log("Runing deploy!");
const serveScript = import.meta.resolve("./run-serve.tsx");
console.log("Runing deploy!", Deno.mainModule);
let arg = parse(Deno.args);
let env = await Env.load();
@ -66,7 +66,7 @@ try
`--project=${useProject}`,
`--import-map=./deno.json`,
`--token=${useToken}`,
serveScript
Deno.mainModule
],
stdin: "piped",
stdout: "piped"

View File

@ -1,6 +1,7 @@
import * as MIME from "https://deno.land/std@0.180.0/media_types/mod.ts";
import * as SWCW from "https://esm.sh/@swc/wasm-web@1.3.62";
import CustomServe from ">able/api.tsx";
export const Root = new URL(`file://${Deno.cwd().replaceAll("\\", "/")}`).toString();
@ -72,11 +73,11 @@ export type Configuration = {Start:string, Allow:string, Reset:string, SWCOp
export type ConfigurationArgs = Partial<Configuration>;
let Configuration:Configuration =
{
Start: "",
Start: ">able/app.tsx",
Allow: "*",
Reset: "/clear-cache",
async Extra(inReq, inURL, inExt, inMap, inConfig){},
async Serve(inReq, inURL, inExt, inMap, inConfig){},
Serve: CustomServe,
Remap: (inImports, inConfig)=>
{
return inImports;
@ -258,104 +259,106 @@ export const Configure =(config:ConfigurationArgs)=>
ImportMapReload();
}
await ImportMapReload();
try
export default async()=>
{
await SWCW.default();
}
catch(e)
{
console.log("swc init error:", e);
}
const server = Deno.serve({port:parseInt(Deno.env.get("port")||"8000")}, async(req: Request)=>
{
const url:URL = new URL(req.url);
const ext = Extension(url.pathname);
const headers = {"content-type":"application/json", "Access-Control-Allow-Origin": Configuration.Allow, "charset":"UTF-8"};
let proxy = Root + url.pathname;
if(url.pathname.includes("__/") || url.pathname.lastIndexOf("__.") > -1)
await ImportMapReload();
try
{
return new Response(`{"error":"unmatched route", "path":"${url.pathname}"}`, {status:404, headers});
await SWCW.default();
}
// proxy imports
if(url.pathname.startsWith(encodeURI("/>")))
catch(e)
{
let bestMatch="";
for(let key in ImportMapProxies)
console.log("swc init error:", e);
}
const server = Deno.serve({port:parseInt(Deno.env.get("port")||"8000")}, async(req: Request)=>
{
const url:URL = new URL(req.url);
const ext = Extension(url.pathname);
const headers = {"content-type":"application/json", "Access-Control-Allow-Origin": Configuration.Allow, "charset":"UTF-8"};
let proxy = Root + url.pathname;
if(url.pathname.includes("__/") || url.pathname.lastIndexOf("__.") > -1)
{
if(url.pathname.startsWith(key) && key.length > bestMatch.length)
return new Response(`{"error":"unmatched route", "path":"${url.pathname}"}`, {status:404, headers});
}
// proxy imports
if(url.pathname.startsWith(encodeURI("/>")))
{
let bestMatch="";
for(let key in ImportMapProxies)
{
bestMatch = key;
if(url.pathname.startsWith(key) && key.length > bestMatch.length)
{
bestMatch = key;
}
}
if(bestMatch.length)
{
const match = ImportMapProxies[bestMatch];
const path = url.pathname.substring(bestMatch.length);
proxy = path ? match + path : Root + match;
}
}
// allow for custom handlers
const custom = await Configuration.Extra(req, url, ext, ImportMap, Configuration);
if(custom)
{
return custom;
}
const api = await Configuration.Serve(req, url, ext, ImportMap, Configuration);
if(api)
{
return api;
}
// transpileable files
if(Transpile.Check(ext))
{
console.log("transpiling:", proxy);
const code = await Transpile.Fetch(proxy, url.pathname);
if(code)
{
return new Response(code, {headers:{...headers, "content-type":"application/javascript"}} );
}
}
// custom page html
if(!ext)
{
const shell = await Configuration.Shell(req, url, ext, ImportMap, Configuration);
if(shell)
{
return shell;
}
}
if(bestMatch.length)
// cache-reset route
if(url.pathname === Configuration.Reset)
{
const match = ImportMapProxies[bestMatch];
const path = url.pathname.substring(bestMatch.length);
proxy = path ? match + path : Root + match;
}
}
// allow for custom handlers
const custom = await Configuration.Extra(req, url, ext, ImportMap, Configuration);
if(custom)
{
return custom;
}
const api = await Configuration.Serve(req, url, ext, ImportMap, Configuration);
if(api)
{
return api;
}
// transpileable files
if(Transpile.Check(ext))
{
console.log("transpiling:", proxy);
const code = await Transpile.Fetch(proxy, url.pathname);
if(code)
{
return new Response(code, {headers:{...headers, "content-type":"application/javascript"}} );
}
}
// custom page html
if(!ext)
{
const shell = await Configuration.Shell(req, url, ext, ImportMap, Configuration);
if(shell)
{
return shell;
return new Response(`{"cleared":${Transpile.Clear()}}`, {headers});
}
}
// cache-reset route
if(url.pathname === Configuration.Reset)
{
return new Response(`{"cleared":${Transpile.Clear()}}`, {headers});
}
// all other static files
if(ext)
{
try
// all other static files
if(ext)
{
const type = MIME.typeByExtension(ext);
const file = await fetch(proxy);
return new Response(file.body, {headers:{...headers, "content-type":type||""}});
try
{
const type = MIME.typeByExtension(ext);
const file = await fetch(proxy);
return new Response(file.body, {headers:{...headers, "content-type":type||""}});
}
catch(e)
{
return new Response(`{"error":"${e}", "path":"${url.pathname}"}`, {status:404, headers});
}
}
catch(e)
{
return new Response(`{"error":"${e}", "path":"${url.pathname}"}`, {status:404, headers});
}
}
return new Response(`{"error":"unmatched route", "path":"${url.pathname}"}`, {status:404, headers});
});
return new Response(`{"error":"unmatched route", "path":"${url.pathname}"}`, {status:404, headers});
});
}

23
run.tsx
View File

@ -1,5 +1,14 @@
import * as Serve from "./run-serve.tsx";
export default Serve.Configure;
export default function(config:Serve.ConfigurationArgs)
{
if(Deno.env.get("deploy"))
{
return;
}
Serve.Configure(config)
Serve.default();
}
Deno.args.forEach(arg=>
{
@ -10,7 +19,15 @@ Deno.args.forEach(arg=>
}
});
if(Deno.env.get("dev"))
if(Deno.env.get("dep"))
{
import("./run-local.tsx");
import("./run-deploy.tsx");
}
else
{
if(Deno.env.get("dev"))
{
await import("./run-local.tsx");
}
Serve.default();
}