Skip to content

Commit

Permalink
create relative import path generation
Browse files Browse the repository at this point in the history
  • Loading branch information
inkognitro committed Mar 24, 2024
1 parent 18f5a35 commit 971d416
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/oas3/codegen/ts/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,38 @@ export class DefaultCodeGenerator implements CodeGenerator {

private createRelativeImportPath(
localOutputPath: OutputPath,
referencingOutputPath: OutputPath
refOutputPath: OutputPath
): string {
return this.createFilePathFromOutputPath(localOutputPath); // todo: reference relatively
const targetFilePath = this.createFilePathFromOutputPath(localOutputPath);
const refFilePath = this.createFilePathFromOutputPath(refOutputPath);
if (targetFilePath === refFilePath) {
throw new Error(
'same filePath cases should have been handled before calling this function'
);
}
const targetFileName = targetFilePath.split('/').slice(-1).join('');
const targetFolderPathParts = targetFilePath.split('/').slice(0, -1);
const targetFolderPath = targetFolderPathParts.join('/');
const refFolderPathParts = refFilePath.split('/').slice(0, -1);
const refFolderPath = refFolderPathParts.join('/');
if (targetFolderPath === refFolderPath) {
const targetFileNameParts = targetFileName.split('.ts');
return `./${targetFileNameParts[0]}`;
}
let relativePath = '../';
let commonIndex = refFolderPathParts.length - 1;
for (let index = refFolderPathParts.length - 1; index >= 0; index--) {
const refPathPrefixParts = refFolderPathParts.slice(0, index);
const targetPathPrefixParts = targetFolderPathParts.slice(0, index);
if (!areOutputPathsEqual(targetPathPrefixParts, refPathPrefixParts)) {
relativePath += '../';
continue;
}
commonIndex = index;
break;
}
const relativePathPartsToTarget = targetFolderPathParts.slice(commonIndex);
return `${relativePath}${relativePathPartsToTarget.join('/')}`;
}

private isSameOutputFile(outputPath1: OutputPath, outputPath2: OutputPath) {
Expand Down

0 comments on commit 971d416

Please sign in to comment.