bake working
This commit is contained in:
		
							parent
							
								
									f0c34f461e
								
							
						
					
					
						commit
						8d89cebe12
					
				
							
								
								
									
										22
									
								
								cli.tsx
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								cli.tsx
									
									
									
									
									
								
							@ -4,7 +4,7 @@ import { RootHost, HuntConfig, Install, Check } from "./checker.tsx";
 | 
			
		||||
 | 
			
		||||
let arg = await Arg.parse(Deno.args);
 | 
			
		||||
let env = await Env.load();
 | 
			
		||||
const collect =async(inKey:string, inArg:Record<string, string>, inEnv:Record<string, string>):Promise<string|undefined>=>
 | 
			
		||||
const collect =(inKey:string, inArg:Record<string, string>, inEnv:Record<string, string>):string|undefined=>
 | 
			
		||||
{
 | 
			
		||||
    const scanArg = inArg[inKey];
 | 
			
		||||
    const scanEnvFile = inEnv[inKey];
 | 
			
		||||
@ -93,7 +93,6 @@ if(arg._.length)
 | 
			
		||||
        case "serve" :
 | 
			
		||||
        {
 | 
			
		||||
            const args = ["run", `-A`, `--no-lock`, `--config=${config.path}`, RootHost+"run.tsx", ...Deno.args];
 | 
			
		||||
            console.log("args are", args);
 | 
			
		||||
            await SubProcess(args);
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
@ -102,15 +101,12 @@ if(arg._.length)
 | 
			
		||||
            const useToken = await collect("DENO_DEPLOY_TOKEN", arg, env);
 | 
			
		||||
            const useProject = await collect("DENO_DEPLOY_PROJECT", arg, env);
 | 
			
		||||
            
 | 
			
		||||
            let scanProd:string[]|string|null = prompt(`Do you want to deploy to *production*?`);
 | 
			
		||||
            let scanProd = confirm(`Do you want to deploy to *production*?`);
 | 
			
		||||
            let argProd:string[] = [];
 | 
			
		||||
            if(scanProd)
 | 
			
		||||
            {
 | 
			
		||||
                scanProd = prompt(`Are you sure? This will update the live project at "${useProject}"`);
 | 
			
		||||
                scanProd = scanProd ? ["--prod"] : [];
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                scanProd = [];
 | 
			
		||||
                scanProd = confirm(`Are you sure? This will update the live project at "${useProject}"`);
 | 
			
		||||
                argProd = scanProd ? ["--prod"] : [];
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            const command = [
 | 
			
		||||
@ -124,13 +120,19 @@ if(arg._.length)
 | 
			
		||||
                `--token=${useToken}`,
 | 
			
		||||
                `--import-map=${imports.path}`,
 | 
			
		||||
                `--exclude=.*,.*/,`,
 | 
			
		||||
                ...scanProd,
 | 
			
		||||
                ...argProd,
 | 
			
		||||
                RootHost+"run.tsx"];
 | 
			
		||||
 | 
			
		||||
            await SubProcess(command);
 | 
			
		||||
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        case "baker" :
 | 
			
		||||
        {
 | 
			
		||||
            const args = ["run", `-A`, `--no-lock`, `--config=${config.path}`, RootHost+"run.tsx", ...Deno.args];
 | 
			
		||||
            await SubProcess(args);
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        case "upgrade" :
 | 
			
		||||
        {
 | 
			
		||||
            await SubProcess(["install", `-A`, `-r`, `-f`, `--no-lock`, `--config=${config.path}`, RootHost+"cli.tsx", ...Deno.args]);
 | 
			
		||||
 | 
			
		||||
@ -1,9 +1,15 @@
 | 
			
		||||
import { walk, type WalkOptions, ensureFile } from "https://deno.land/std@0.204.0/fs/mod.ts";
 | 
			
		||||
 | 
			
		||||
import ts from "npm:typescript";
 | 
			
		||||
import ts, { isAssertEntry } from "npm:typescript";
 | 
			
		||||
const tsopts:ts.CompilerOptions = { declaration: true, emitDeclarationOnly: true };
 | 
			
		||||
const tshost = ts.createCompilerHost(tsopts);
 | 
			
		||||
const tstypes =(fileNames: string[]):string[]=> {
 | 
			
		||||
const tstypes =(fileName: string):string=> {
 | 
			
		||||
    let output = "";
 | 
			
		||||
    tshost.writeFile = (fileName: string, contents: string) => output = contents;
 | 
			
		||||
    ts.createProgram([fileName], tsopts, tshost).emit();
 | 
			
		||||
    return output;
 | 
			
		||||
}
 | 
			
		||||
const tstypes_all =(fileNames: string[]):string[]=> {
 | 
			
		||||
    const output:string[] = [];
 | 
			
		||||
    tshost.writeFile = (fileName: string, contents: string) => output[fileName.indexOf(fileName)] = contents;
 | 
			
		||||
    ts.createProgram(fileNames, tsopts, tshost).emit();
 | 
			
		||||
@ -40,24 +46,58 @@ const options:SWCW.Options = {
 | 
			
		||||
const dir = Deno.cwd();
 | 
			
		||||
const folder = dir.substring(dir.lastIndexOf("\\")+1);
 | 
			
		||||
console.log("searching", dir);
 | 
			
		||||
 | 
			
		||||
for await(const file of walk(dir, {exts:["tsx", "ts", "jsx", "js"], includeDirs:false}))
 | 
			
		||||
const extensions = ["tsx", "ts", "jsx", "js"]
 | 
			
		||||
for await(const file of walk(dir, {includeDirs:false}))
 | 
			
		||||
{
 | 
			
		||||
    
 | 
			
		||||
    const pathClean = file.path.replaceAll("\\", "/");
 | 
			
		||||
    const text = await Deno.readTextFile(pathClean);
 | 
			
		||||
 | 
			
		||||
    const {code} = await SWCW.transform(text, { ...options, filename:file.name});
 | 
			
		||||
    const pathRel = pathClean.substring(dir.length);
 | 
			
		||||
    const pathJS = `baked${pathRel}`;
 | 
			
		||||
    const pathDTS = pathJS.substring(0, pathJS.lastIndexOf("."))+".d.ts";
 | 
			
		||||
 | 
			
		||||
    await ensureFile(pathDTS);
 | 
			
		||||
    await ensureFile(pathJS);
 | 
			
		||||
    if(!pathRel.startsWith(".") && !pathRel.includes("/."))
 | 
			
		||||
    {  
 | 
			
		||||
        const extension = file.name.substring(file.name.lastIndexOf(".")+1);
 | 
			
		||||
        const pathBake = `.bake${pathRel}`;
 | 
			
		||||
        if(extensions.includes(extension))
 | 
			
		||||
        {
 | 
			
		||||
            const pathDTS = pathBake.substring(0, pathBake.lastIndexOf("."))+".d.ts";
 | 
			
		||||
 | 
			
		||||
    await Deno.writeTextFile(pathJS, `/// <reference types=".${pathDTS.substring(pathDTS.lastIndexOf("/"))}" />\n${code}`, {create:true});
 | 
			
		||||
            console.log("processing", pathRel);
 | 
			
		||||
    
 | 
			
		||||
            const text = await Deno.readTextFile(pathClean);
 | 
			
		||||
            const {code} = await SWCW.transform(text, { ...options, filename:file.name});        
 | 
			
		||||
 | 
			
		||||
            // check for types export directive
 | 
			
		||||
            let tripleSlash = "";
 | 
			
		||||
            const read = await Deno.open(file.path);
 | 
			
		||||
            const buffer = new Uint8Array(256); // Set the buffer size to the maximum line length
 | 
			
		||||
            try {
 | 
			
		||||
                const bytesRead = await read.read(buffer);
 | 
			
		||||
                if(bytesRead)
 | 
			
		||||
                {
 | 
			
		||||
                    if(new TextDecoder().decode(buffer.subarray(0, bytesRead)).indexOf("@able-types") != -1)
 | 
			
		||||
                    {
 | 
			
		||||
                        tripleSlash =`/// <reference types=".${pathDTS.substring(pathDTS.lastIndexOf("/"))}" />\n`;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            } finally {
 | 
			
		||||
                read.close();
 | 
			
		||||
            }
 | 
			
		||||
        
 | 
			
		||||
            await ensureFile(pathBake);
 | 
			
		||||
            await Deno.writeTextFile(pathBake, tripleSlash+code);
 | 
			
		||||
 | 
			
		||||
            if(tripleSlash)
 | 
			
		||||
            {
 | 
			
		||||
                console.log("making types")
 | 
			
		||||
                await ensureFile(pathDTS);
 | 
			
		||||
                await Deno.writeTextFile(pathDTS, tstypes("."+pathRel));   
 | 
			
		||||
            }
 | 
			
		||||
         
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            await Deno.copyFile("."+pathRel, pathBake);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const output = tstypes(['hmr-listen.tsx']);
 | 
			
		||||
console.log(output);
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user