From bf8b521d2f2528665825d2c32fc20ee052bb6873 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20Sj=C3=B6green?= Date: Tue, 24 Sep 2024 14:19:52 +0200 Subject: [PATCH] feat: Publish to npm (#15) --- .github/workflows/publish.yml | 5 +++ .gitignore | 3 +- deno.json | 4 +-- main.ts | 4 +-- mod.ts | 2 +- node/shims.ts | 30 ++++++++++++++++++ scripts/npm.ts | 60 +++++++++++++++++++++++++++++++++++ utils/mod.ts | 2 +- 8 files changed, 103 insertions(+), 7 deletions(-) create mode 100644 node/shims.ts create mode 100644 scripts/npm.ts diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index ebf40ee..6b0c31e 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -14,4 +14,9 @@ jobs: steps: - uses: actions/checkout@v4 - uses: denoland/setup-deno@v1 + - run: deno run -A scripts/npm.ts - run: deno publish + - run: npm publish --provenance + working-directory: ./npm + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.gitignore b/.gitignore index 0282f7a..3502f11 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +npm/ examples/ typefetch.d.ts -tests/*/schemas/*.ts +tests/*/schemas/*.ts \ No newline at end of file diff --git a/deno.json b/deno.json index 64e3539..36766d7 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@denosaurs/typefetch", - "version": "0.0.22", + "version": "0.0.23", "exports": { ".": "./main.ts" }, @@ -13,7 +13,7 @@ "openapi-types": "npm:openapi-types@12.1", "ts-morph": "npm:ts-morph@22.0" }, - "exclude": ["tests/*/schemas/*.ts"], + "exclude": ["tests/*/schemas/*.ts", "npm/*"], "test": { "include": ["tests/test.ts"] }, diff --git a/main.ts b/main.ts index 3a5212b..e79d224 100644 --- a/main.ts +++ b/main.ts @@ -1,5 +1,5 @@ -import { parseArgs } from "@std/cli"; -import * as yaml from "@std/yaml"; +import { parseArgs } from "@std/cli/parse-args"; +import * as yaml from "@std/yaml/parse"; import { wait } from "@denosaurs/wait"; import { ModuleDeclarationKind, Project } from "ts-morph"; diff --git a/mod.ts b/mod.ts index 3db0a38..70ee1b4 100644 --- a/mod.ts +++ b/mod.ts @@ -4,7 +4,7 @@ import type { ModuleDeclaration, SourceFile, } from "ts-morph"; -import { STATUS_CODE } from "@std/http"; +import { STATUS_CODE } from "@std/http/status"; import type { OpenAPI } from "./types/openapi.ts"; diff --git a/node/shims.ts b/node/shims.ts new file mode 100644 index 0000000..310d48c --- /dev/null +++ b/node/shims.ts @@ -0,0 +1,30 @@ +// deno-lint-ignore-file no-explicit-any +import process from "node:process"; + +export function addEventListener( + type: "unload", + listener: () => any, + options?: never, +): void { + if (type !== "unload") { + console.warn( + "The `addEventListener` shim does not support any event type other than `unload`", + ); + return; + } + if (listener.length >= 1) { + console.warn( + "The `addEventListener` shim does not support any arguments passed to the listener", + ); + return; + } + if (options !== undefined) { + console.warn("The `addEventListener` shim does not support options"); + return; + } + + process.on("exit", listener); + process.on("beforeExit", listener); + process.on("SIGINT", listener); + process.on("uncaughtException", listener); +} diff --git a/scripts/npm.ts b/scripts/npm.ts new file mode 100644 index 0000000..72ba1f0 --- /dev/null +++ b/scripts/npm.ts @@ -0,0 +1,60 @@ +import manifest from "../deno.json" with { type: "json" }; + +import { build, emptyDir } from "jsr:@deno/dnt"; + +await emptyDir("./npm"); + +await build({ + entryPoints: ["./main.ts"], + scriptModule: false, + outDir: "./npm", + shims: { + deno: true, + timers: true, + custom: [{ + globalNames: ["addEventListener"], + module: "./node/shims.ts", + }], + }, + test: false, + importMap: "./deno.json", + package: { + name: manifest.name, + version: manifest.version, + description: + "📤 Magically generate `fetch` types from OpenAPI schemas for zero-cost browser-native api clients", + keywords: [ + "fetch", + "openapi", + "swagger", + "api", + "rest", + "typescript", + "types", + "ts", + "dts", + "node", + "deno", + "browser", + ], + license: "MIT", + repository: { + type: "git", + url: "git+https://github.com/denosaurs/typefetch.git", + }, + bugs: { + url: "https://github.com/denosaurs/typefetch/issues", + }, + }, + postBuild() { + Deno.copyFileSync("LICENSE", "npm/LICENSE"); + Deno.copyFileSync("README.md", "npm/README.md"); + + // Add shebang to main.js + const main = Deno.readTextFileSync("npm/esm/main.js"); + Deno.writeTextFileSync( + "npm/esm/main.js", + `#!/usr/bin/env node --no-warnings=ExperimentalWarning\n${main}`, + ); + }, +}); diff --git a/utils/mod.ts b/utils/mod.ts index 2cbb726..7c199ef 100644 --- a/utils/mod.ts +++ b/utils/mod.ts @@ -111,7 +111,7 @@ export function resolveRef(object: unknown, ref: string): T { * Resolves a path to an absolute path with support for file URLs. */ export function resolve(path: string | URL): string { - if (URL.canParse(path)) { + if (URL.canParse(path.toString())) { try { path = fromFileUrl(path); // deno-lint-ignore no-empty