Skip to content

Commit 4a569b8

Browse files
committed
refactor: refactor custom codecs function to return a either monad
DX-658
1 parent 443b16f commit 4a569b8

File tree

1 file changed

+46
-29
lines changed

1 file changed

+46
-29
lines changed

packages/openapi-generator/src/project.ts

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import resolve from 'resolve';
66

77
import { KNOWN_IMPORTS, type KnownCodec } from './knownImports';
88
import { parseSource, type SourceFile } from './sourceFile';
9-
import { errorLeft, logError, logInfo } from './error';
9+
import { errorLeft, logInfo } from './error';
1010

1111
const readFile = promisify(fs.readFile);
1212

@@ -63,7 +63,19 @@ export class Project {
6363

6464
if (!visitedPackages.has(sym.from)) {
6565
// This is a step that checks if this import has custom codecs, and loads them into known imports
66-
await this.populateCustomCodecs(baseDir, sym.from);
66+
const codecs = await this.getCustomCodecs(baseDir, sym.from);
67+
if (E.isLeft(codecs)) {
68+
return codecs;
69+
}
70+
71+
if (Object.keys(codecs.right).length > 0) {
72+
this.knownImports[sym.from] = {
73+
...codecs.right,
74+
...this.knownImports[sym.from],
75+
};
76+
77+
logInfo(`Loaded custom codecs for ${sym.from}`);
78+
}
6779
}
6880

6981
visitedPackages.add(sym.from);
@@ -158,39 +170,44 @@ export class Project {
158170
return this.types;
159171
}
160172

161-
private async populateCustomCodecs(basedir: string, packageName: string) {
173+
private async getCustomCodecs(
174+
basedir: string,
175+
packageName: string,
176+
): Promise<E.Either<string, Record<string, KnownCodec>>> {
177+
let packageJsonPath = '';
178+
162179
try {
163-
const packageJson = resolve.sync(`${packageName}/package.json`, {
180+
packageJsonPath = resolve.sync(`${packageName}/package.json`, {
164181
basedir,
165182
extensions: ['.json'],
166183
});
167-
const packageInfo = JSON.parse(fs.readFileSync(packageJson, 'utf8'));
168-
169-
if (packageInfo['customCodecFile']) {
170-
// The package defines their own custom codecs
171-
const customCodecPath = resolve.sync(
172-
`${packageName}/${packageInfo['customCodecFile']}`,
173-
{
174-
basedir,
175-
extensions: ['.ts', '.js'],
176-
},
177-
);
178-
const module = await import(customCodecPath);
179-
if (module.default === undefined) {
180-
logError(`Could not find default export in ${customCodecPath}`);
181-
return;
182-
}
183-
184-
const customCodecs = module.default(E);
185-
this.knownImports[packageName] = {
186-
...customCodecs,
187-
...this.knownImports[packageName],
188-
};
184+
} catch (e) {
185+
// This should not lead to the failure of the entire project, so return an empty record
186+
return E.right({});
187+
}
189188

190-
logInfo(`Loaded custom codecs for ${packageName}`);
189+
const packageInfo = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
190+
191+
if (packageInfo['customCodecFile']) {
192+
// The package defines their own custom codecs
193+
const customCodecPath = resolve.sync(
194+
`${packageName}/${packageInfo['customCodecFile']}`,
195+
{
196+
basedir,
197+
extensions: ['.ts', '.js'],
198+
},
199+
);
200+
201+
const module = await import(customCodecPath);
202+
if (module.default === undefined) {
203+
// Package does not have a default export so we can't use it. Format of the custom codec file is incorrect
204+
return errorLeft(`Could not find default export in ${customCodecPath}`);
191205
}
192-
} catch (e) {
193-
return;
206+
207+
const customCodecs = module.default(E);
208+
return E.right(customCodecs);
194209
}
210+
211+
return E.right({});
195212
}
196213
}

0 commit comments

Comments
 (0)