Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add doc generation for AI #17

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/modules/Configuration.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export declare const ConfigurationSchema: Schema.Struct<{
examplesCompilerOptions: Schema.optional<
Schema.Union<[typeof Schema.String, Schema.Record$<typeof Schema.String, typeof Schema.Unknown>]>
>
enableAI: Schema.optional<typeof Schema.Boolean>
enableJson: Schema.optional<typeof Schema.Boolean>
}>
```

Expand All @@ -77,6 +79,8 @@ export interface ConfigurationShape {
readonly exclude: ReadonlyArray<string>
readonly parseCompilerOptions: Record<string, unknown>
readonly examplesCompilerOptions: Record<string, unknown>
readonly enableAI: boolean
readonly enableJson: boolean
}
```

Expand Down
25 changes: 25 additions & 0 deletions docs/modules/Domain.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Added in v1.0.0

<h2 class="text-delta">Table of contents</h2>

- [accessors](#accessors)
- [printablesFromModule](#printablesfrommodule)
- [constructors](#constructors)
- [createClass](#createclass)
- [createConstant](#createconstant)
Expand All @@ -37,13 +39,26 @@ Added in v1.0.0
- [Module (interface)](#module-interface)
- [NamedDoc (interface)](#nameddoc-interface)
- [Namespace (interface)](#namespace-interface)
- [Printable (type alias)](#printable-type-alias)
- [Property (interface)](#property-interface)
- [TypeAlias (interface)](#typealias-interface)
- [sorting](#sorting)
- [ByPath](#bypath)

---

# accessors

## printablesFromModule

**Signature**

```ts
export declare const printablesFromModule: (module: Module) => ReadonlyArray<Printable>
```
Added in v1.0.0
# constructors
## createClass
Expand Down Expand Up @@ -370,6 +385,16 @@ export interface Namespace extends NamedDoc {

Added in v1.0.0

## Printable (type alias)

**Signature**

```ts
export type Printable = Class | Constant | Export | Function | Interface | TypeAlias | Namespace
```
Added in v1.0.0
## Property (interface)
**Signature**
Expand Down
15 changes: 15 additions & 0 deletions docs/modules/Markdown.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Added in v1.0.0

- [printers](#printers)
- [printModule](#printmodule)
- [printPrintableForAI](#printprintableforai)

---

Expand All @@ -40,3 +41,17 @@ console.log(Markdown.printModule(m, 0))
```

Added in v1.0.0

## printPrintableForAI

**Signature**

```ts
export declare const printPrintableForAI: (
projectName: string,
module: Domain.Module,
printable: Domain.Printable
) => Effect.Effect<string, never, never>
```
Added in v1.0.0
10 changes: 10 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@
],
"description": "tsconfig for the examples options (or path to a tsconfig)",
"default": {}
},
"enableAI": {
"type": "boolean",
"description": "Whether or not to enable AI for the examples",
"default": true
},
"enableJson": {
"type": "boolean",
"description": "Whether or not to output JSON files",
"default": true
}
},
"additionalProperties": false
Expand Down
26 changes: 25 additions & 1 deletion src/CLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@ const examplesCompilerOptions = Options.file("examples-tsconfig-file", { exists:
Options.optional
)

const enableAI = Options.boolean("no-enable-ai", {
ifPresent: false,
negationNames: ["enable-ai"]
}).pipe(
Options.withFallbackConfig(Config.boolean("enableAI")),
Options.withDefault(true),
Options.withDescription(
"Whether or not to generate AI documentation for the project"
)
)

const enableJson = Options.boolean("no-enable-json", {
ifPresent: false,
negationNames: ["enable-json"]
}).pipe(
Options.withFallbackConfig(Config.boolean("enableJson")),
Options.withDefault(true),
Options.withDescription(
"Whether or not to generate JSON output for the project"
)
)

const options = {
projectHomepage,
srcDir,
Expand All @@ -168,7 +190,9 @@ const options = {
runExamples,
exclude,
parseCompilerOptions,
examplesCompilerOptions
examplesCompilerOptions,
enableAI,
enableJson
}

/** @internal */
Expand Down
12 changes: 12 additions & 0 deletions src/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ export const ConfigurationSchema = Schema.Struct({
examplesCompilerOptions: Schema.optional(compilerOptionsSchema).annotations({
description: "tsconfig for the examples options (or path to a tsconfig)",
default: {}
}),
enableAI: Schema.optional(Schema.Boolean).annotations({
description: "Whether or not to enable AI for the examples",
default: true
}),
enableJson: Schema.optional(Schema.Boolean).annotations({
description: "Whether or not to output JSON files",
default: true
})
}).annotations({ identifier: "ConfigurationSchema" })

Expand All @@ -100,6 +108,8 @@ export interface ConfigurationShape {
readonly exclude: ReadonlyArray<string>
readonly parseCompilerOptions: Record<string, unknown>
readonly examplesCompilerOptions: Record<string, unknown>
readonly enableAI: boolean
readonly enableJson: boolean
}

/**
Expand Down Expand Up @@ -244,6 +254,8 @@ export const load = (args: {
readonly exclude: ReadonlyArray<string>
readonly parseCompilerOptions: Option.Option<string | Record<string, unknown>>
readonly examplesCompilerOptions: Option.Option<string | Record<string, unknown>>
readonly enableAI: boolean
readonly enableJson: boolean
}) =>
Effect.gen(function*(_) {
// Extract the requisite services
Expand Down
87 changes: 83 additions & 4 deletions src/Core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import * as CommandExecutor from "@effect/platform/CommandExecutor"
import * as FileSystem from "@effect/platform/FileSystem"
import * as Path from "@effect/platform/Path"
import chalk from "chalk"
import { pipe } from "effect"
import * as Array from "effect/Array"
import * as Chunk from "effect/Chunk"
import * as Effect from "effect/Effect"
import { pipe } from "effect/Function"
import * as Option from "effect/Option"
import * as Stream from "effect/Stream"
import * as String from "effect/String"
import * as Glob from "glob"
import * as Configuration from "./Configuration.js"
import type * as Domain from "./Domain.js"
import * as Domain from "./Domain.js"
import { DocgenError } from "./Error.js"
import * as File from "./File.js"
import { printModule } from "./Markdown.js"
import { printModule, printPrintableForAI } from "./Markdown.js"
import * as Parser from "./Parser.js"
import * as Process from "./Process.js"

Expand Down Expand Up @@ -436,7 +437,9 @@ const getMarkdown = (modules: ReadonlyArray<Domain.Module>) =>
const index = yield* _(getMarkdownIndex)
const yml = yield* _(getMarkdownConfigYML)
const moduleFiles = yield* _(getModuleMarkdownFiles(modules))
return [homepage, index, yml, ...moduleFiles]
const aiFiles = yield* _(maybeGetAIMarkdownFiles(modules))
const jsonFiles = yield* _(maybeGetJsonFiles(modules))
return [homepage, index, yml, ...moduleFiles, ...aiFiles, ...jsonFiles]
})

const getMarkdownHomepage = Effect.gen(function*(_) {
Expand Down Expand Up @@ -538,6 +541,14 @@ const getModuleMarkdownOutputPath = (module: Domain.Module) =>
))
)

const getAIMarkdownOutputPath = (module: Domain.Module, printable: Domain.Printable) =>
Effect.map(Effect.all([Configuration.Configuration, Path.Path]), ([config, path]) =>
path.join(
config.outDir,
"ai",
`${module.path.slice(1).join("-").replace(/\.ts$/, "")}-${printable.name}.md`
))

const getModuleMarkdownFiles = (modules: ReadonlyArray<Domain.Module>) =>
Effect.forEach(modules, (module, order) =>
Effect.gen(function*(_) {
Expand All @@ -546,6 +557,74 @@ const getModuleMarkdownFiles = (modules: ReadonlyArray<Domain.Module>) =>
return File.createFile(outputPath, content, true)
}))

const getAIMarkdownFiles = (projectName: string, modules: ReadonlyArray<Domain.Module>) =>
Effect.gen(function*(_) {
const aiModules = pipe(
modules,
Array.flatMap((module) =>
pipe(
Domain.printablesFromModule(module),
Array.map((printable) => ({ module, printable }))
)
),
Array.filter(({ printable }) => printable.description._tag === "Some")
)

return yield* _(Effect.forEach(aiModules, ({ module, printable }) =>
Effect.gen(function*(_) {
const outputPath = yield* _(getAIMarkdownOutputPath(module, printable))
const content = yield* _(printPrintableForAI(projectName, module, printable))
return File.createFile(outputPath, content, true)
})))
})

const getJsonFiles = (projectName: string, modules: ReadonlyArray<Domain.Module>) =>
Effect.gen(function*() {
const config = yield* Configuration.Configuration
const path = yield* Path.Path
const printables = pipe(
modules,
Array.bindTo("module"),
Array.bind("printable", ({ module }) => Domain.printablesFromModule(module)),
Array.map(({ module, printable }) => {
return {
_tag: printable._tag,
module: {
name: module.name,
path: module.path
},
project: projectName,
name: printable.name,
description: Option.getOrNull(printable.description),
deprecated: printable.deprecated,
examples: printable.examples.map((example) => example.body),
since: Option.getOrNull(printable.since),
category: Option.getOrNull(printable.category),
signature: "signature" in printable ? printable.signature : null
}
})
)

return [File.createFile(
path.join(config.outDir, `${projectName.replace("/", "-")}.json`),
JSON.stringify(printables, null, 2),
true
)]
})

const maybeGetAIMarkdownFiles = (modules: ReadonlyArray<Domain.Module>) =>
Effect.flatMap(
Configuration.Configuration,
(config) =>
config.enableAI ? getAIMarkdownFiles(config.projectName, modules) : Effect.succeed([])
)

const maybeGetJsonFiles = (modules: ReadonlyArray<Domain.Module>) =>
Effect.flatMap(
Configuration.Configuration,
(config) => config.enableJson ? getJsonFiles(config.projectName, modules) : Effect.succeed([])
)

const writeMarkdown = (files: ReadonlyArray<File.File>) =>
Effect.gen(function*(_) {
const config = yield* _(Configuration.Configuration)
Expand Down
31 changes: 31 additions & 0 deletions src/Domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ export interface Namespace extends NamedDoc {
readonly namespaces: ReadonlyArray<Namespace>
}

/**
* @category model
* @since 1.0.0
*/
export type Printable =
| Class
| Constant
| Export
| Function
| Interface
| TypeAlias
| Namespace

// -------------------------------------------------------------------------------------
// constructors
// -------------------------------------------------------------------------------------
Expand Down Expand Up @@ -282,6 +295,24 @@ export const createExport = (doc: NamedDoc, signature: string): Export => ({
signature
})

// -------------------------------------------------------------------------------------
// accessors
// -------------------------------------------------------------------------------------

/**
* @category accessors
* @since 1.0.0
*/
export const printablesFromModule = (module: Module): ReadonlyArray<Printable> =>
[
module.classes,
module.constants,
module.exports,
module.functions,
module.interfaces,
module.typeAliases
].flat()

/**
* @category constructors
* @since 1.0.0
Expand Down
Loading
Loading