@sqldoc/ns-codegen 0.0.1 → 0.0.2
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 +6 -5
- package/src/__tests__/plugin.test.ts +0 -232
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@sqldoc/ns-codegen",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.2",
|
|
5
5
|
"description": "Code generation namespace for sqldoc -- runs configurable templates against compiled Atlas schema",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": {
|
|
@@ -14,17 +14,18 @@
|
|
|
14
14
|
"types": "./src/index.ts",
|
|
15
15
|
"files": [
|
|
16
16
|
"src",
|
|
17
|
+
"!src/__tests__",
|
|
17
18
|
"package.json"
|
|
18
19
|
],
|
|
19
20
|
"peerDependencies": {
|
|
20
|
-
"@sqldoc/core": "0.0.
|
|
21
|
-
"@sqldoc/atlas": "0.0.
|
|
21
|
+
"@sqldoc/core": "0.0.2",
|
|
22
|
+
"@sqldoc/atlas": "0.0.2"
|
|
22
23
|
},
|
|
23
24
|
"devDependencies": {
|
|
24
25
|
"typescript": "^5.9.3",
|
|
25
26
|
"vitest": "^4.1.0",
|
|
26
|
-
"@sqldoc/
|
|
27
|
-
"@sqldoc/
|
|
27
|
+
"@sqldoc/atlas": "0.0.2",
|
|
28
|
+
"@sqldoc/core": "0.0.2"
|
|
28
29
|
},
|
|
29
30
|
"scripts": {
|
|
30
31
|
"test": "vitest run"
|
|
@@ -1,232 +0,0 @@
|
|
|
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
|
-
})
|