able-baker/run-baker.tsx

104 lines
3.2 KiB
TypeScript
Raw Normal View History

2023-11-06 12:18:44 -05:00
import { walk, type WalkOptions, ensureFile } from "https://deno.land/std@0.204.0/fs/mod.ts";
2023-11-06 15:45:47 -05:00
import ts, { isAssertEntry } from "npm:typescript";
2023-11-06 12:18:44 -05:00
const tsopts:ts.CompilerOptions = { declaration: true, emitDeclarationOnly: true };
const tshost = ts.createCompilerHost(tsopts);
2023-11-06 15:45:47 -05:00
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[]=> {
2023-11-06 12:18:44 -05:00
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);
2023-11-06 15:45:47 -05:00
const extensions = ["tsx", "ts", "jsx", "js"]
for await(const file of walk(dir, {includeDirs:false}))
2023-11-06 12:18:44 -05:00
{
2023-11-06 15:45:47 -05:00
2023-11-06 12:18:44 -05:00
const pathClean = file.path.replaceAll("\\", "/");
const pathRel = pathClean.substring(dir.length);
2023-11-06 15:45:47 -05:00
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";
2023-11-06 12:18:44 -05:00
2023-11-06 15:45:47 -05:00
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);
2023-11-06 12:18:44 -05:00
2023-11-06 15:45:47 -05:00
if(tripleSlash)
{
console.log("making types")
await ensureFile(pathDTS);
await Deno.writeTextFile(pathDTS, tstypes("."+pathRel));
}
}
else
{
await Deno.copyFile("."+pathRel, pathBake);
}
}
}
2023-11-06 12:18:44 -05:00