import path from 'path' ; import getInternalImports from '@codemods/get-internal-imports/getInternalImports' ; import { getRelativePath } from '@codemods/sub-graph/getRelativePath' ; import { getFilename } from '@codemods/sub-graph/getFilename' ; import { runCodemodFile } from '@codemods/move-file-to-package/runCodemodFile' ; import getInternalExportsSource from '@codemods/move-file-to-package/getInternalExportsSource' ; import { moveFileToPackage } from '@codemods/move-file-to-package/moveFileToPackageCore' ; const cwd = process . cwd ( ) ; ( async ( ) => { try { const modelRelativePath = 'packages/main/src/models/index.ts' ; const modelsPath = path . join ( cwd , modelRelativePath ) ; const models = await runCodemodFile ( getInternalExportsSource , modelsPath ) ; let movedModels = [ ] ; console . log ( { models } ) ; for ( const model of models ) { const modelpath = getRelativePath ( modelsPath , model ) ; const modelWithExtension = getFilename ( modelpath ) ; if ( modelWithExtension . includes ( '.js' ) ) { console . log ( 'we cant move .js files: ' , modelpath ) ; continue ; } if ( ! modelWithExtension ) { console . log ( 'model not found: ' , modelWithExtension ) ; continue ; } const internalImports = await runCodemodFile ( getInternalImports , modelWithExtension ) ; if ( internalImports . length > 0 ) { const imports = internalImports . map ( ( i ) => getRelativePath ( modelpath , i . source . value ) ) ; console . log ( 'sent run codemod on model because it has internal imports' , { model } ) ; continue ; } console . log ( 'move model: ' , modelpath ) ; const getDefaultName = ( m ) => { const paths = m . split ( '/' ) ; const modelName = paths [ paths . length - 1 ] ; if ( modelName . includes ( 'Model' ) ) { return modelName . replace ( 'Model' , '' ) ; } return modelName ; } ; const modulesPath = '/modules/' ; const getTo = ( m ) => { const mPath = m . indexOf ( modulesPath ) ; const to = m . slice ( mPath + modulesPath . length ) ; return `packages/modules/src/${ to }.ts` ; } ; if ( ! modelpath . includes ( modulesPath ) ) { console . log ( 'modelPath is not on modules we cant move' ) ; continue ; } const from = `${ modelpath }.ts` ; const config = { from , fromPackage : 'packages/main' , to : getTo ( modelpath ) , toPackage : 'packages/modules' , toPackageName : '@repo/modules' , defaultName : getDefaultName ( modelpath ) , namespaceName : null } ; console . log ( { config } ) ; await moveFileToPackage ( config ) ; movedModels = [ ... movedModels , modelpath ] ; } console . log ( { movedModels } ) ; } catch ( err ) { console . log ( 'e' , err ) ; } process . exit ( 0 ) ; } ) ( ) ;