@sqldoc/ns-codegen 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +32 -0
- package/src/__tests__/plugin.test.ts +232 -0
- package/src/index.ts +112 -0
- package/src/types.ts +88 -0
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "module",
|
|
3
|
+
"name": "@sqldoc/ns-codegen",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"description": "Code generation namespace for sqldoc -- runs configurable templates against compiled Atlas schema",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./src/index.ts",
|
|
9
|
+
"import": "./src/index.ts",
|
|
10
|
+
"default": "./src/index.ts"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"main": "./src/index.ts",
|
|
14
|
+
"types": "./src/index.ts",
|
|
15
|
+
"files": [
|
|
16
|
+
"src",
|
|
17
|
+
"package.json"
|
|
18
|
+
],
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"@sqldoc/core": "0.0.1",
|
|
21
|
+
"@sqldoc/atlas": "0.0.1"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"typescript": "^5.9.3",
|
|
25
|
+
"vitest": "^4.1.0",
|
|
26
|
+
"@sqldoc/core": "0.0.1",
|
|
27
|
+
"@sqldoc/atlas": "0.0.1"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"test": "vitest run"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import type { ProjectContext } from '@sqldoc/core'
|
|
2
|
+
import { describe, expect, it } from 'vitest'
|
|
3
|
+
import plugin from '../index'
|
|
4
|
+
|
|
5
|
+
function makeCtx(overrides: Partial<ProjectContext> = {}): ProjectContext {
|
|
6
|
+
return {
|
|
7
|
+
outputs: [],
|
|
8
|
+
mergedSql: '',
|
|
9
|
+
allFileTags: [],
|
|
10
|
+
docsMeta: [],
|
|
11
|
+
config: {},
|
|
12
|
+
projectRoot: '/tmp/test',
|
|
13
|
+
atlasRealm: { schemas: [{ name: 'public', tables: [] }] },
|
|
14
|
+
...overrides,
|
|
15
|
+
} as unknown as ProjectContext
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe('ns-codegen plugin', () => {
|
|
19
|
+
describe('tag definitions', () => {
|
|
20
|
+
it('defines rename tag targeting table, column, view', () => {
|
|
21
|
+
expect(plugin.tags.rename).toBeDefined()
|
|
22
|
+
expect(plugin.tags.rename.targets).toEqual(['table', 'column', 'view'])
|
|
23
|
+
expect(plugin.tags.rename.args).toEqual([{ type: 'string' }, { type: 'string' }])
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('defines skip tag targeting table, column, view', () => {
|
|
27
|
+
expect(plugin.tags.skip).toBeDefined()
|
|
28
|
+
expect(plugin.tags.skip.targets).toEqual(['table', 'column', 'view'])
|
|
29
|
+
expect(plugin.tags.skip.args).toEqual([{ type: 'string' }])
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('defines type tag targeting column only', () => {
|
|
33
|
+
expect(plugin.tags.type).toBeDefined()
|
|
34
|
+
expect(plugin.tags.type.targets).toEqual(['column'])
|
|
35
|
+
expect(plugin.tags.type.args).toEqual([{ type: 'string' }, { type: 'string' }])
|
|
36
|
+
})
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
describe('afterCompile', () => {
|
|
40
|
+
it('returns empty files when config.templates is empty array', async () => {
|
|
41
|
+
const ctx = makeCtx({ config: { templates: [] } })
|
|
42
|
+
const result = await plugin.afterCompile!(ctx)
|
|
43
|
+
expect(result.files).toEqual([])
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('returns empty files when config.templates is undefined', async () => {
|
|
47
|
+
const ctx = makeCtx({ config: {} })
|
|
48
|
+
const result = await plugin.afterCompile!(ctx)
|
|
49
|
+
expect(result.files).toEqual([])
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('throws when atlasRealm is not provided', async () => {
|
|
53
|
+
const ctx = makeCtx({
|
|
54
|
+
config: { templates: [{ template: 'some-template', output: 'out' }] },
|
|
55
|
+
atlasRealm: undefined,
|
|
56
|
+
})
|
|
57
|
+
await expect(plugin.afterCompile!(ctx)).rejects.toThrow('ns-codegen requires Atlas schema')
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('calls template.generate with correct TemplateContext fields', async () => {
|
|
61
|
+
const recordedCtx: any[] = []
|
|
62
|
+
const _mockTemplate = {
|
|
63
|
+
generate: (ctx: any) => {
|
|
64
|
+
recordedCtx.push(ctx)
|
|
65
|
+
return { files: [{ path: 'test.ts', content: '// test' }] }
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// We create a real file so afterCompile can dynamic-import it.
|
|
70
|
+
const fs = await import('node:fs')
|
|
71
|
+
const os = await import('node:os')
|
|
72
|
+
const path = await import('node:path')
|
|
73
|
+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegen-test-'))
|
|
74
|
+
const templatePath = path.join(tmpDir, 'mock-template.mjs')
|
|
75
|
+
fs.writeFileSync(
|
|
76
|
+
templatePath,
|
|
77
|
+
`
|
|
78
|
+
export function generate(ctx) {
|
|
79
|
+
globalThis.__codegen_test_ctx__ = ctx
|
|
80
|
+
return { files: [{ path: 'out.ts', content: '// generated' }] }
|
|
81
|
+
}
|
|
82
|
+
`,
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
const realm = { schemas: [{ name: 'public', tables: [{ name: 'users' }] }] }
|
|
86
|
+
const allFileTags = [{ sourceFile: 'test.sql', objects: [] }]
|
|
87
|
+
const docsMeta = [{ annotations: [{ object: 'users', text: 'test' }] }]
|
|
88
|
+
const templateConfig = { option1: 'value1' }
|
|
89
|
+
|
|
90
|
+
const ctx = makeCtx({
|
|
91
|
+
config: {
|
|
92
|
+
templates: [
|
|
93
|
+
{
|
|
94
|
+
template: templatePath,
|
|
95
|
+
output: 'generated',
|
|
96
|
+
config: templateConfig,
|
|
97
|
+
},
|
|
98
|
+
],
|
|
99
|
+
},
|
|
100
|
+
atlasRealm: realm,
|
|
101
|
+
allFileTags,
|
|
102
|
+
docsMeta,
|
|
103
|
+
} as any)
|
|
104
|
+
|
|
105
|
+
const _result = await plugin.afterCompile!(ctx)
|
|
106
|
+
|
|
107
|
+
const capturedCtx = (globalThis as any).__codegen_test_ctx__
|
|
108
|
+
expect(capturedCtx).toBeDefined()
|
|
109
|
+
expect(capturedCtx.realm).toEqual(realm)
|
|
110
|
+
expect(capturedCtx.allFileTags).toBe(allFileTags)
|
|
111
|
+
expect(capturedCtx.docsMeta).toBe(docsMeta)
|
|
112
|
+
expect(capturedCtx.config).toEqual(templateConfig)
|
|
113
|
+
expect(capturedCtx.output).toBe('generated')
|
|
114
|
+
expect(capturedCtx.templateName).toBe('mock-template')
|
|
115
|
+
|
|
116
|
+
// Clean up
|
|
117
|
+
fs.rmSync(tmpDir, { recursive: true })
|
|
118
|
+
delete (globalThis as any).__codegen_test_ctx__
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
it('prefixes file paths with entry.output', async () => {
|
|
122
|
+
const fs = await import('node:fs')
|
|
123
|
+
const os = await import('node:os')
|
|
124
|
+
const pathMod = await import('node:path')
|
|
125
|
+
const tmpDir = fs.mkdtempSync(pathMod.join(os.tmpdir(), 'codegen-test-'))
|
|
126
|
+
const templatePath = pathMod.join(tmpDir, 'path-template.mjs')
|
|
127
|
+
fs.writeFileSync(
|
|
128
|
+
templatePath,
|
|
129
|
+
`
|
|
130
|
+
export function generate(ctx) {
|
|
131
|
+
return { files: [
|
|
132
|
+
{ path: 'types.ts', content: '// types' },
|
|
133
|
+
{ path: 'queries.ts', content: '// queries' },
|
|
134
|
+
] }
|
|
135
|
+
}
|
|
136
|
+
`,
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
const ctx = makeCtx({
|
|
140
|
+
config: {
|
|
141
|
+
templates: [
|
|
142
|
+
{
|
|
143
|
+
template: templatePath,
|
|
144
|
+
output: 'src/generated',
|
|
145
|
+
config: {},
|
|
146
|
+
},
|
|
147
|
+
],
|
|
148
|
+
},
|
|
149
|
+
} as any)
|
|
150
|
+
|
|
151
|
+
const result = await plugin.afterCompile!(ctx)
|
|
152
|
+
expect(result.files).toHaveLength(2)
|
|
153
|
+
expect(result.files[0].filePath).toBe('src/generated/types.ts')
|
|
154
|
+
expect(result.files[1].filePath).toBe('src/generated/queries.ts')
|
|
155
|
+
|
|
156
|
+
fs.rmSync(tmpDir, { recursive: true })
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
it('throws when template does not export a generate function', async () => {
|
|
160
|
+
const fs = await import('node:fs')
|
|
161
|
+
const os = await import('node:os')
|
|
162
|
+
const pathMod = await import('node:path')
|
|
163
|
+
const tmpDir = fs.mkdtempSync(pathMod.join(os.tmpdir(), 'codegen-test-'))
|
|
164
|
+
const templatePath = pathMod.join(tmpDir, 'bad-template.mjs')
|
|
165
|
+
fs.writeFileSync(templatePath, `export const name = 'bad'`)
|
|
166
|
+
|
|
167
|
+
const ctx = makeCtx({
|
|
168
|
+
config: {
|
|
169
|
+
templates: [
|
|
170
|
+
{
|
|
171
|
+
template: templatePath,
|
|
172
|
+
output: 'out',
|
|
173
|
+
},
|
|
174
|
+
],
|
|
175
|
+
},
|
|
176
|
+
} as any)
|
|
177
|
+
|
|
178
|
+
await expect(plugin.afterCompile!(ctx)).rejects.toThrow('does not export a generate function')
|
|
179
|
+
|
|
180
|
+
fs.rmSync(tmpDir, { recursive: true })
|
|
181
|
+
})
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
describe('extractTemplateName', () => {
|
|
185
|
+
it('extracts template name from scoped package path', async () => {
|
|
186
|
+
// We test indirectly via afterCompile setting templateName on context
|
|
187
|
+
const fs = await import('node:fs')
|
|
188
|
+
const os = await import('node:os')
|
|
189
|
+
const pathMod = await import('node:path')
|
|
190
|
+
const tmpDir = fs.mkdtempSync(pathMod.join(os.tmpdir(), 'codegen-test-'))
|
|
191
|
+
|
|
192
|
+
// Simulate a path like @sqldoc/templates/typescript by creating nested dirs
|
|
193
|
+
const templatePath = pathMod.join(tmpDir, 'typescript.mjs')
|
|
194
|
+
fs.writeFileSync(
|
|
195
|
+
templatePath,
|
|
196
|
+
`
|
|
197
|
+
export function generate(ctx) {
|
|
198
|
+
globalThis.__codegen_name_test__ = ctx.templateName
|
|
199
|
+
return { files: [] }
|
|
200
|
+
}
|
|
201
|
+
`,
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
const ctx = makeCtx({
|
|
205
|
+
config: {
|
|
206
|
+
templates: [
|
|
207
|
+
{
|
|
208
|
+
template: templatePath,
|
|
209
|
+
output: 'out',
|
|
210
|
+
},
|
|
211
|
+
],
|
|
212
|
+
},
|
|
213
|
+
} as any)
|
|
214
|
+
|
|
215
|
+
await plugin.afterCompile!(ctx)
|
|
216
|
+
expect((globalThis as any).__codegen_name_test__).toBe('typescript')
|
|
217
|
+
|
|
218
|
+
fs.rmSync(tmpDir, { recursive: true })
|
|
219
|
+
delete (globalThis as any).__codegen_name_test__
|
|
220
|
+
})
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
describe('plugin metadata', () => {
|
|
224
|
+
it('has apiVersion 1', () => {
|
|
225
|
+
expect(plugin.apiVersion).toBe(1)
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
it('has name codegen', () => {
|
|
229
|
+
expect(plugin.name).toBe('codegen')
|
|
230
|
+
})
|
|
231
|
+
})
|
|
232
|
+
})
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import * as path from 'node:path'
|
|
2
|
+
import type { AtlasRealm } from '@sqldoc/atlas'
|
|
3
|
+
import type { NamespacePlugin, ProjectContext, ProjectOutput } from '@sqldoc/core'
|
|
4
|
+
import { findSqldocDir, tsImport, unwrapDefault } from '@sqldoc/core'
|
|
5
|
+
import type { CodegenConfig, TemplateContext } from './types.ts'
|
|
6
|
+
|
|
7
|
+
/** Extract template name from import path: '@sqldoc/templates/typescript' -> 'typescript', './my.ts' -> 'my' */
|
|
8
|
+
function extractTemplateName(importPath: string): string {
|
|
9
|
+
const parts = importPath.split('/')
|
|
10
|
+
const last = parts[parts.length - 1]
|
|
11
|
+
return last.replace(/\.[^.]+$/, '')
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const plugin: NamespacePlugin = {
|
|
15
|
+
apiVersion: 1,
|
|
16
|
+
name: 'codegen',
|
|
17
|
+
tags: {
|
|
18
|
+
rename: {
|
|
19
|
+
description: 'Rename table/column in generated code (optional second arg scopes to a specific template)',
|
|
20
|
+
targets: ['table', 'column', 'view'],
|
|
21
|
+
args: [{ type: 'string' }, { type: 'string' }],
|
|
22
|
+
},
|
|
23
|
+
skip: {
|
|
24
|
+
description: 'Exclude from generated code (optional arg scopes to a specific template)',
|
|
25
|
+
targets: ['table', 'column', 'view'],
|
|
26
|
+
args: [{ type: 'string' }],
|
|
27
|
+
},
|
|
28
|
+
type: {
|
|
29
|
+
description: 'Override generated type for a column (optional second arg scopes to a specific template)',
|
|
30
|
+
targets: ['column'],
|
|
31
|
+
args: [{ type: 'string' }, { type: 'string' }],
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
async afterCompile(ctx: ProjectContext): Promise<ProjectOutput> {
|
|
36
|
+
const config = ctx.config as CodegenConfig
|
|
37
|
+
if (!config.templates || config.templates.length === 0) {
|
|
38
|
+
return { files: [] }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const realm = ctx.atlasRealm as AtlasRealm | undefined
|
|
42
|
+
if (!realm) {
|
|
43
|
+
throw new Error('ns-codegen requires Atlas schema. Run with a database connection (devUrl in config).')
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const allFiles: Array<{ filePath: string; content: string; source: string }> = []
|
|
47
|
+
|
|
48
|
+
for (const entry of config.templates) {
|
|
49
|
+
let template: any
|
|
50
|
+
|
|
51
|
+
if (typeof entry.template === 'function' && typeof entry.template.generate === 'function') {
|
|
52
|
+
// Template object passed directly (typed config path)
|
|
53
|
+
template = entry.template
|
|
54
|
+
} else if (typeof entry.template === 'string') {
|
|
55
|
+
// Import path — resolve and load
|
|
56
|
+
const isAbsolute = path.isAbsolute(entry.template) || entry.template.startsWith('.')
|
|
57
|
+
let mod: any
|
|
58
|
+
if (isAbsolute) {
|
|
59
|
+
mod = await import(entry.template)
|
|
60
|
+
} else {
|
|
61
|
+
const sqldocDir = findSqldocDir()
|
|
62
|
+
let resolveDir = sqldocDir ? path.join(sqldocDir, 'node_modules') : null
|
|
63
|
+
if (!resolveDir && process.env.SQLDOC_RESOLVE_FROM_LOCAL_PACKAGE === 'true') {
|
|
64
|
+
resolveDir = ctx.projectRoot
|
|
65
|
+
}
|
|
66
|
+
if (!resolveDir) {
|
|
67
|
+
throw new Error(
|
|
68
|
+
`Cannot resolve template '${entry.template}': no .sqldoc/node_modules/ found. Run 'sqldoc init' first.`,
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
mod = (await tsImport(entry.template, resolveDir)) as any
|
|
72
|
+
}
|
|
73
|
+
template = unwrapDefault(mod, (m: any) => typeof m.generate === 'function')
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (!template || typeof template.generate !== 'function') {
|
|
77
|
+
throw new Error(`Template '${entry.template}' does not export a generate function`)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const templateName =
|
|
81
|
+
typeof entry.template === 'string' ? extractTemplateName(entry.template) : (template.name ?? 'unknown')
|
|
82
|
+
const templateCtx: TemplateContext = {
|
|
83
|
+
realm,
|
|
84
|
+
allFileTags: ctx.allFileTags,
|
|
85
|
+
docsMeta: ctx.docsMeta,
|
|
86
|
+
config: entry.config ?? {},
|
|
87
|
+
output: entry.output,
|
|
88
|
+
templateName,
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const result = template.generate(templateCtx)
|
|
92
|
+
const outputIsFile = path.extname(entry.output) !== ''
|
|
93
|
+
|
|
94
|
+
if (outputIsFile && result.files.length > 1) {
|
|
95
|
+
throw new Error(
|
|
96
|
+
`Template '${templateName}' generates ${result.files.length} files but output '${entry.output}' is a file path. Use a directory instead.`,
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
for (const file of result.files) {
|
|
101
|
+
const filePath = outputIsFile ? entry.output : path.join(entry.output, file.path)
|
|
102
|
+
allFiles.push({ filePath, content: file.content, source: templateName })
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return { files: allFiles }
|
|
107
|
+
},
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export default plugin
|
|
111
|
+
export type { CodegenConfig, Template, TemplateContext, TemplateDef, TemplateEntry, TemplateResult } from './types.ts'
|
|
112
|
+
export { defineTemplate } from './types.ts'
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { AtlasRealm } from '@sqldoc/atlas'
|
|
2
|
+
import type { ArgType, DocsMeta, InferSchema } from '@sqldoc/core'
|
|
3
|
+
|
|
4
|
+
/** Config for the codegen namespace in sqldoc.config.ts namespaces.codegen */
|
|
5
|
+
export interface CodegenConfig {
|
|
6
|
+
templates?: TemplateEntry[]
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** A single template entry in the codegen config */
|
|
10
|
+
export interface TemplateEntry {
|
|
11
|
+
/** The template — either an import path string or a Template object */
|
|
12
|
+
template: string | Template<any>
|
|
13
|
+
/** Output path — file path for single-file templates, directory for multi-file templates */
|
|
14
|
+
output: string
|
|
15
|
+
/** Template-specific config (typed by each template) */
|
|
16
|
+
config?: Record<string, unknown>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Context passed to each template's generate function */
|
|
20
|
+
export interface TemplateContext<C = Record<string, unknown>> {
|
|
21
|
+
/** Full post-compile Atlas realm */
|
|
22
|
+
realm: AtlasRealm
|
|
23
|
+
/** All tags across all files, grouped by source file then by SQL object */
|
|
24
|
+
allFileTags: Array<{
|
|
25
|
+
sourceFile: string
|
|
26
|
+
objects: Array<{
|
|
27
|
+
objectName: string
|
|
28
|
+
target: string
|
|
29
|
+
tags: Array<{ namespace: string; tag: string | null; args: Record<string, unknown> | unknown[] }>
|
|
30
|
+
}>
|
|
31
|
+
}>
|
|
32
|
+
/** Aggregated docs metadata from all plugins */
|
|
33
|
+
docsMeta: DocsMeta[]
|
|
34
|
+
/** Template-specific config from sqldoc.config.ts */
|
|
35
|
+
config: C
|
|
36
|
+
/** Output directory path */
|
|
37
|
+
output: string
|
|
38
|
+
/** Template name (derived from import path) */
|
|
39
|
+
templateName: string
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Result returned from a template's generate function */
|
|
43
|
+
export interface TemplateResult {
|
|
44
|
+
files: Array<{ path: string; content: string }>
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Template definition (the object shape) */
|
|
48
|
+
export interface TemplateDef<S extends Record<string, ArgType> = Record<string, ArgType>> {
|
|
49
|
+
/** Human-readable template name */
|
|
50
|
+
name: string
|
|
51
|
+
/** What this template generates */
|
|
52
|
+
description: string
|
|
53
|
+
/** Target language/format */
|
|
54
|
+
language: string
|
|
55
|
+
/** Config schema using ArgType definitions — use `as const` for type inference */
|
|
56
|
+
configSchema?: S
|
|
57
|
+
/** Generate output files from the schema + tags */
|
|
58
|
+
generate: (ctx: TemplateContext<InferSchema<S>>) => TemplateResult
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** A template is callable (creates a typed TemplateEntry) and has metadata properties */
|
|
62
|
+
export interface Template<S extends Record<string, ArgType> = Record<string, ArgType>> extends TemplateDef<S> {
|
|
63
|
+
(opts: { output: string } & InferSchema<S>): TemplateEntry
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Helper to define a template with full type inference.
|
|
68
|
+
* Returns a callable: `typescript({ output: '...', dateType: 'temporal' })`
|
|
69
|
+
*/
|
|
70
|
+
export function defineTemplate<const S extends Record<string, ArgType>>(def: TemplateDef<S>): Template<S> {
|
|
71
|
+
const fn = function (this: void, opts: { output: string } & InferSchema<S>): TemplateEntry {
|
|
72
|
+
const { output, ...config } = opts
|
|
73
|
+
return {
|
|
74
|
+
template: fn as any,
|
|
75
|
+
output,
|
|
76
|
+
config: Object.keys(config).length > 0 ? (config as Record<string, unknown>) : undefined,
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
// 'name' is read-only on functions — use defineProperty
|
|
80
|
+
Object.defineProperty(fn, 'name', { value: def.name, writable: false })
|
|
81
|
+
Object.assign(fn, {
|
|
82
|
+
description: def.description,
|
|
83
|
+
language: def.language,
|
|
84
|
+
configSchema: def.configSchema,
|
|
85
|
+
generate: def.generate,
|
|
86
|
+
})
|
|
87
|
+
return fn as unknown as Template<S>
|
|
88
|
+
}
|