Skip to content

Commit

Permalink
Merge pull request #344 from cmaster11/custom-path
Browse files Browse the repository at this point in the history
Support new settings `interfaceFileSuffix` and `omitIndexFiles`
  • Loading branch information
mrjono1 authored Jul 31, 2023
2 parents 44c4028 + c0c4e82 commit aff21be
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ export interface Settings {
* This ensures that an interface and Schema with the file name are not confused
*/
schemaFileSuffix: string;
/**
* If provided, appends this suffix to the generated interface filenames
* @default ""
*/
interfaceFileSuffix: string;
/**
* If `true` the console will include more information
* @default false
Expand Down Expand Up @@ -243,6 +248,11 @@ export interface Settings {
* @default *.ts files
*/
inputFileFilter: RegExp;
/**
* If true, skips the creation of index.ts files in the generated interface directories
* @default false
*/
omitIndexFiles: boolean
}
```

Expand Down
31 changes: 31 additions & 0 deletions src/__tests__/interfaceFileSuffix/interfaceFileSuffix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {existsSync, rmdirSync} from 'fs';

import {convertFromDirectory} from '../../index';

const typeOutputDirectory = './src/__tests__/interfaceFileSuffix/interfaces';

describe('Create interfaces from schema files with a suffix in the interface filename', () => {
beforeAll(() => {
if (existsSync(typeOutputDirectory)) {
rmdirSync(typeOutputDirectory, {recursive: true});
}
});

test('generates interfaces but no index files', async () => {
const result = await convertFromDirectory({
schemaDirectory: './src/__tests__/interfaceFileSuffix/schemas',
interfaceFileSuffix: '.generated',
typeOutputDirectory
});

expect(result)
.toBe(true);

expect(existsSync(`${typeOutputDirectory}/One.generated.ts`))
.toBe(true)

// Index file should be untouched
expect(existsSync(`${typeOutputDirectory}/index.ts`))
.toBe(true)
});
});
14 changes: 14 additions & 0 deletions src/__tests__/interfaceFileSuffix/schemas/OneSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Joi from 'joi';

export const TestSchema = Joi.object({
name: Joi.string().optional(),
propertyName1: Joi.boolean().required(),
'yellow.flower': Joi.string()
})
.meta({ className: 'TestSchema' })
.description('a test schema definition');

export const purple = (): void => {
// eslint-disable-next-line no-console
console.log('this is not a schema');
};
27 changes: 27 additions & 0 deletions src/__tests__/noIndex/noIndex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {existsSync, rmdirSync} from 'fs';

import {convertFromDirectory} from '../../index';

const typeOutputDirectory = './src/__tests__/noIndex/interfaces';

describe('Create interfaces from schema files but not index files', () => {
beforeAll(() => {
if (existsSync(typeOutputDirectory)) {
rmdirSync(typeOutputDirectory, {recursive: true});
}
});

test('generates interfaces but no index files', async () => {
const result = await convertFromDirectory({
schemaDirectory: './src/__tests__/noIndex/schemas',
omitIndexFiles: true,
typeOutputDirectory
});

expect(result)
.toBe(true);

expect(existsSync(`${typeOutputDirectory}/index.ts`))
.toBe(false)
});
});
10 changes: 10 additions & 0 deletions src/__tests__/noIndex/schemas/FooBarSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Joi from 'joi';

export const BarSchema = Joi.object({
id: Joi.number().required().description('Id').example(1)
}).meta({ className: 'Bar' });

export const FooSchema = Joi.object({
id: Joi.number().required().description('Id').example(1),
bar: BarSchema.required().description('Bar')
}).meta({ className: 'Foo' });
14 changes: 14 additions & 0 deletions src/__tests__/noIndex/schemas/OneSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Joi from 'joi';

export const TestSchema = Joi.object({
name: Joi.string().optional(),
propertyName1: Joi.boolean().required(),
'yellow.flower': Joi.string()
})
.meta({ className: 'TestSchema' })
.description('a test schema definition');

export const purple = (): void => {
// eslint-disable-next-line no-console
console.log('this is not a schema');
};
4 changes: 4 additions & 0 deletions src/__tests__/noIndex/schemas/notASchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const red = (): void => {
// eslint-disable-next-line no-console
console.log('this is not a schema');
};
2 changes: 1 addition & 1 deletion src/convertFilesInDirectory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export async function convertFilesInDirectory(
}
}

if (!appSettings.indexAllToRoot && !appSettings.flattenTree) {
if (!appSettings.omitIndexFiles && (!appSettings.indexAllToRoot && !appSettings.flattenTree)) {
// Write index.ts
writeIndexFile(appSettings, fileNamesToExport);
}
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function defaultSettings(settings: Partial<Settings>): Settings {
useLabelAsInterfaceName: false,
defaultToRequired: false,
schemaFileSuffix: 'Schema',
interfaceFileSuffix: '',
debug: false,
fileHeader: `/**
* This file was automatically generated by joi-to-typescript
Expand All @@ -33,7 +34,8 @@ function defaultSettings(settings: Partial<Settings>): Settings {
honorCastTo: [],
treatDefaultedOptionalAsRequired: false,
supplyDefaultsInType: false,
inputFileFilter: InputFileFilter.Default
inputFileFilter: InputFileFilter.Default,
omitIndexFiles: false,
},
settings
) as Settings;
Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export interface Settings {
* This ensures that an interface and Schema with the file name are not confused
*/
readonly schemaFileSuffix: string;
/**
* If provided, appends this suffix to the generated interface filenames
* @default ""
*/
readonly interfaceFileSuffix: string;
/**
* If `true` the console will include more information
* @default false
Expand Down Expand Up @@ -86,6 +91,11 @@ export interface Settings {
* @default *.ts files
*/
readonly inputFileFilter: RegExp;
/**
* If true, skips the creation of index.ts files in the generated interface directories
* @default false
*/
readonly omitIndexFiles: boolean
}

export class InputFileFilter {
Expand Down
4 changes: 2 additions & 2 deletions src/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export function writeIndexFile(settings: Settings, fileNamesToExport: string[]):
}

export function getTypeFileNameFromSchema(schemaFileName: string, settings: Settings): string {
return schemaFileName.endsWith(`${settings.schemaFileSuffix}.ts`)
return (schemaFileName.endsWith(`${settings.schemaFileSuffix}.ts`)
? schemaFileName.substring(0, schemaFileName.length - `${settings.schemaFileSuffix}.ts`.length)
: schemaFileName.replace('.ts', '');
: schemaFileName.replace('.ts', '')) + settings.interfaceFileSuffix;
}

/**
Expand Down

0 comments on commit aff21be

Please sign in to comment.