|
| 1 | +import * as fs from 'fs'; |
| 2 | +import { resolve } from 'path'; |
| 3 | +import { inspect } from 'util'; |
| 4 | +import { ViteDevServer } from 'vite'; |
| 5 | +export default function autoAPI() { |
| 6 | + return { |
| 7 | + name: 'watch-monorepo-changes', |
| 8 | + configureServer(server: ViteDevServer) { |
| 9 | + const watchPath = resolve(__dirname, '../../packages/kit-headless'); |
| 10 | + server.watcher.on('change', (file: string) => { |
| 11 | + if (file.startsWith(watchPath)) { |
| 12 | + loopOnAllChildFiles(file); |
| 13 | + } |
| 14 | + }); |
| 15 | + }, |
| 16 | + }; |
| 17 | +} |
| 18 | +// the object should have this general structure, arranged from parent to child |
| 19 | +// componentName:[subComponent,subcomponent,...] |
| 20 | +// subComponentName:[publicType,publicType,...] |
| 21 | +// publicType:[{ comment,prop,type },{ comment,prop,type },...] |
| 22 | +// THEY UPPER-MOST KEY IS ALWAYS USED AS A HEADING |
| 23 | +export type ComponentParts = Record<string, SubComponents>; |
| 24 | +type SubComponents = SubComponent[]; |
| 25 | +export type SubComponent = Record<string, PublicType[]>; |
| 26 | +export type PublicType = Record<string, ParsedProps[]>; |
| 27 | +type ParsedProps = { |
| 28 | + comment: string; |
| 29 | + prop: string; |
| 30 | + type: string; |
| 31 | +}; |
| 32 | +function parseSingleComponentFromDir(path: string, ref: SubComponents) { |
| 33 | + const component_name = /\/([\w-]*).tsx/.exec(path); |
| 34 | + if (component_name === null || component_name[1] === null) { |
| 35 | + // may need better behavior |
| 36 | + return; |
| 37 | + } |
| 38 | + const sourceCode = fs.readFileSync(path, 'utf-8'); |
| 39 | + const comments = extractPublicTypes(sourceCode); |
| 40 | + const parsed: PublicType[] = []; |
| 41 | + for (const comment of comments) { |
| 42 | + const api = extractComments(comment.string); |
| 43 | + const pair: PublicType = { [comment.label]: api }; |
| 44 | + parsed.push(pair); |
| 45 | + } |
| 46 | + const completeSubComponent: SubComponent = { [component_name[1]]: parsed }; |
| 47 | + ref.push(completeSubComponent); |
| 48 | + return ref; |
| 49 | +} |
| 50 | + |
| 51 | +function extractPublicTypes(strg: string) { |
| 52 | + const getPublicTypes = /type Public([A-Z][\w]*)*[\w\W]*?{([\w|\W]*?)}(;| &)/gm; |
| 53 | + const cms = []; |
| 54 | + let groups; |
| 55 | + while ((groups = getPublicTypes.exec(strg)) !== null) { |
| 56 | + const string = groups[2]; |
| 57 | + cms.push({ label: groups[1], string }); |
| 58 | + } |
| 59 | + return cms; |
| 60 | +} |
| 61 | +function extractComments(strg: string): ParsedProps[] { |
| 62 | + const magical_regex = |
| 63 | + /^\s*?\/[*]{2}\n?([\w|\W|]*?)\s*[*]{1,2}[/]\n[ ]*([\w|\W]*?): ([\w|\W]*?);?$/gm; |
| 64 | + |
| 65 | + const cms = []; |
| 66 | + let groups; |
| 67 | + |
| 68 | + while ((groups = magical_regex.exec(strg)) !== null) { |
| 69 | + const trimStart = /^ *|(\* *)/g; |
| 70 | + const comment = groups[1].replaceAll(trimStart, ''); |
| 71 | + const prop = groups[2]; |
| 72 | + const type = groups[3]; |
| 73 | + cms.push({ comment, prop, type }); |
| 74 | + } |
| 75 | + return cms; |
| 76 | +} |
| 77 | +function writeToDocs(fullPath: string, componentName: string, api: ComponentParts) { |
| 78 | + if (fullPath.includes('kit-headless')) { |
| 79 | + const relDocPath = `../website/src/routes//docs/headless/${componentName}`; |
| 80 | + const fullDocPath = resolve(__dirname, relDocPath); |
| 81 | + const dirPath = fullDocPath.concat('/auto-api'); |
| 82 | + |
| 83 | + if (!fs.existsSync(dirPath)) { |
| 84 | + fs.mkdirSync(dirPath); |
| 85 | + } |
| 86 | + const json = JSON.stringify(api, null, 2); |
| 87 | + const hacky = `export const api=${json}`; |
| 88 | + |
| 89 | + try { |
| 90 | + fs.writeFileSync(dirPath.concat('/api.ts'), hacky); |
| 91 | + console.log('auto-api: succesfully genereated new json!!! :)'); |
| 92 | + } catch (err) { |
| 93 | + return; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | +function loopOnAllChildFiles(filePath: string) { |
| 98 | + const childComponentRegex = /\/([\w-]*).tsx$/.exec(filePath); |
| 99 | + if (childComponentRegex === null) { |
| 100 | + return; |
| 101 | + } |
| 102 | + const parentDir = filePath.replace(childComponentRegex[0], ''); |
| 103 | + const componentRegex = /\/(\w*)$/.exec(parentDir); |
| 104 | + if (!fs.existsSync(parentDir) || componentRegex == null) { |
| 105 | + return; |
| 106 | + } |
| 107 | + const componentName = componentRegex[1]; |
| 108 | + const allParts: SubComponents = []; |
| 109 | + const store: ComponentParts = { [componentName]: allParts }; |
| 110 | + fs.readdirSync(parentDir).forEach((fileName) => { |
| 111 | + if (/tsx$/.test(fileName)) { |
| 112 | + const fullPath = parentDir + '/' + fileName; |
| 113 | + parseSingleComponentFromDir(fullPath, store[componentName]); |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + writeToDocs(filePath, componentName, store); |
| 118 | +} |
0 commit comments