diff --git a/README.md b/README.md index 59b29b2..096c541 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,8 @@ import { fileURLToPath } from "url"; const root = dirname(fileURLToPath(import.meta.url)) generate({ - sourceDirectory: join(root, "src"), - outputConfig: [ + sourceDir: join(root, "src"), + outputs: [ { renderer: defaultRenderers.jsonSchema, folder: join(root, "schema") @@ -40,7 +40,7 @@ generate({ }) ``` -The tool takes all TypeScript files from the `sourceDirectory` and then runs each TypeScript file inside through each output configuration. Built-in output renderers are for JSON Schema definitions and Markdown documentation, so all you need to do is to specify absolute folder paths for them and import them from the module. It does not do any cleanup, it only overwrites existing files. Types that are referenced from other files are also referenced this way in JSON Schema and Markdown, so that the output is a mirror of the TypeScript files without any duplicate definitions in both JSON Schema and Markdown. Note that all types must be present in the specified directory or its subdirectories, otherwise references/links in JSON schema and Markdown will not work. +The tool takes all TypeScript files from the `sourceDir` and its subdirectories and then runs each TypeScript file inside through each output configuration. Built-in output renderers are for JSON Schema definitions and Markdown documentation, so all you need to do is to specify absolute folder paths for them and import them from the module. It does not do any cleanup, it only overwrites existing files. Types that are referenced from other files are also referenced this way in JSON Schema and Markdown, so that the output is a mirror of the TypeScript files without any duplicate definitions in both JSON Schema and Markdown. The relative folder structure is mirrored as well. Note that all types must be present in the specified directory or its subdirectories, otherwise references/links in JSON schema and Markdown will not work. You can also build your own renderer by conforming to the `Renderer` type that can be imported. diff --git a/src/main.ts b/src/main.ts index 641fb8f..565281d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -22,24 +22,26 @@ export type Output = { renderer: Renderer } -export const defaultRenderers = { +export const defaultRenderers = Object.freeze({ jsonSchema: jsonSchemaRenderer, markdown: markdownRenderer, -} +}) export type GeneratorOptions = { - sourceDirectory: string - outputConfig: Output[] + sourceDir: string + outputs: Output[] debug?: boolean - filterFiles?: (fileName: string) => boolean + filterFile?: (fileName: string) => boolean } -export const generate = ({ - sourceDirectory, - outputConfig, - debug = false, - filterFiles, -}: GeneratorOptions): void => { +export const generate = (options: GeneratorOptions): void => { + const { + sourceDir, + outputs, + debug = false, + filterFile, + } = options + const flattenTypeScriptFileNamesFromDir = (dirPath: string): string[] => { const dirEntryToFilePath = (dirEntry: Dirent) => join(dirPath, dirEntry.name).split(sep).join("/") @@ -58,26 +60,26 @@ export const generate = ({ }) } - const tsFiles = flattenTypeScriptFileNamesFromDir(sourceDirectory) + const tsFiles = flattenTypeScriptFileNamesFromDir(sourceDir) const program = ts.createProgram(tsFiles, { strict: true }) // KEEP ALWAYS, SIDE EFFECT: it fills the parent references of nodes const checker = program.getTypeChecker() - outputConfig.forEach(({ folder }) => mkdirSync(folder, { recursive: true })) + outputs.forEach(({ folder }) => mkdirSync(folder, { recursive: true })) const rootFiles = program .getSourceFiles() .filter(file => tsFiles.includes(file.fileName)) const filteredFiles = - filterFiles - ? rootFiles.filter(file => filterFiles(file.fileName)) + filterFile + ? rootFiles.filter(file => filterFile(file.fileName)) : rootFiles filteredFiles.forEach(file => { - const relativePath = relative(sourceDirectory, file.fileName) + const relativePath = relative(sourceDir, file.fileName) const dir = dirname(relativePath) const name = basename(relativePath, ".ts") @@ -88,7 +90,7 @@ export const generate = ({ writeFileSync(`${file.fileName}.ast.json`, JSON.stringify(ast, undefined, 2)) } - outputConfig.forEach(({ folder, renderer: { transformer, fileExtension } }) => { + outputs.forEach(({ folder, renderer: { transformer, fileExtension } }) => { const outputDir = join(folder, dir) mkdirSync(outputDir, { recursive: true })