import { ASTPath , Transform , ImportDeclaration , ImportSpecifier } from 'jscodeshift' ; import { filterImportSpecifiers , renameImportSpecifiers , ImportSpecifierArray , renameImports , hasImportSpecifiers , } from './utils' ; const mainPackage = '@workday/canvas-kit-labs-react' ; const headerPackage = '@workday/canvas-kit-labs-react/header' ; const searchBarRenameImports = [ 'SearchBar' , 'SearchBarProps' , 'SearchBarState' ] ; const searchBarImportSpecifiers = [ ... searchBarRenameImports , 'SearchThemeAttributes' , 'SearchThemes' , ] ; const renameMap = { SearchBar : 'SearchForm' , SearchBarProps : 'SearchFormProps' , SearchBarState : 'SearchFormState' , } ; const transform = ( file , api ) => { const j = api . jscodeshift ; const root = j ( file . source ) ; if ( ! hasImportSpecifiers ( api , root , '@workday/canvas-kit-labs-react/header' , Object . keys ( renameMap ) ) ) { return file . source ; } function insertImportDeclarationBefore ( nodePath , specifiers , sourcePath ) { nodePath . insertBefore ( j . importDeclaration ( specifiers , j . stringLiteral ( sourcePath ) ) ) ; } root . find ( j . ImportDeclaration , { source : { value : mainPackage } } ) . forEach ( nodePath => { const importSpecifiers = nodePath . value . specifiers || [ ] ; const searchBarImports = filterImportSpecifiers ( importSpecifiers ) ; renameImportSpecifiers ( searchBarImports , renameMap ) ; } ) ; root . find ( j . ImportDeclaration , { source : { value : headerPackage } } ) . forEach ( nodePath => { const importSpecifiers = nodePath . value . specifiers || [ ] ; const headerSpecifiers = [ ] as ImportSpecifierArray ; const searchBarSpecifiers = [ ] as ImportSpecifier [ ] ; importSpecifiers . forEach ( specifier => { if ( specifier . type === 'ImportSpecifier' && searchBarImportSpecifiers . includes ( specifier . imported . name ) ) { searchBarSpecifiers . push ( specifier ) ; } else { headerSpecifiers . push ( specifier ) ; } } ) ; root . find ( j . ImportDeclaration , { source : { value : headerPackage } } ) . remove ( ) ; if ( searchBarSpecifiers . length ) { const renamedSearchBarSpecifiers = renameImportSpecifiers ( searchBarSpecifiers , renameMap ) ; insertImportDeclarationBefore ( nodePath , renamedSearchBarSpecifiers , '@workday/canvas-kit-labs-react/search-form' ) ; } if ( headerSpecifiers . length ) { insertImportDeclarationBefore ( nodePath , headerSpecifiers , headerPackage ) ; } } ) ; return renameImports ( api , root , '@workday/canvas-kit-labs-react/header' , renameMap ) . toSource ( ) ; } ; export default transform ;