import * as fs from 'fs' ; import * as path from 'path' ; let demoOrder = 1 ; const DEMO_FILE_REG = /demo\/.*\.(js|jsx|ts|tsx)$/ ; const changeFileExt = ( file , ext ) => { return path . join ( path . dirname ( file ) , path . basename ( file , path . extname ( file ) ) + ext ) ; } ; export default ( fileInfo , api , options ) => { const j = api . jscodeshift ; const basename = path . basename ( fileInfo . path ) ; if ( basename === 'build.json' ) { let hasChanged = false ; let config = JSON . parse ( fileInfo . source ) ; const pluginList = config . plugins || [ ] ; for ( let i = 0 ; i < pluginList . length ; i ++ ) { if ( pluginList [ 0 ] [ 0 ] === 'build-plugin-rax-component' ) { config = Object . assign ( { } , config , pluginList [ 0 ] [ 1 ] || { } ) ; if ( config . forceInline !== undefined ) { config . inlineStyle = config . forceInline ; delete config . forceInline ; } config . plugins . splice ( i , 1 , 'build-plugin-component' ) ; hasChanged = true ; } } if ( hasChanged ) { return JSON . stringify ( config , null , '  ' ) ; } return null ; } else if ( basename === 'package.json' ) { const config = JSON . parse ( fileInfo . source ) ; const { devDependencies = { } } = config ; if ( devDependencies [ 'build-plugin-rax-component' ] ) { devDependencies [ 'build-plugin-component' ] = '^1.0.0' ; delete devDependencies [ 'build-plugin-rax-component' ] ; return JSON . stringify ( config , null , '  ' ) ; } return null ; } else if ( options . dry !== true && DEMO_FILE_REG . test ( fileInfo . path ) ) { const transform = j ( fileInfo . source ) ; transform . find ( j . ExpressionStatement ) . forEach ( ( p ) => { const { expression } = p . node ; if ( expression . callee . name === 'render' && expression . arguments [ 0 ] . type === 'JSXElement' ) { const demoFnName = 'App' ; const demoJSXElement = expression . arguments [ 0 ] ; j ( p ) . replaceWith ( j . functionDeclaration ( j . identifier ( demoFnName ) , [ ] , j . blockStatement ( [ j . returnStatement ( demoJSXElement ) ] ) ) ) . insertAfter ( j . exportDeclaration ( true , { type : 'Identifier' , name : demoFnName } ) ) ; } } ) ; transform . find ( j . ImportDeclaration ) . forEach ( ( p ) => { const { node } = p ; if ( node . source . value . indexOf ( 'driver-' ) > - 1 ) { j ( p ) . remove ( ) ; } } ) ; fs . writeFileSync ( changeFileExt ( fileInfo . path , '.md' ) , `${ '---\n' + 'title: Baisc\n' + `order: ${ demoOrder ++ }\n` + '---\n' + '\n' + `${ fileInfo . path } usage\n` + '```jsx\n' }${ transform . toSource ( ) }\`\`\`` , 'utf8' ) ; return fileInfo . source ; } else { return fileInfo . source ; } } ;