From 27868946ada49d2cb72babc54197d4ccb2442dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Salvador=20L=C3=B3pez=20Mendoza?= Date: Sat, 26 Mar 2022 08:40:02 -0600 Subject: [PATCH] feat: Support reexport with custom output pattern --- src/handlers/re-export.ts | 43 +++++++++++++++++++++++++++++++++++---- src/test/generate.spec.ts | 31 ++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/src/handlers/re-export.ts b/src/handlers/re-export.ts index 9365b037..6bd6e118 100644 --- a/src/handlers/re-export.ts +++ b/src/handlers/re-export.ts @@ -24,17 +24,42 @@ function beforeGenerateFiles(args: EventArguments) { const rootDirectory = project.getDirectoryOrThrow(output); if ([ReExport.Directories, ReExport.All].includes(config.reExport)) { - for (const directory of rootDirectory.getDirectories()) { + for (const directory of rootDirectory.getDescendantDirectories()) { + let indexSourceFile: SourceFile | undefined; + const exportDeclarations: ExportDeclarationStructure[] = directory .getSourceFiles() .filter(sourceFile => { return sourceFile.getBaseName() !== 'index.ts'; }) .map(sourcesFile => getExportDeclaration(directory, sourcesFile)); - directory.createSourceFile( - 'index.ts', + + if (exportDeclarations.length > 0) { + indexSourceFile = directory.createSourceFile( + 'index.ts', + { + statements: exportDeclarations, + }, + { + overwrite: true, + }, + ); + } + + if (indexSourceFile) { + continue; + } + + const namespaceExportDeclarations: ExportDeclarationStructure[] = directory + .getDirectories() + .map(sourceDirectory => + getNamespaceExportDeclaration(directory, sourceDirectory), + ); + + project.createSourceFile( + `${directory.getPath()}/index.ts`, { - statements: exportDeclarations, + statements: namespaceExportDeclarations, }, { overwrite: true, @@ -87,3 +112,13 @@ function getExportDeclaration( moduleSpecifier: directory.getRelativePathAsModuleSpecifierTo(sourceFile), }; } + +function getNamespaceExportDeclaration( + directory: Directory, + sourceDirectory: Directory, +): ExportDeclarationStructure { + return { + kind: StructureKind.ExportDeclaration, + moduleSpecifier: directory.getRelativePathAsModuleSpecifierTo(sourceDirectory), + }; +} diff --git a/src/test/generate.spec.ts b/src/test/generate.spec.ts index 6ecf3fbb..3936f123 100644 --- a/src/test/generate.spec.ts +++ b/src/test/generate.spec.ts @@ -1548,6 +1548,37 @@ describe('reexport option', () => { }); }); + describe('reexport directories with output file pattern', () => { + before(async () => { + ({ project, sourceFiles } = await testGenerate({ + schema: ` + model User { + id Int @id + }`, + options: [ + 'reExport = Directories', + `outputFilePattern = "{model}/{plural.type}/{name}.{type}.ts"`, + ], + })); + }); + + it('user should export plural types', () => { + sourceFile = project.getSourceFile(s => + s.getFilePath().endsWith('/user/index.ts'), + )!; + + for (const exp of [ + `export * from './args';`, + `export * from './enums';`, + `export * from './inputs';`, + `export * from './models';`, + `export * from './outputs';`, + ]) { + expect(sourceFile.getText()).toContain(exp); + } + }); + }); + describe('reexport single', () => { before(async () => { ({ project, sourceFiles } = await testGenerate({