From f0c34f461ec300079ce63b249468486e299c637f Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Mon, 6 Nov 2023 12:18:44 -0500 Subject: [PATCH] baker started --- run-baker.tsx | 63 +++++++++++++++++++++++++++++ run-deploy.tsx | 106 ------------------------------------------------- run-serve.tsx | 2 +- 3 files changed, 64 insertions(+), 107 deletions(-) create mode 100644 run-baker.tsx delete mode 100644 run-deploy.tsx diff --git a/run-baker.tsx b/run-baker.tsx new file mode 100644 index 0000000..7828c48 --- /dev/null +++ b/run-baker.tsx @@ -0,0 +1,63 @@ +import { walk, type WalkOptions, ensureFile } from "https://deno.land/std@0.204.0/fs/mod.ts"; + +import ts from "npm:typescript"; +const tsopts:ts.CompilerOptions = { declaration: true, emitDeclarationOnly: true }; +const tshost = ts.createCompilerHost(tsopts); +const tstypes =(fileNames: string[]):string[]=> { + const output:string[] = []; + tshost.writeFile = (fileName: string, contents: string) => output[fileName.indexOf(fileName)] = contents; + ts.createProgram(fileNames, tsopts, tshost).emit(); + return output; +} + +import * as SWCW from "https://esm.sh/@swc/wasm-web@1.3.62"; + +await SWCW.default(); +const options:SWCW.Options = { + sourceMaps: false, + minify: true, + jsc: + { + target:"es2022", + minify: + { + compress: { unused: true }, + mangle: false + }, + parser: + { + syntax: "typescript", + tsx: true, + }, + transform: + { + react: { runtime: "automatic" } + } + } +} + + +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 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); + + await Deno.writeTextFile(pathJS, `/// \n${code}`, {create:true}); +} + + + +const output = tstypes(['hmr-listen.tsx']); +console.log(output); \ No newline at end of file diff --git a/run-deploy.tsx b/run-deploy.tsx deleted file mode 100644 index 5f9ef7c..0000000 --- a/run-deploy.tsx +++ /dev/null @@ -1,106 +0,0 @@ -import * as Env from "https://deno.land/std@0.194.0/dotenv/mod.ts"; -import { parse } from "https://deno.land/std@0.194.0/flags/mod.ts"; - - -const collect =async(inKey:string, inArg:Record, inEnv:Record):Promise=> -{ - const scanArg = inArg[inKey]; - const scanEnvFile = inEnv[inKey]; - const scanEnvDeno = Deno.env.get(inKey); - - if(scanArg) - { - console.log(`Using "${inKey}" from passed argument.`); - return scanArg; - } - if(scanEnvFile) - { - console.log(`Using "${inKey}" from .env file.`); - return scanEnvFile; - } - if(scanEnvDeno) - { - console.log(`Using "${inKey}" from environment variable.`); - return scanEnvDeno; - } - - const scanUser = await prompt(`No "${inKey}" found. Enter one here:`); - if(!scanUser || scanUser?.length < 3) - { - console.log("Exiting..."); - Deno.exit(); - } - return scanUser; -}; - -const prompt =async(question: string):Promise=> -{ - 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"); -}; - -try -{ - console.log("Runing deploy!", Deno.mainModule); - - let arg = parse(Deno.args); - let env = await Env.load(); - - let useToken = await collect("DENO_DEPLOY_TOKEN", arg, env); - let useProject = await collect("DENO_DEPLOY_PROJECT", arg, env); - - let scanProd:string|string[] = await prompt(`Do you want to deploy to *production*? [y/n]`); - if(scanProd == "y") - { - scanProd = await prompt(`This will update the live project at ${useProject} are you sure you want to continue? [y/n]`); - scanProd = scanProd=="y" ? ["--prod"] : []; - } - else - { - scanProd = []; - } - - const command = new Deno.Command( - `deno`, - { - args:[ - "run", - "-A", - "--no-lock", - "https://deno.land/x/deploy/deployctl.ts", - "deploy", - `--project=${useProject}`, - `--import-map=./deno.json`, - `--token=${useToken}`, - ...scanProd, - Deno.mainModule - ], - 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 { - Deno.stdout.write(chunk); - return Promise.resolve(); - }, - }); - child.stdout.pipeTo(writableStream); - - // manually close stdin - child.stdin.close(); - const status = await child.status; -} -catch(e) -{ - console.error(e); -} \ No newline at end of file diff --git a/run-serve.tsx b/run-serve.tsx index cb61263..ff9547e 100644 --- a/run-serve.tsx +++ b/run-serve.tsx @@ -92,7 +92,7 @@ let Configuration:Configuration = minify: { compress: { unused: true }, - mangle: true + mangle: false }, parser: {