I have a script i'm using to extract types from a github repository so I don't have to manually check and sync types when the repository changes.
As a simple replication of what im trying to achieve:
This file here has types exported, I'm actually looping over all files in the data folder, however I'll use this one as an example.
I'm looking for any types that extend HassEntityBase, and i'm attempting to pull through all related types
I have this working for everything except HvacModes and I'm assuming it's because it's derived from the constant above (which i also need to retrieve)
Is this possible with ts-morph? I feel like i've exhausted all options
There's another file that has:
export interface ScriptEntity extends HassEntityBase {
attributes: HassEntityAttributeBase & {
last_triggered: string;
mode: (typeof MODES)[number];
current?: number;
max?: number;
};
}
So it's creating the type inline, and I've managed to extract this and the constant with this:
const exploreTypeNode = (typeNode: TypeNode, sourceFile: SourceFile) => {
let output = '';
const typeText = typeNode.getText();
// Handle 'typeof' cases
if (typeText.includes('typeof')) {
const match = typeText.match(/typeof (\w+)/);
if (match) {
const keyword = match[1];
const constantText = extractConstants(sourceFile, keyword);
if (constantText) {
output += `export const ${constantText}\n`;
}
}
}
// Handle object types and other complex types
typeNode.forEachChild(child => {
child.forEachDescendantAsArray().forEach(descendant => {
const decKind = descendant.getKind();
if (decKind === SyntaxKind.TypeReference || decKind === SyntaxKind.TypeLiteral) {
output += exploreTypeNode(child as TypeNode, sourceFile);
}
});
});
return output;
};
However this doesn't work obviously with the ClimateEntity because it's referenced above
I've also tried adding the alias Symbol, then retrieving declarations from that but unfortunately no luck..
Any help would be fantastic