-
Notifications
You must be signed in to change notification settings - Fork 82
/
untyped.ts
68 lines (61 loc) 路 2.15 KB
/
untyped.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { writeFile } from 'fs/promises'
import { resolve } from 'pathe'
import { resolveSchema, generateTypes, generateMarkdown } from 'untyped'
// @ts-ignore
import untypedPlugin from 'untyped/babel-plugin'
import jiti from 'jiti'
import { pascalCase } from 'scule'
import type { BuildContext, UntypedBuildEntry, UntypedOutputs } from '../types'
export async function typesBuild (ctx: BuildContext) {
const entries = ctx.options.entries.filter(entry => entry.builder === 'untyped') as UntypedBuildEntry[]
await ctx.hooks.callHook('untyped:entries', ctx, entries)
for (const entry of entries) {
const options = {
jiti: {
esmResolve: true,
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 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, {
interfaceName: 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)
}