diff --git a/src/cli.ts b/src/cli.ts index d959369..dc2a67c 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,11 +1,12 @@ -#!/usr/bin/env -S deno run --no-npm --no-prompt --allow-read=. --allow-write=. +#!/usr/bin/env -S deno run --no-prompt --allow-read=. --allow-write=lib/ +import { existsSync } from "node:fs"; import { ts } from "./deps.deno.ts"; import { getHelpText } from "./help.ts"; import { getVersion, initializeProject } from "./init.ts"; import { Context, deno2node, emit } from "./mod.ts"; const { options, fileNames, errors } = ts.parseCommandLine(Deno.args); -const tsConfigFilePath = options.project ?? fileNames[0] ?? "tsconfig.json"; +const tsConfigFilePath = options.project ?? ts.findConfigFile(".", existsSync); if (errors.length) { for (const error of errors) { @@ -30,11 +31,26 @@ if (options.init) { Deno.exit(0); } -console.time("Loading tsconfig"); -const ctx = new Context({ tsConfigFilePath, compilerOptions: options }); -console.timeEnd("Loading tsconfig"); +const ctx = new Context({ + tsConfigFilePath, + compilerOptions: options, + skipAddingFilesFromTsConfig: true, +}); + +console.time("Loading source files"); +if (fileNames.length) { + ctx.project.addSourceFilesAtPaths(fileNames); +} else if (tsConfigFilePath) { + ctx.project.addSourceFilesFromTsConfig(tsConfigFilePath); +} else { + console.error("Specify entry points."); + Deno.exit(2); +} +ctx.project.resolveSourceFileDependencies(); +console.timeEnd("Loading source files"); + +deno2node(ctx); -await deno2node(ctx); console.time("Emitting"); const diagnostics = await emit(ctx.project); console.timeEnd("Emitting"); diff --git a/src/deno2node.ts b/src/deno2node.ts index 390367a..0a54e91 100644 --- a/src/deno2node.ts +++ b/src/deno2node.ts @@ -1,24 +1,8 @@ -// based on https://github.com/dsherret/code-block-writer/blob/99454249cd13bd89befa45ac815b37b3e02896f5/scripts/build_npm.ts - -import { Context } from "./context.ts"; -import { Node, SourceFile } from "./deps.deno.ts"; -import { transpileSpecifier } from "./_transformations/specifiers.ts"; -import { vendorEverything } from "./_transformations/vendor.ts"; import { shimEverything } from "./_transformations/shim.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)); - } - } - } -} +import { transpileSpecifiers } from "./_transformations/specifiers.ts"; +import { vendorEverything } from "./_transformations/vendor.ts"; +import { type Context } from "./context.ts"; +import { type SourceFile } from "./deps.deno.ts"; const isDenoSpecific = (sourceFile: SourceFile) => sourceFile.getBaseNameWithoutExtension().toLowerCase().endsWith(".deno"); @@ -28,20 +12,20 @@ const isDenoSpecific = (sourceFile: SourceFile) => * * 1. Changes import specifiers to be Node-friendly: * - changes extension in relative specifiers to `.js`, - * - replaces some `https://` imports with bare specifiers. + * - replaces `npm:` imports with bare specifiers. * * 2. Changes `*.deno.js` imports specifiers to `*.node.js` * (`import './deps.deno.ts'` -> `import './deps.node.js'`). * This can be used for re-exporting dependencies * and other runtime-specific code. * - * 3. Rewrites remaining https: imports to point + * 3. Rewrites remaining `https:` imports to point * into `vendorDir`, if specified: * ```json - * // @filename: tsconfig.json + * //@filename: tsconfig.json * { * "deno2node": { - * "vendorDir": "src/.deno2node/vendor/" + * "vendorDir": "src/vendor/" // path within `rootDir` * } * } * ``` @@ -49,7 +33,7 @@ const isDenoSpecific = (sourceFile: SourceFile) => * 4. Imports Node.js shims for Deno globals * from [shim file], if specified: * ```json - * // @filename: tsconfig.json + * //@filename: tsconfig.json * { * "deno2node": { * "shim": "src/shim.node.ts" @@ -57,18 +41,19 @@ const isDenoSpecific = (sourceFile: SourceFile) => * } * ``` * - * [shim file]: https://github.com/wojpawlik/deno2node/blob/main/src/shim.node.ts + * [shim file]: https://github.com/fromdeno/deno2node/blob/main/src/shim.node.ts */ -export async function deno2node(ctx: Context): Promise { +export function deno2node(ctx: Context): void { console.time("Basic transformations"); for (const sourceFile of ctx.project.getSourceFiles()) { if (isDenoSpecific(sourceFile)) { ctx.project.removeSourceFile(sourceFile); continue; } - transpileImportSpecifiers(sourceFile); + transpileSpecifiers(sourceFile); } console.timeEnd("Basic transformations"); - await vendorEverything(ctx); + + vendorEverything(ctx); shimEverything(ctx); }