Skip to content

Commit

Permalink
feat: untyped hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 29, 2021
1 parent 46f8b96 commit e5ddb8e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 23 deletions.
9 changes: 4 additions & 5 deletions src/builder/mkdist.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { mkdist, MkdistOptions } from 'mkdist'
import { symlink, rmdir } from '../utils'
import type { BuildEntry, BuildContext } from '../types'
import type { MkdistEntry, BuildContext } from '../types'
export async function mkdistBuild (ctx: BuildContext) {
type MkdistEntry = BuildEntry & { builder: 'mkdist' }
const entries = ctx.options.entries.filter(e => e.builder === 'mkdist') as MkdistEntry[]
await ctx.hooks.callHook('mkdist:before', ctx, entries)
await ctx.hooks.callHook('mkdist:entries', ctx, entries)
for (const entry of entries) {
const distDir = entry.outDir!
if (ctx.options.stub) {
Expand All @@ -21,13 +20,13 @@ export async function mkdistBuild (ctx: BuildContext) {
// @ts-ignore
ext: entry.ext
}
await ctx.hooks.callHook('mkdist:options', ctx, mkdistOptions)
await ctx.hooks.callHook('mkdist:entry:options', ctx, entry, mkdistOptions)
const output = await mkdist(mkdistOptions)
ctx.buildEntries.push({
path: distDir,
chunks: [`${output.writtenFiles.length} files`]
})
await ctx.hooks.callHook('mkdist:build', ctx, output)
await ctx.hooks.callHook('mkdist:entry:build', ctx, entry, output)
}
}
await ctx.hooks.callHook('mkdist:done', ctx)
Expand Down
59 changes: 44 additions & 15 deletions src/builder/untyped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,61 @@ import { resolveSchema, generateTypes, generateMarkdown } from 'untyped'
import untypedPlugin from 'untyped/loader/babel'
import jiti from 'jiti'
import { pascalCase } from 'scule'
import type { BuildContext } from '../types'
import type { BuildContext, UntypedEntry, UntypedOutputs } from '../types'

export async function typesBuild (ctx: BuildContext) {
for (const entry of ctx.options.entries.filter(entry => entry.builder === 'untyped')) {
const _require = jiti(ctx.options.rootDir, {
interopDefault: true,
transformOptions: {
babel: {
plugins: [
untypedPlugin
]
const entries = ctx.options.entries.filter(entry => entry.builder === 'untyped') as UntypedEntry[]
await ctx.hooks.callHook('untyped:entries', ctx, entries)

for (const entry of entries) {
const options = {
jiti: {
interopDefault: true,
transformOptions: {
babel: {
plugins: [
untypedPlugin
]
}
}
}
})
}
await ctx.hooks.callHook('untyped:entry:options', ctx, entry, options)

const _require = jiti(ctx.options.rootDir, options.jiti)

const distDir = entry.outDir!
const srcConfig = _require(resolve(ctx.options.rootDir, entry.input))

const defaults = entry.defaults || {}
const schema = resolveSchema(srcConfig, defaults)

await writeFile(resolve(distDir, `${entry.name}.md`), generateMarkdown(schema))
await writeFile(resolve(distDir, `${entry.name}.schema.json`), JSON.stringify(schema, null, 2))
await writeFile(resolve(distDir, `${entry.name}.defaults.json`), JSON.stringify(defaults, null, 2))
if (entry.declaration) {
await writeFile(resolve(distDir, `${entry.name}.d.ts`), generateTypes(schema, pascalCase(entry.name + '-schema')))
await ctx.hooks.callHook('untyped:entry:schema', ctx, entry, schema)

const outputs: UntypedOutputs = {
markdown: {
fileName: resolve(distDir, `${entry.name}.md`),
contents: generateMarkdown(schema)
},
schema: {
fileName: `${entry.name}.schema.json`,
contents: JSON.stringify(schema, null, 2)
},
defaults: {
fileName: `${entry.name}.defaults.json`,
contents: JSON.stringify(defaults, null, 2)
},
declaration: entry.declaration
? {
fileName: `${entry.name}.d.ts`,
contents: generateTypes(schema, pascalCase(entry.name + '-schema'))
}
: undefined
}
await ctx.hooks.callHook('untyped:entry:outputs', ctx, entry, outputs)
for (const output of Object.values(outputs)) {
await writeFile(resolve(distDir, output.fileName), output.contents, 'utf8')
}
}
await ctx.hooks.callHook('untyped:done', ctx)
}
22 changes: 19 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { PackageJson } from 'pkg-types'
import type { Hookable } from 'hookable'
import type { RollupOptions, RollupBuild } from 'rollup'
import type { MkdistOptions } from 'mkdist'
import { Schema } from 'untyped'

export interface BuildEntry {
input: string
Expand All @@ -15,6 +16,7 @@ export interface BuildEntry {
}

export type MkdistEntry = BuildEntry & { builder: 'mkdist' }
export type UntypedEntry = BuildEntry & { builder: 'untyped' }

export interface BuildOptions {
rootDir: string
Expand Down Expand Up @@ -44,6 +46,14 @@ export interface BuildConfig extends Partial<Omit<BuildOptions, 'entries'>> {
hooks?: BuildHooks // eslint-disable-line no-use-before-define
}

export interface UntypedOutput { fileName: string, contents: string }
export interface UntypedOutputs {
markdown: UntypedOutput,
schema: UntypedOutput,
defaults: UntypedOutput,
declaration?: UntypedOutput
}

export interface BuildHooks {
'build:before': (ctx: BuildContext) => void | Promise<void>
'build:done': (ctx: BuildContext) => void | Promise<void>
Expand All @@ -54,10 +64,16 @@ export interface BuildHooks {
'rollup:dts:build': (ctx: BuildContext, build: RollupBuild) => void | Promise<void>
'rollup:done': (ctx: BuildContext) => void | Promise<void>

'mkdist:before': (ctx: BuildContext, entries: MkdistEntry[]) => void | Promise<void>
'mkdist:options': (ctx: BuildContext, options: MkdistOptions) => void | Promise<void>
'mkdist:build': (ctx: BuildContext, output: { writtenFiles: string[] }) => void | Promise<void>
'mkdist:entries': (ctx: BuildContext, entries: MkdistEntry[]) => void | Promise<void>
'mkdist:entry:options': (ctx: BuildContext, entry: MkdistEntry, options: MkdistOptions) => void | Promise<void>
'mkdist:entry:build': (ctx: BuildContext, entry: MkdistEntry, output: { writtenFiles: string[] }) => void | Promise<void>
'mkdist:done': (ctx: BuildContext) => void | Promise<void>

'untyped:entries': (ctx: BuildContext, entries: UntypedEntry[]) => void | Promise<void>
'untyped:entry:options': (ctx: BuildContext, entry: UntypedEntry, options: any) => void | Promise<void>
'untyped:entry:schema': (ctx: BuildContext, entry: UntypedEntry, schema: Schema) => void | Promise<void>
'untyped:entry:outputs': (ctx: BuildContext, entry: UntypedEntry, templates: UntypedTemplates) => void | Promise<void>
'untyped:done': (ctx: BuildContext) => void | Promise<void>
}

export function defineBuildConfig (config: BuildConfig): BuildConfig {
Expand Down

0 comments on commit e5ddb8e

Please sign in to comment.