Skip to content

Commit

Permalink
docs(changeset): move doc out of vinxi core
Browse files Browse the repository at this point in the history
  • Loading branch information
nksaraf committed Oct 17, 2023
1 parent 197c459 commit a009733
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 3 deletions.
8 changes: 8 additions & 0 deletions .changeset/stale-planes-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@vinxi/openapi": patch
"@vinxi/doc": patch
"vinxi": patch
"@vinxi/deno-doc": patch
---

move doc out of vinxi core
2 changes: 1 addition & 1 deletion packages/doc/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@vinxi/doc",
"name": "@vinxi/deno-doc",
"main": "deno_doc.cjs",
"version": "0.0.2"
}
7 changes: 7 additions & 0 deletions packages/vinxi-doc/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# @vinxi/doc

## 0.0.2

### Patch Changes

- 5b0304b: release vinxi/doc
127 changes: 127 additions & 0 deletions packages/vinxi-doc/doc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import fs from "node:fs";
import { createRequire } from "node:module";
import path from "node:path";
import { fileURLToPath } from "node:url";

export async function doc(src) {
const require = createRequire(import.meta.url);
const mod = require("@vinxi/deno-doc");
const resolve = require("resolve");
const path = require("path");
const Module = require("module");
const fs = require("fs");

const resolveFrom = (fromDirectory, moduleId, silent) => {
if (typeof fromDirectory !== "string") {
throw new TypeError(
`Expected \`fromDir\` to be of type \`string\`, got \`${typeof fromDirectory}\``,
);
}

if (typeof moduleId !== "string") {
throw new TypeError(
`Expected \`moduleId\` to be of type \`string\`, got \`${typeof moduleId}\``,
);
}

try {
fromDirectory = fs.realpathSync(fromDirectory);
} catch (error) {
if (error.code === "ENOENT") {
fromDirectory = path.resolve(fromDirectory);
} else if (silent) {
return;
} else {
throw error;
}
}

const fromFile = path.join(fromDirectory, "noop.js");

const resolveFileName = () =>
Module._resolveFilename(moduleId, {
id: fromFile,
filename: fromFile,
paths: Module._nodeModulePaths(fromDirectory),
});

if (silent) {
try {
return resolveFileName();
} catch (error) {
return;
}
}

return resolveFileName();
};

function resolvePackage(specifier, referrer) {
try {
const resolved = resolve.sync(specifier, {
preserveSymlinks: false,
basedir: path.dirname(new URL(referrer).pathname),
packageFilter: (pkg, _pkgPath) => {
if (pkg.types?.length) {
pkg.main = pkg.types;
}
return pkg;
},
extensions: [".ts", ".tsx", ".d.ts"],
});
return "file://" + resolved;
} catch (e) {
console.error(e);
return null;
}
}

function resolveFn(specifier, referrer) {
if (specifier.startsWith(".") || specifier.startsWith("/")) {
if (!specifier.endsWith(".ts") && !specifier.endsWith(".tsx")) {
for (const p of [
specifier + ".ts",
specifier + ".tsx",
specifier + ".d.ts",
specifier + "/index.ts",
specifier + "/index.tsx",
specifier + "/index.d.ts",
]) {
let p1 = path.dirname(new URL(referrer).pathname);
fs.existsSync(path.join(p1, p));
if (fs.existsSync(path.join(p1, p))) {
let resolved = path.join(path.dirname(referrer), p);
return resolved;
}
}
}
} else {
return resolvePackage(specifier, referrer);
}
}
return await mod.doc(
src,
false,
async (id) => {
// if (!id.startsWith("file://")) {
// return null;
// }
if (id.endsWith(".css")) {
return {
kind: "module",
specifier: id.replace(/\.css$/, ".css.ts"),
content: `export default {}`,
};
}
const test = await fs.promises.readFile(fileURLToPath(id), {
encoding: "utf-8",
});
return {
kind: "module",
specifier: id,
content: test,
};
},
resolveFn,
);
}
12 changes: 12 additions & 0 deletions packages/vinxi-doc/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@vinxi/doc",
"module": "doc.js",
"type": "module",
"version": "0.0.2",
"exports": {
".": "./doc.js"
},
"dependencies": {
"@vinxi/deno-doc": "0.0.2"
}
}
47 changes: 47 additions & 0 deletions packages/vinxi-openapi/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { eventHandler, lazyEventHandler } from "vinxi/server";

export default lazyEventHandler(async () => {
const { doc } = await import("@vinxi/doc");

return eventHandler(async (event) => {
const routers = import.meta.env.ROUTERS;

console.log(
await Promise.all(
routers.map((router) => import.meta.env.MANIFEST[router].routes()),
),
);

/** @type {import('openapi-types').OpenAPIV3.Document['paths']} */
const paths = {};
for (var router of routers) {
const base = import.meta.env.MANIFEST[router].base;
const routes = await import.meta.env.MANIFEST[router].routes();
for (var route of routes) {
const path = new URL(base + "." + route.path, "http://localhost:3000")
.pathname;
paths[path] = {
get: {
"": {},
},
};
}
}
if (event.path === "/openapi") {
/** @type {import('openapi-types').OpenAPIV3.Document} */
const x = {
paths,
openapi: "3.0.0",
info: {
title: "Vinxi",
version: "1.0.0",
description: "Vinxi",
},
};
return x;
}
console.log(event.path);

return await doc(`file://` + event.path);
});
});
11 changes: 11 additions & 0 deletions packages/vinxi-openapi/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { fileURLToPath } from "node:url";

export function openapi() {
return {
name: "docs",
mode: "handler",
base: "/spec",
handler: fileURLToPath(new URL("./handler.js", import.meta.url)),
target: "server",
};
}
6 changes: 5 additions & 1 deletion packages/vinxi-openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
"type": "module",
"module": "index.js",
"dependencies": {
"@vinxi/doc": "workspace:0.0.2",
"openapi-types": "^12.1.3"
},
"exports": {
".": "./index.js"
},
"devDependencies": {
"vinxi": "workspace:^"
}
}
}
1 change: 0 additions & 1 deletion packages/vinxi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@
"@types/micromatch": "^4.0.2",
"@types/serve-static": "^1.15.2",
"@types/ws": "^8.5.5",
"@vinxi/doc": "workspace:^",
"c12": "^1.4.2",
"chokidar": "^3.5.3",
"consola": "^3.2.3",
Expand Down

0 comments on commit a009733

Please sign in to comment.