Skip to content

Commit

Permalink
💥 Accept entry points in CLI (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
wojpawlik committed Jul 19, 2023
1 parent cc7d7c1 commit 16eb7da
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
28 changes: 22 additions & 6 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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");
Expand Down
43 changes: 14 additions & 29 deletions src/deno2node.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -28,47 +12,48 @@ 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`
* }
* }
* ```
*
* 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"
* }
* }
* ```
*
* [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<void> {
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);
}

0 comments on commit 16eb7da

Please sign in to comment.