-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for creating barrel files
- Loading branch information
Showing
16 changed files
with
696 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import type { ExportDeclarationStructure, OptionalKind } from "ts-morph"; | ||
|
||
import { BaseHandler } from "../BaseHandler"; | ||
import { compareObjectValues } from "../BaseHandler/compareFunctions"; | ||
|
||
// note: this class will only handle generic barrel files. | ||
// barrel files for model specific files will be handled by the corresponding type handler | ||
export class BarrelFileHandler extends BaseHandler { | ||
async createBarrelFiles(): Promise<void> { | ||
await Promise.all([ | ||
this._createSharedBarrelFile(), | ||
this._createModelBarrelFiles(), | ||
this._createTopLevelBarrelFile(), | ||
]); | ||
} | ||
|
||
private async _createModelBarrelFiles(): Promise<void> { | ||
const sourceFilesPromise: Promise<void>[] = []; | ||
|
||
// eslint-disable-next-line no-restricted-syntax | ||
for (const model of this.baseParser.modelsList) { | ||
const sourceFile = this.baseFileGenerator.createSourceFile(`${model}/index`); | ||
|
||
sourceFile.addExportDeclarations([ | ||
{ | ||
moduleSpecifier: `./${this.config.paths.inputTypes}`, | ||
}, | ||
{ | ||
moduleSpecifier: `./${this.config.paths.model}`, | ||
}, | ||
{ | ||
moduleSpecifier: `./${this.config.paths.outputTypes}`, | ||
}, | ||
{ | ||
moduleSpecifier: `./${this.config.paths.resolvers}`, | ||
}, | ||
]); | ||
|
||
sourceFilesPromise.push(sourceFile.save()); | ||
} | ||
|
||
await Promise.all(sourceFilesPromise); | ||
} | ||
|
||
private async _createSharedBarrelFile(): Promise<void> { | ||
const sourceFile = this.baseFileGenerator.createSourceFile("shared/index"); | ||
|
||
sourceFile.addExportDeclarations([ | ||
{ | ||
moduleSpecifier: `./${this.config.paths.inputTypes}`, | ||
}, | ||
{ | ||
moduleSpecifier: `./${this.config.paths.outputTypes}`, | ||
}, | ||
]); | ||
|
||
await sourceFile.save(); | ||
} | ||
|
||
private async _createTopLevelBarrelFile(): Promise<void> { | ||
const sourceFile = this.baseFileGenerator.createSourceFile("index"); | ||
const exportDeclarations: OptionalKind<ExportDeclarationStructure>[] = []; | ||
|
||
exportDeclarations.push({ | ||
moduleSpecifier: `./shared`, | ||
}); | ||
|
||
// eslint-disable-next-line no-restricted-syntax | ||
for (const model of this.baseParser.modelsList) { | ||
exportDeclarations.push({ moduleSpecifier: `./${model}` }); | ||
} | ||
|
||
sourceFile.addExportDeclarations( | ||
exportDeclarations.sort((a, b) => compareObjectValues({ a, b, field: "moduleSpecifier" })) | ||
); | ||
|
||
await sourceFile.save(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.