Skip to content

Commit 8103e62

Browse files
committed
Add support to inject raw content
1 parent c978b40 commit 8103e62

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

src/generator-options.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,16 @@ export const CommonOptionsSchema = z.object({
271271

272272
export interface CommonOptions extends z.TypeOf<typeof CommonOptionsSchema> {}
273273

274+
export const RawContentSchema = z.object({
275+
/** Raw content injected before generated code in each file */
276+
before: z.string().nullish(),
277+
278+
/** Raw content injected after generated code in each file */
279+
after: z.string().nullish()
280+
});
281+
282+
export interface RawContent extends z.TypeOf<typeof RawContentSchema> {}
283+
274284
export const GeneratorOptsSchema = z.object({
275285
/** Simulate the generation and print the outcome without actually modifying any files */
276286
dryRun: z.boolean().nullish(),
@@ -356,7 +366,15 @@ export const GeneratorOptsSchema = z.object({
356366
'never',
357367
'interactively',
358368
'all'
359-
]).nullish()
369+
]).nullish(),
370+
371+
/**
372+
* Support injection of raw content in the generated files.
373+
* This is useful for adding things like eslint-ignore, additional exports etc.
374+
*
375+
* @see RawContent
376+
*/
377+
rawContent: RawContentSchema.nullish(),
360378
});
361379

362380
/**

src/generator.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,14 @@ export class Generator {
406406
}
407407

408408
protected async postProcessOutput(output: string, _table: Table) {
409-
return output;
409+
const sections = [output];
410+
if (this.opts.rawContent?.before) {
411+
sections.unshift(this.opts.rawContent.before)
412+
}
413+
if (this.opts.rawContent?.after) {
414+
sections.push(this.opts.rawContent.after)
415+
}
416+
return sections.join('\n');
410417
}
411418

412419
protected getClassNameFromTableName(tableName: string, tableKind: TableKind) {
@@ -586,4 +593,4 @@ export class Generator {
586593
}
587594

588595

589-
type TableKind = "Table" | "View"
596+
type TableKind = "Table" | "View"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// AuthorsTable.ts :
2+
/* eslint-disable */
3+
/**
4+
* DO NOT EDIT:
5+
*
6+
* This file has been auto-generated from database schema using ts-sql-codegen.
7+
* Any changes will be overwritten.
8+
*/
9+
import { Table } from "ts-sql-query/Table";
10+
import type { DBConnection } from "../helpers/connection-source";
11+
12+
export class AuthorsTable extends Table<DBConnection, 'AuthorsTable'> {
13+
id = this.primaryKey('id', 'int');
14+
name = this.optionalColumn('name', 'string');
15+
dob = this.optionalColumn('dob', 'localDate');
16+
createdAt = this.columnWithDefaultValue('created_at', 'localDateTime');
17+
updatedAt = this.columnWithDefaultValue('updated_at', 'localDateTime');
18+
19+
constructor() {
20+
super('authors')
21+
}
22+
}
23+
24+
25+
26+
/* Generated on: 2020-10-10 */

test/test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,23 @@ describe("Generator", function () {
525525
await generator.generate();
526526
await snap(await readAllGenerated());
527527
})
528+
529+
it("supports injection of raw content in generated files", async () => {
530+
const generator = new Generator({
531+
schemaPath,
532+
connectionSourcePath,
533+
outputDirPath,
534+
tables: {
535+
include: ["authors"],
536+
},
537+
rawContent: {
538+
before: `/* eslint-disable */`,
539+
after: `/* Generated on: 2020-10-10 */`
540+
}
541+
});
542+
await generator.generate();
543+
await snap(await readAllGenerated());
544+
})
528545
});
529546

530547
const readAllGenerated = async () => {

0 commit comments

Comments
 (0)