Files
supabase/spec/parser/tsdoc.ts
Copple e486815ab0 Docs: hide generated (#9078)
* removes all generated files

* ignore all generated files

* moves the docs generator into docusaurus

* clean up old files from the generator

* generator -> parser

* Adds the latest spec

* fixing generated path

* updates specs

* delete generated files

* keep the generated folder

* keep generated folder

* Adds a dart generator

* Addds new auth doc for Dart v1

* Adds postgrest method in Dart doc

* Adds realtime docs to Dart v1

* Adds storage docs to Dart doc

* Adds filters and modifiers to Dart docs spec file

* fix: label used in dart sidebar to match spec file for Dart docs

* final items for generator

* Adds a build command

* Adds typescript

* fix broken links

* fix build

* fix import error

* lowercase helpers

* clean missing file

Co-authored-by: dshukertjr <18113850+dshukertjr@users.noreply.github.com>
2022-09-23 11:40:07 +02:00

88 lines
2.2 KiB
TypeScript

import { writeToDisk } from './helpers'
import stringify from 'json-stringify-safe'
const fs = require('fs')
const main = (command: string[], options: any) => {
handleInput(command[0], options)
}
// Run everything
const argv = require('minimist')(process.argv.slice(2))
main(argv['_'], argv)
function handleInput(command: string, options: any) {
switch (command) {
case 'dereference':
dereference(options)
break
default:
console.log('Unrecognized command:', command)
break
}
}
interface KV {
[key: string]: any
}
async function dereference({
input,
output,
}: {
input: string
output: string
}) {
console.log('input', input)
const specRaw = fs.readFileSync(input, 'utf8')
const spec = JSON.parse(specRaw)
const kv = chilrenReducer({}, spec)
// console.log('kv', kv)
const dereferenced = dereferenceReducer(spec, kv)
// console.log('dereferenced', dereferenced)
await writeToDisk(output, stringify(dereferenced, null, 2))
// console.log('JSON.stringify(dereferenced)', JSON.stringify(spec))
}
function chilrenReducer(acc: KV, child: any): KV {
if (!!child.children) {
child.children.forEach((x: any) => chilrenReducer(acc, x))
}
const { id }: { id: string } = child
acc[id] = { ...child }
return acc
}
// Recurse through all children, and if the `type.type` == 'reference'
// then it will add a key "dereferecnced" to the object.
function dereferenceReducer(child: any, kv: KV) {
if (!!child.children) {
child.children.forEach((x: any) => dereferenceReducer(x, kv))
}
if (!!child.signatures) {
child.signatures.forEach((x: any) => dereferenceReducer(x, kv))
}
if (!!child.parameters) {
child.parameters.forEach((x: any) => dereferenceReducer(x, kv))
}
const final = { ...child }
if (
// For now I can only dereference parameters
// because anything else is producing an error when saving to file:
// TypeError: Converting circular structure to JSON
final.kindString == 'Parameter' &&
final.type?.type == 'reference' &&
final.type?.id
) {
const dereferenced = kv[final.type.id]
final.type.dereferenced = dereferenced || {}
return final
} else {
return final
}
}