import { Context } from "./context.ts";import { Node, SourceFile } from "./deps.deno.ts";import { transpileSpecifier } from "./_transformations/specifiers.ts";import { vendorEverything } from "./_transformations/vendor.ts";
function transpileImportSpecifiers(sourceFile: SourceFile) { for (const statement of sourceFile.getStatements()) { if ( Node.isImportDeclaration(statement) || Node.isExportDeclaration(statement) ) { const modSpecifierValue = statement.getModuleSpecifierValue(); if (modSpecifierValue !== undefined) { statement.setModuleSpecifier(transpileSpecifier(modSpecifierValue)); } } }}
function createShimmer(ctx: Context) { if (ctx.config.shim === undefined) { return () => {}; } const shimFile = ctx.project.addSourceFileAtPath( ctx.resolve(ctx.config.shim), ); const shims = Array.from(shimFile.getExportedDeclarations().keys()); return (sourceFile: SourceFile) => { if (sourceFile === shimFile) return; const locals = new Set( sourceFile.getLocals().map((l) => l.getEscapedName()), ); const index = sourceFile.getStatementsWithComments().length; const moduleSpecifier = "./" + sourceFile.getRelativePathTo(shimFile).replace( /tsx?$/, "js", ); sourceFile.insertImportDeclaration(index, { namedImports: shims.filter((s) => !locals.has(s)), moduleSpecifier, }); };}
const isDenoSpecific = (sourceFile: SourceFile) => sourceFile.getBaseNameWithoutExtension().toLowerCase().endsWith(".deno");
const isNodeSpecific = (sourceFile: SourceFile) => sourceFile.getBaseNameWithoutExtension().toLowerCase().endsWith(".node");
export async function deno2node(ctx: Context): Promise<void> { const shim = createShimmer(ctx); console.time("Basic transformations"); for (const sourceFile of ctx.project.getSourceFiles()) { if (isDenoSpecific(sourceFile)) { ctx.project.removeSourceFile(sourceFile); continue; } transpileImportSpecifiers(sourceFile); } console.timeEnd("Basic transformations"); console.time("Vendoring"); await vendorEverything(ctx); console.timeEnd("Vendoring"); console.time("Shimming"); for (const sourceFile of ctx.project.getSourceFiles()) { if (!isNodeSpecific(sourceFile)) { shim(sourceFile); } } console.timeEnd("Shimming");}