Skip to content

Commit

Permalink
fix: cleanup file path adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Jul 20, 2023
1 parent 23b8d6c commit 01332c5
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 81 deletions.
24 changes: 12 additions & 12 deletions src/data-source/options/module.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import type { DataSourceOptions } from 'typeorm';
import { OptionsError } from '../../errors';
import { adjustFilePaths } from '../../utils';
import { findDataSource } from '../find';
import type { DataSourceOptionsBuildContext } from './type';
import {
adjustFilePathsForDataSourceOptions,
mergeDataSourceOptionsWithEnv,
readDataSourceOptionsFromEnv,
} from './utils';

export async function extendDataSourceOptions(
options: DataSourceOptions,
tsConfigDirectory?: string,
) : Promise<DataSourceOptions> {
await adjustFilePathsForDataSourceOptions(options, { root: tsConfigDirectory });

return options;
}

/**
* Build DataSourceOptions from DataSource or from configuration.
*
Expand All @@ -36,8 +27,13 @@ export async function buildDataSourceOptions(
});

if (dataSource) {
const options = await extendDataSourceOptions(
const options = await adjustFilePaths(
dataSource.options,
[
'entities',
'migrations',
'subscribers',
],
tsconfigDirectory,
);

Expand All @@ -46,7 +42,11 @@ export async function buildDataSourceOptions(

const options = readDataSourceOptionsFromEnv();
if (options) {
return extendDataSourceOptions(options);
return adjustFilePaths(
options,
['entities', 'migrations', 'subscribers'],
tsconfigDirectory,
);
}

throw OptionsError.notFound();
Expand Down
59 changes: 0 additions & 59 deletions src/data-source/options/utils/file-path.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/data-source/options/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './env';
export * from './file-path';
export * from './merge';
7 changes: 3 additions & 4 deletions src/seeder/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import type { ObjectLiteral } from 'rapiq';
import { MssqlParameter, Table } from 'typeorm';
import type { DataSource, DataSourceOptions, QueryRunner } from 'typeorm';
import type { MongoQueryRunner } from 'typeorm/driver/mongodb/MongoQueryRunner';
import { adjustFilePathsForDataSourceOptions, setDataSource } from '../data-source';
import { setDataSource } from '../data-source';
import { useEnv } from '../env';
import { adjustFilePaths } from '../utils';
import { SeederEntity } from './entity';
import { prepareSeederFactories, useSeederFactoryManager } from './factory';
import type { SeederOptions, SeederPrepareElement } from './type';
Expand Down Expand Up @@ -328,9 +329,7 @@ export class SeederExecutor {
options.factories = ['src/database/factories/**/*{.ts,.js}'];
}

await adjustFilePathsForDataSourceOptions(options, {
keys: ['seeds', 'factories'],
});
await adjustFilePaths(options, ['seeds', 'factories']);

return options;
}
Expand Down
36 changes: 36 additions & 0 deletions src/utils/file-path.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { CodeTransformation, isCodeTransformation } from './code-transformation';
import { safeReplaceWindowsSeparator } from './separator';
import { withoutTrailingSlash } from './slash';
import { readTsConfig } from './tsconfig';

const stripLeadingModifier = (text: string) => {
if (text.startsWith('./')) {
Expand Down Expand Up @@ -100,3 +102,37 @@ export function transformFilePath(

return stripLeadingModifier(base);
}

export async function adjustFilePaths<T extends Record<string, any>>(
input: T,
keys?: (keyof T)[],
rootDirectory?: string,
): Promise<T> {
if (isCodeTransformation(CodeTransformation.JUST_IN_TIME)) {
return input;
}

const { compilerOptions } = await readTsConfig(rootDirectory);

keys = keys || Object.keys(input);

for (let i = 0; i < keys.length; i++) {
const key = keys[i];

let value = input[key] as unknown;

if (typeof value === 'string') {
value = transformFilePath(value, compilerOptions?.outDir);
} else if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
if (typeof value[i] === 'string') {
value[i] = transformFilePath(value[i], compilerOptions?.outDir);
}
}
}

input[key] = value as T[keyof T];
}

return input;
}
3 changes: 2 additions & 1 deletion test/unit/data-source/options/env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
buildDataSourceOptions,
hasEnvDataSourceOptions,
mergeDataSourceOptionsWithEnv,
readDataSourceOptionsFromEnv, resetEnv,
readDataSourceOptionsFromEnv,
resetEnv,
} from '../../../../src';
import { User } from '../../../data/entity/user';

Expand Down
9 changes: 5 additions & 4 deletions test/unit/utils/file-path.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
adjustFilePathsForDataSourceOptions, transformFilePath,
adjustFilePaths,
transformFilePath,
} from '../../../src';

describe('src/connection/utils.ts', () => {
Expand Down Expand Up @@ -88,7 +89,7 @@ describe('src/connection/utils.ts', () => {
});

it('should modify connection option(s) for runtime environment', async () => {
const modifiedConnectionOptions = await adjustFilePathsForDataSourceOptions({
const modifiedConnectionOptions = await adjustFilePaths({
factories: ['src/factories.ts'],
seeds: ['src/seeds.ts'],
entities: ['src/entities.ts'],
Expand All @@ -102,12 +103,12 @@ describe('src/connection/utils.ts', () => {
subscribers: ['dist/subscribers.js'],
});

const modifiedConnectionOption = await adjustFilePathsForDataSourceOptions({
const modifiedConnectionOption = await adjustFilePaths({
entities: ['./src/entities.ts'],
});
expect(modifiedConnectionOption).toEqual({ entities: ['dist/entities.js'] });

const modifiedConnectionOptionAlt = await adjustFilePathsForDataSourceOptions({
const modifiedConnectionOptionAlt = await adjustFilePaths({
entities: ['src/entities.ts'],
});
expect(modifiedConnectionOptionAlt).toEqual({ entities: ['dist/entities.js'] });
Expand Down

0 comments on commit 01332c5

Please sign in to comment.