Skip to content

Commit

Permalink
Add publish workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
zauni committed Sep 23, 2024
1 parent 82cf03e commit 09e1e48
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 11 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Publish

on:
release:
types: [created]

jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v4
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v4
with:
node-version-file: .node-version
registry-url: https://registry.npmjs.org
- run: npm ci
- run: npm run build
- name: Publish package to JSR
run: npx jsr publish
- name: Publish package to NPM
run: npm publish --provenance
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ following contents:
_src/yaml.d.ts_

```ts
/// <reference types="unplugin-openapi/types/yaml" />
/// <reference types="unplugin-openapi/yaml" />
```

_src/index.ts_
Expand Down
3 changes: 0 additions & 3 deletions fs-monkey.d.ts

This file was deleted.

17 changes: 16 additions & 1 deletion jsr.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,20 @@
"$schema": "https://jsr.io/schema/config-file.v1.json",
"name": "@zauni/unplugin-openapi",
"version": "1.0.0",
"exports": "./src/index.ts"
"exports": "./src/mod.ts",
"publish": {
"include": [
"README.md",
"types",
"src/astro.ts",
"src/esbuild.ts",
"src/index.ts",
"src/mod.ts",
"src/rolldown.ts",
"src/rollup.ts",
"src/rspack.ts",
"src/vite.ts",
"src/webpack.ts"
]
}
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@
"webpack",
"rspack",
"esbuild",
"farm",
"plugin",
"astro"
],
Expand Down
24 changes: 19 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
dataToEsm,
} from "@rollup/pluginutils";
import type { OpenAPI, OpenAPIV2, OpenAPIV3, OpenAPIV3_1 } from "openapi-types";
import type { UnpluginFactory } from "unplugin";
import type { UnpluginFactory, UnpluginInstance } from "unplugin";
import { createUnplugin } from "unplugin";

export * from "openapi-types";
Expand Down Expand Up @@ -111,9 +111,11 @@ export const unpluginFactory: UnpluginFactory<Options | undefined> = (opts) => {
},
},
esbuild: {
// handle .json files as JavaScript modules
loader: "js",
},
rolldown: {
// handle .json files as JavaScript modules
options(opts) {
const moduleTypes: Record<string, "js"> = Object.fromEntries(
(options.extensions ?? []).map((ext) => [ext, "js"]),
Expand All @@ -125,13 +127,15 @@ export const unpluginFactory: UnpluginFactory<Options | undefined> = (opts) => {
},
},
webpack(compiler) {
// handle .json files as JavaScript modules
compiler.options.module.rules.push({
test: (value) =>
(options.extensions ?? []).some((ext) => value.includes(ext)),
type: "javascript/auto", // Treat JSON files as JavaScript modules
});
},
rspack(compiler) {
// handle .json files as JavaScript modules
compiler.options.module.rules.push({
test: (value) =>
(options.extensions ?? []).some((ext) => value.includes(ext)),
Expand All @@ -141,29 +145,39 @@ export const unpluginFactory: UnpluginFactory<Options | undefined> = (opts) => {
};
};

export const unplugin = /* #__PURE__ */ createUnplugin(unpluginFactory);
export const unplugin: UnpluginInstance<Options | undefined, boolean> =
/* #__PURE__ */ createUnplugin(unpluginFactory);

export default unplugin;

// Type guard for OpenAPI 3.0 and 3.1
/**
* Type guard for OpenAPI 3.0 and 3.1
*/
export function isOpenAPIV3(
doc: OpenAPI.Document,
): doc is OpenAPIV3.Document | OpenAPIV3_1.Document {
return "openapi" in doc;
}

// Type guard for OpenAPI 2.0
/**
* Type guard for OpenAPI 2.0
*/
export function isOpenAPIV2(doc: OpenAPI.Document): doc is OpenAPIV2.Document {
return "swagger" in doc;
}

// Utility functions to get the narrowed type
/**
* Utility function to get the narrowed type of the OpenAPI 3.x document
*/
export function getOpenAPIV3(
doc: OpenAPI.Document,
): OpenAPIV3.Document | OpenAPIV3_1.Document | undefined {
return isOpenAPIV3(doc) ? doc : undefined;
}

/**
* Utility function to get the narrowed type of the OpenAPI 2.x document
*/
export function getOpenAPIV2(
doc: OpenAPI.Document,
): OpenAPIV2.Document | undefined {
Expand Down
16 changes: 16 additions & 0 deletions src/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import esbuild from "./esbuild.js";
import rolldown from "./rolldown.js";
import rollup from "./rollup.js";
import rspack from "./rspack.js";
import vite from "./vite.js";
import webpack from "./webpack.js";

export { esbuild, rolldown, rollup, rspack, vite, webpack };

export type { Options } from "./index.js";
export {
isOpenAPIV2,
isOpenAPIV3,
getOpenAPIV2,
getOpenAPIV3,
} from "./index.js";

0 comments on commit 09e1e48

Please sign in to comment.