Skip to content

Commit

Permalink
improve upon index exports now that types are also re-exported
Browse files Browse the repository at this point in the history
  • Loading branch information
nedsalk committed Dec 26, 2024
1 parent 143f356 commit aa6b0af
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 22 deletions.
22 changes: 15 additions & 7 deletions packages/abi/src/gen/renderers/ts/renderers/render-index-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import indexTemplate from '../templates/index.hbs';
import { getParentDirWrapper } from './get-parent-dir-wrapper';
import { templateRenderer } from './template-renderer';

export type IndexContents = Map<
Abi['programType'],
{ filename: string; exportedContent: string[] }[]
>;
/**
* @returns an array of index files
* that includes the root index.ts and the index.ts for each provided program type.
*/
export function renderIndexFiles(
indexContents: Map<Abi['programType'], string[]>,
indexContents: IndexContents,
versions: BinaryVersions
): AbiGenResult[] {
const results: AbiGenResult[] = [];
Expand All @@ -23,18 +27,21 @@ export function renderIndexFiles(
// from index.ts to e.g. contracts/index.ts
const indexFilename = withParentDir('index.ts');

const pathsToFiles = files.map((file) => {
const exports = files.map(({ filename, exportedContent }) => {
// from e.g. contracts/AbiContract.ts to AbiContract.ts
const relativePathToFile = removeParentDir(file);
const relativePathToFile = removeParentDir(filename);
// remove .ts extension
return relativePathToFile.split('.')[0];
return {
path: relativePathToFile.split('.')[0],
exportedContent: `{ ${exportedContent.join(', ')} }`,
};
});

const content = templateRenderer({
versions,
template: indexTemplate,
data: {
paths: pathsToFiles,
exports,
},
});

Expand All @@ -46,15 +53,16 @@ export function renderIndexFiles(

const mainIndexFileImportPaths = [...indexContents.keys()]
.sort()
.map((programType) => getParentDirWrapper(programType).parentDir);
.map((programType) => getParentDirWrapper(programType).parentDir)
.map((path) => ({ path, exportedContent: '*' }));

const mainIndexFile: AbiGenResult = {
filename: 'index.ts',
content: templateRenderer({
versions,
template: indexTemplate,
data: {
paths: mainIndexFileImportPaths,
exports: mainIndexFileImportPaths,
},
}),
};
Expand Down
8 changes: 4 additions & 4 deletions packages/abi/src/gen/renderers/ts/renderers/render-program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function renderProgram(details: ProgramDetails, versions: BinaryVersions)
versions,
data: { name },
}),
exportInIndexFile: true,
exportInIndexFile: [name],
},

{
Expand All @@ -73,7 +73,7 @@ export function renderProgram(details: ProgramDetails, versions: BinaryVersions)
versions,
data: { name },
}),
exportInIndexFile: true,
exportInIndexFile: [`${name}Factory`],
});
}
break;
Expand All @@ -85,7 +85,7 @@ export function renderProgram(details: ProgramDetails, versions: BinaryVersions)
versions,
data: { name },
}),
exportInIndexFile: true,
exportInIndexFile: [name],
});
break;
case 'script':
Expand All @@ -96,7 +96,7 @@ export function renderProgram(details: ProgramDetails, versions: BinaryVersions)
versions,
data: { name },
}),
exportInIndexFile: true,
exportInIndexFile: [name],
});
break;
case 'library':
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { BinaryVersions } from '@fuel-ts/versions';

import type { Abi } from '../../../..';
import type { AbiGenResult, ProgramDetails } from '../../../abi-gen-types';

import type { IndexContents } from './render-index-files';
import { renderIndexFiles } from './render-index-files';
import { renderProgram } from './render-program';

Expand All @@ -13,20 +13,20 @@ import { renderProgram } from './render-program';
*/
export function renderPrograms(details: ProgramDetails[], versions: BinaryVersions) {
const results: AbiGenResult[] = [];
const indexContents = new Map<Abi['programType'], string[]>();
const indexContents: IndexContents = new Map();

for (const d of details) {
const files = renderProgram(d, versions);

results.push(...files);

files.forEach((file) => {
if (!file.exportInIndexFile) {
if (!file.exportInIndexFile?.length) {
return;
}

const contents = indexContents.get(d.abi.programType) ?? [];
contents.push(file.filename);
contents.push({ filename: file.filename, exportedContent: file.exportInIndexFile });
indexContents.set(d.abi.programType, contents);
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/abi/src/gen/renderers/ts/templates/index.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{header}}

{{#each paths}}
export * from './{{this}}';
{{#each exports}}
export {{exportedContent}} from './{{path}}';
{{/each}}
2 changes: 1 addition & 1 deletion packages/abi/src/gen/renderers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import type { AbiGenResult, ProgramDetails } from '../abi-gen-types';
export type Renderer = (details: ProgramDetails[], versions: BinaryVersions) => AbiGenResult[];

export type TsAbiGenResult = AbiGenResult & {
exportInIndexFile?: boolean;
exportInIndexFile?: string[];
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
*/


export * from './AbiContract';
export * from './AbiContractFactory';
export { AbiContract } from './AbiContract';
export { AbiContractFactory } from './AbiContractFactory';
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
*/


export * from './AbiPredicate';
export { AbiPredicate } from './AbiPredicate';
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
*/


export * from './AbiScript';
export { AbiScript } from './AbiScript';

0 comments on commit aa6b0af

Please sign in to comment.