Skip to content

Commit

Permalink
fix: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
AllieJonsson committed Jan 3, 2025
1 parent debead6 commit 8f1e335
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 56 deletions.
15 changes: 15 additions & 0 deletions packages/core/src/writers/generate-imports-for-builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import uniqBy from 'lodash.uniqby';
import { GeneratorImport, NormalizedOutputOptions } from '../types';
import { upath, camel } from '../utils';

export const generateImportsForBuilder = (
output: NormalizedOutputOptions,
imports: GeneratorImport[],
relativeSchemasPath: string,
) =>
output.schemas && !output.indexFiles
? uniqBy(imports, 'name').map((i) => ({
exports: [i],
dependency: upath.joinSafe(relativeSchemasPath, camel(i.name)),
}))
: [{ exports: imports, dependency: relativeSchemasPath }];
17 changes: 3 additions & 14 deletions packages/core/src/writers/single-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
isSyntheticDefaultImportsAllow,
upath,
} from '../utils';
import { generateImportsForBuilder } from './generate-imports-for-builder';
import { generateTarget } from './target';
import { getOrvalGeneratedTypes } from './types';

Expand Down Expand Up @@ -56,7 +57,7 @@ export const writeSingleMode = async ({
);

const importsForBuilder = schemasPath
? generateImports(
? generateImportsForBuilder(
output,
imports.filter(
(imp) => !importsMock.some((impMock) => imp.name === impMock.name),
Expand All @@ -83,7 +84,7 @@ export const writeSingleMode = async ({

if (output.mock) {
const importsMockForBuilder = schemasPath
? generateImports(output, importsMock, schemasPath)
? generateImportsForBuilder(output, importsMock, schemasPath)
: [];
data += builder.importsMock({
implementation: implementationMock,
Expand Down Expand Up @@ -138,15 +139,3 @@ export const writeSingleMode = async ({
throw `Oups... 🍻. An Error occurred while writing file => ${e}`;
}
};

const generateImports = (
output: NormalizedOutputOptions,
imports: GeneratorImport[],
relativeSchemasPath: string,
) =>
output.schemas && !output.indexFiles
? uniqBy(imports, 'name').map((i) => ({
exports: [i],
dependency: upath.joinSafe(relativeSchemasPath, camel(i.name)),
}))
: [{ exports: imports, dependency: relativeSchemasPath }];
17 changes: 3 additions & 14 deletions packages/core/src/writers/split-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { generateTarget } from './target';
import { getOrvalGeneratedTypes } from './types';
import { getMockFileExtensionByTypeName } from '../utils/fileExtensions';
import uniqBy from 'lodash.uniqby';
import { generateImportsForBuilder } from './generate-imports-for-builder';

export const writeSplitMode = async ({
builder,
Expand Down Expand Up @@ -58,7 +59,7 @@ export const writeSplitMode = async ({
output.tsconfig,
);

const importsForBuilder = generateImports(
const importsForBuilder = generateImportsForBuilder(
output,
imports,
relativeSchemasPath,
Expand All @@ -80,7 +81,7 @@ export const writeSplitMode = async ({
output,
});

const importsMockForBuilder = generateImports(
const importsMockForBuilder = generateImportsForBuilder(
output,
importsMock,
relativeSchemasPath,
Expand Down Expand Up @@ -178,15 +179,3 @@ export const writeSplitMode = async ({
throw `Oups... 🍻. An Error occurred while splitting => ${e}`;
}
};

const generateImports = (
output: NormalizedOutputOptions,
imports: GeneratorImport[],
relativeSchemasPath: string,
) =>
output.schemas && !output.indexFiles
? uniqBy(imports, 'name').map((i) => ({
exports: [i],
dependency: upath.joinSafe(relativeSchemasPath, camel(i.name)),
}))
: [{ exports: imports, dependency: relativeSchemasPath }];
17 changes: 3 additions & 14 deletions packages/core/src/writers/split-tags-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { generateTargetForTags } from './target-tags';
import { getOrvalGeneratedTypes } from './types';
import { getMockFileExtensionByTypeName } from '../utils/fileExtensions';
import uniqBy from 'lodash.uniqby';
import { generateImportsForBuilder } from './generate-imports-for-builder';

export const writeSplitTagsMode = async ({
builder,
Expand Down Expand Up @@ -63,7 +64,7 @@ export const writeSplitTagsMode = async ({
)
: '../' + filename + '.schemas';

const importsForBuilder = generateImports(
const importsForBuilder = generateImportsForBuilder(
output,
imports,
relativeSchemasPath,
Expand All @@ -85,7 +86,7 @@ export const writeSplitTagsMode = async ({
output,
});

const importsMockForBuilder = generateImports(
const importsMockForBuilder = generateImportsForBuilder(
output,
importsMock,
relativeSchemasPath,
Expand Down Expand Up @@ -192,15 +193,3 @@ export const writeSplitTagsMode = async ({

return generatedFilePathsArray.flatMap((it) => it);
};

const generateImports = (
output: NormalizedOutputOptions,
imports: GeneratorImport[],
relativeSchemasPath: string,
) =>
output.schemas && !output.indexFiles
? uniqBy(imports, 'name').map((i) => ({
exports: [i],
dependency: upath.joinSafe(relativeSchemasPath, camel(i.name)),
}))
: [{ exports: imports, dependency: relativeSchemasPath }];
17 changes: 3 additions & 14 deletions packages/core/src/writers/tags-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
kebab,
upath,
} from '../utils';
import { generateImportsForBuilder } from './generate-imports-for-builder';
import { generateTargetForTags } from './target-tags';
import { getOrvalGeneratedTypes } from './types';

Expand Down Expand Up @@ -60,7 +61,7 @@ export const writeTagsMode = async ({
)
: './' + filename + '.schemas';

const importsForBuilder = generateImports(
const importsForBuilder = generateImportsForBuilder(
output,
imports.filter(
(imp) => !importsMock.some((impMock) => imp.name === impMock.name),
Expand All @@ -85,7 +86,7 @@ export const writeTagsMode = async ({
});

if (output.mock) {
const importsMockForBuilder = generateImports(
const importsMockForBuilder = generateImportsForBuilder(
output,
importsMock,
schemasPathRelative,
Expand Down Expand Up @@ -163,15 +164,3 @@ export const writeTagsMode = async ({

return generatedFilePathsArray.flatMap((it) => it);
};

const generateImports = (
output: NormalizedOutputOptions,
imports: GeneratorImport[],
relativeSchemasPath: string,
) =>
output.schemas && !output.indexFiles
? uniqBy(imports, 'name').map((i) => ({
exports: [i],
dependency: upath.joinSafe(relativeSchemasPath, camel(i.name)),
}))
: [{ exports: imports, dependency: relativeSchemasPath }];

0 comments on commit 8f1e335

Please sign in to comment.