Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for "duplicate identifier" via generateIndexFile hook for multiple schemas #558

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 87 additions & 4 deletions packages/kanel/src/hooks/generateIndexFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,49 @@ type ExportsItem = {
exportAsType: boolean;
};

type SchemaExports = {
items: Record<string, ExportsItem[]>;
path: string;
};

function stringifyExportItem(item: ExportsItem): string {
const prefix = item.exportAsType ? "type " : "";
return `${prefix}${item.wasExportedAs === "default" ? "default as " : ""}${
item.name
}`;
}

function getSchemaFromPath(path: string, outputPath: string): string {
const relpath = relative(outputPath, path);
const relparts = relpath.split("/");

// Make sure path is part of a schema path
if (relparts.length === 2) {
return relparts[0];
}

return null;
}

export const makeGenerateIndexFile: (
config: GenerateIndexFileConfig,
) => PreRenderHook = (config) => (outputAcc, instantiatedConfig) => {
const allExports: Record<string, ExportsItem[]> = {};
const schemaExports: Record<string, SchemaExports> = {};
const { outputPath } = instantiatedConfig;

for (const path of Object.keys(outputAcc)) {
const schema = getSchemaFromPath(path, outputPath);

if (schema && !schemaExports[schema]) {
schemaExports[schema] = {
path: join(outputPath, schema),
items: { [path]: [] },
};
} else if (schema) {
schemaExports[schema].items[path] = [];
}

const file = outputAcc[path];
allExports[path] = [];
for (const declaration of file.declarations) {
Expand All @@ -50,23 +80,76 @@ export const makeGenerateIndexFile: (
}

const { name, exportAs, declarationType } = declaration;
allExports[path].push({
const declarationItem = {
name,
wasExportedAs: exportAs,
exportAsType: ["typeDeclaration", "interface"].includes(
declarationType,
),
});
};

// Add to the global index
allExports[path].push(declarationItem);

// Add to the schema index
if (schema) {
schemaExports[schema].items[path].push(declarationItem);
}
}
}

const schemaIndexFiles: Record<string, FileContents> = {};
for (const schema of Object.keys(schemaExports)) {
const schemaPath = schemaExports[schema].path;
const schemaLines = Object.keys(schemaExports[schema].items).map(
(itemPath) => {
const schemaDeclExports = schemaExports[schema].items[itemPath];
if (schemaDeclExports.length === 0) {
return "";
}

let relativePath = relative(schemaPath, itemPath);
// Fix Windows-style paths in import line
if (sep === "\\") {
relativePath = relativePath.replaceAll("\\", "/");
}

const line = `export { ${schemaDeclExports
.map(stringifyExportItem)
.join(", ")} } from './${relativePath}';`;

return line;
},
);

const schemaIndexFile: FileContents = {
declarations: [
{
declarationType: "generic",
lines: schemaLines,
},
],
};

const schemaIndexPath = join(schemaPath, "index");
schemaIndexFiles[schemaIndexPath] = schemaIndexFile;
}

// Return now if using schema-indexes to avoid a conflicting global index
if (Object.values(schemaIndexFiles).length > 0) {
return {
...outputAcc,
...schemaIndexFiles,
};
}

const lines = Object.keys(allExports).map((path) => {
const exports = allExports[path];
if (exports.length === 0) {
return "";
}

let relativePath = relative(instantiatedConfig.outputPath, path);
let relativePath = relative(outputPath, path);
// Fix Windows-style paths in import line
if (sep === "\\") {
relativePath = relativePath.replaceAll("\\", "/");
Expand All @@ -88,7 +171,7 @@ export const makeGenerateIndexFile: (
],
};

const path = join(instantiatedConfig.outputPath, "index");
const path = join(outputPath, "index");

return {
...outputAcc,
Expand Down