diff --git a/common/logger.ts b/common/logger.ts index 7be861a..8100aa7 100644 --- a/common/logger.ts +++ b/common/logger.ts @@ -7,19 +7,30 @@ import { type Sink, } from "@logtape/logtape"; import { getFileSink } from "@logtape/file"; +import process from "node:process"; + +// Runtime detection for Deno vs Node.js +const isDeno = typeof Deno !== "undefined"; +const getEnv = (key: string): string | undefined => { + if (isDeno) { + return Deno.env.get(key); + } else { + return process.env[key]; + } +}; -const allSystemsEnabled = !Deno.env.get("LOG_SYSTEMS"); -const enabledSystems = (Deno.env.get("LOG_SYSTEMS") || "") +const allSystemsEnabled = !getEnv("LOG_SYSTEMS"); +const enabledSystems = (getEnv("LOG_SYSTEMS") || "") .replace(",", " ") .split(" ") .filter(Boolean); -const enabledEnv = Deno.env.get("LOG_ENABLED"); +const enabledEnv = getEnv("LOG_ENABLED"); const enabled = enabledEnv === "true" || enabledEnv === "t" || enabledEnv === "1"; -const level = (Deno.env.get("LOG_LEVEL") || "info") as LogLevel; -const logDestination = Deno.env.get("LOG_DESTINATION"); +const level = (getEnv("LOG_LEVEL") || "info") as LogLevel; +const logDestination = getEnv("LOG_DESTINATION"); // Initialize LogTape configuration let configured = false; diff --git a/deno.lock b/deno.lock index fee14b2..5bc79b7 100644 --- a/deno.lock +++ b/deno.lock @@ -55,6 +55,7 @@ "npm:multiformats@*": "13.4.0", "npm:multiformats@^13.4.0": "13.4.0", "npm:multiformats@^13.4.1": "13.4.1", + "npm:prettier@^3.6.2": "3.6.2", "npm:rate-limiter-flexible@^2.4.1": "2.4.2", "npm:uint8arrays@3.0.0": "3.0.0", "npm:uint8arrays@^5.1.0": "5.1.0", @@ -428,6 +429,10 @@ ], "bin": true }, + "prettier@3.6.2": { + "integrity": "sha512-I7AIg5boAr5R0FFtJ6rCfD+LFsWHp81dolrFD8S79U9tb8Az2nGrJncnMSnys+bpQJfRUzqs9hnA81OAA3hCuQ==", + "bin": true + }, "rate-limiter-flexible@2.4.2": { "integrity": "sha512-rMATGGOdO1suFyf/mI5LYhts71g1sbdhmd6YvdiXO2gJnd42Tt6QS4JUKJKSWVVkMtBacm6l40FR7Trjo6Iruw==" }, @@ -465,6 +470,16 @@ "integrity": "sha512-gzUt/qt81nXsFGKIFcC3YnfEAx5NkunCfnDlvuBSSFS02bcXu4Lmea0AFIUwbLWxWPx3d9p8S5QoaujKcNQxcQ==" } }, + "remote": { + "https://deno.land/x/deno_fmt@0.1.5/mod.ts": "bb74c57907b77a301cef37236af9de85c2cb0ac75f79a9c60db3208e979f19a8", + "https://deno.land/x/deno_fmt@0.1.5/src/context.ts": "baf814994ef5696f8ced9b5d0156b81ec75207e41e989c76b84b87dbe50d6e54", + "https://deno.land/x/deno_fmt@0.1.5/src/dprint.ts": "a5ea7261f77998f0e8d5d9e581c6d15afe86aa9ab32af2dccfe4865b008a2774", + "https://deno.land/x/deno_fmt@0.1.5/src/fmt.ts": "52941a928918a62c5efe9594b83ed1bf89f324ca132c292695ea17e791315dfa", + "https://deno.land/x/deno_fmt@0.1.5/src/helpers.ts": "45da2c3cab4faf8f883bce5b9edd83cde5d80f5f75ca42e41195759b98acbcdf", + "https://deno.land/x/deno_fmt@0.1.5/src/lru.ts": "0a22753be042c5ea63dcf830a04e3481a9cded5b755de7b6146be97a36274ffa", + "https://deno.land/x/deno_fmt@0.1.5/src/options.ts": "840c13cf1d78255681b91625eb7719b2883f68f4f0f25142f697bf0e57304b02", + "https://deno.land/x/deno_fmt@0.1.5/src/sha256.ts": "eea8272cd53072763ad5fa8e632a572676cde41e027485116a8f84a49a669bf8" + }, "workspace": { "members": { "common": { @@ -493,7 +508,8 @@ "jsr:@std/path@^1.1.2", "jsr:@ts-morph/ts-morph@26", "jsr:@zod/zod@^4.1.5", - "npm:@atproto/lexicon@~0.4.14" + "npm:@atproto/lexicon@~0.4.14", + "npm:prettier@^3.6.2" ] }, "syntax": { diff --git a/lex-cli/cmd/gen-api.ts b/lex-cli/cmd/gen-api.ts index 97dfbef..a11d70a 100644 --- a/lex-cli/cmd/gen-api.ts +++ b/lex-cli/cmd/gen-api.ts @@ -9,7 +9,6 @@ import { genClientApi } from "../codegen/client.ts"; import { formatGeneratedFiles } from "../codegen/util.ts"; const command = new Command() - .command("gen-api") .description("Generate a TS client API") .option("--js", "use .js extension for imports instead of .ts") .option("-o, --outdir ", "dir path to write to", { required: true }) @@ -26,7 +25,9 @@ const command = new Command() console.log("This will write the following files:"); printFileDiff(diff); applyFileDiff(diff); - await formatGeneratedFiles(outdir); + if (typeof Deno !== "undefined") { + await formatGeneratedFiles(outdir); + } console.log("API generated."); }, ); diff --git a/lex-cli/cmd/gen-md.ts b/lex-cli/cmd/gen-md.ts index 808f200..6918683 100644 --- a/lex-cli/cmd/gen-md.ts +++ b/lex-cli/cmd/gen-md.ts @@ -1,6 +1,9 @@ import { Command } from "@cliffy/command"; import { readAllLexicons } from "../util.ts"; import * as mdGen from "../mdgen/index.ts"; +import process from "node:process"; + +const isDeno = typeof Deno !== "undefined"; const command = new Command() .description("Generate markdown documentation") @@ -12,7 +15,11 @@ const command = new Command() console.error( "Must supply the path to a .md file as the first parameter", ); - Deno.exit(1); + if (isDeno) { + Deno.exit(1); + } else { + process.exit(1); + } } const lexicons = readAllLexicons(input); await mdGen.process(output, lexicons); diff --git a/lex-cli/cmd/gen-server.ts b/lex-cli/cmd/gen-server.ts index 3c55e72..81f632a 100644 --- a/lex-cli/cmd/gen-server.ts +++ b/lex-cli/cmd/gen-server.ts @@ -27,7 +27,9 @@ const command = new Command() console.log("This will write the following files:"); printFileDiff(diff); applyFileDiff(diff); - await formatGeneratedFiles(outdir); + if (typeof Deno !== "undefined") { + await formatGeneratedFiles(outdir); + } console.log("API generated."); }, ); diff --git a/lex-cli/codegen/common.ts b/lex-cli/codegen/common.ts index bbbab0b..5c268e1 100644 --- a/lex-cli/codegen/common.ts +++ b/lex-cli/codegen/common.ts @@ -6,6 +6,16 @@ import { import type { LexiconDoc } from "@atproto/lexicon"; import type { GeneratedFile } from "../types.ts"; import type { CodeGenOptions } from "./util.ts"; +import { format, type Options as PrettierOptions } from "prettier"; +import * as fmt from "deno_fmt"; + +const PRETTIER_OPTS: PrettierOptions = { + parser: "typescript", + tabWidth: 2, + semi: false, + singleQuote: true, + trailingComma: "all", +}; export const utilTs = ( project: Project, @@ -276,7 +286,12 @@ export async function gen( const file = project.createSourceFile(path); gen(file); await file.save(); // Save in the "in memory" file system - const content = `${banner()}${file.getFullText()}`; + let content = `${banner()}${file.getFullText()}`; + if (!(typeof Deno !== "undefined")) { + content = await format(content, PRETTIER_OPTS); + } else { + content = await fmt.format(content); + } return { path, content }; } diff --git a/lex-cli/deno.json b/lex-cli/deno.json index f9c9209..85c8c23 100644 --- a/lex-cli/deno.json +++ b/lex-cli/deno.json @@ -9,7 +9,9 @@ "@std/fs": "jsr:@std/fs@^1.0.19", "@std/path": "jsr:@std/path@^1.1.2", "@atproto/lexicon": "npm:@atproto/lexicon@^0.4.14", + "prettier": "npm:prettier@^3.6.2", "ts-morph": "jsr:@ts-morph/ts-morph@^26.0.0", - "zod": "jsr:@zod/zod@^4.1.5" + "zod": "jsr:@zod/zod@^4.1.5", + "deno_fmt": "https://deno.land/x/deno_fmt@0.1.5/mod.ts" } } diff --git a/lex-cli/mod.ts b/lex-cli/mod.ts index 67f3eaa..de41995 100644 --- a/lex-cli/mod.ts +++ b/lex-cli/mod.ts @@ -2,6 +2,9 @@ import { Command } from "@cliffy/command"; import { genApi, genMd, genServer, genTsObj } from "./cmd/index.ts"; +import process from "node:process"; + +const isDeno = typeof Deno !== "undefined"; await new Command() .name("lex-cli") @@ -10,4 +13,4 @@ await new Command() .command("gen-md", genMd) .command("gen-server", genServer) .command("gen-ts-obj", genTsObj) - .parse(Deno.args); + .parse(isDeno ? Deno.args : process.argv.slice(2));