Skip to content

Commit

Permalink
feat(cli): find nodes dynamically using syntax kind instead of index
Browse files Browse the repository at this point in the history
  • Loading branch information
satya-achanta-venkata committed Oct 27, 2023
1 parent c0616c7 commit e848da7
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions packages/pharos-cli/src/utils/initComponentsUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@ export async function addNewComponentToInitComponents(
SyntaxKind.ImportDeclaration
);

importStatementsDeclarations[0].addNamedImport(componentName);
importStatementsDeclarations.forEach((dec) => {
const importClause = dec.getImportClause();
if (importClause && importClause.getNamedImports().length > 0) {
dec.addNamedImport(componentName);
}
});

const registerComponentsExpressions = sourceFile.getDescendantsOfKind(
SyntaxKind.ArrayLiteralExpression
);
const registerComponentsExpressions = sourceFile.getDescendantsOfKind(SyntaxKind.CallExpression);

registerComponentsExpressions[0].addElement(componentName);
registerComponentsExpressions.forEach((callExp) => {
callExp.forEachChild((expChild) => {
if (expChild.isKind(SyntaxKind.ArrayLiteralExpression)) {
expChild.addElement(componentName);
}
});
});

await sourceFile.save();
}

0 comments on commit e848da7

Please sign in to comment.