-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(changeset): move doc out of vinxi core
- Loading branch information
Showing
9 changed files
with
218 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters