const replace = require ( 'replace-in-file' ) ; import { sync } from 'glob' ; import * as fs from './fs' ; import * as path from './path' ; import { logError , logInfo } from '../helpers' ; param { string } oldName param { string } newName param { ... string } pathComponents export function source ( oldName , newName , ... pathComponents ) { const directory = path . project . resolve ( ... pathComponents ) ; try { replace . sync ( { files : directory + '/**/*' , ignore : directory + '/Pods/**/*' , from : [ new RegExp ( oldName , 'g' ) , new RegExp ( oldName . toLocaleLowerCase ( ) , 'g' ) ] , to : [ newName , newName . toLocaleLowerCase ( ) ] , glob : { dot : true } } ) ; } catch ( err ) { logError ( `renaming project within source` , err ) ; process . exit ( 1 ) ; } logInfo ( `renamed project within source in ${ directory }` ) ; } param { string } oldPkg param { string } newPkg param { ... string } pathComponents export function pkgDirectory ( oldPkg , newPkg , ... pathComponents ) { const directory = path . project . resolve ( ... pathComponents ) ; const oldPathPart = oldPkg . replace ( /\./g , '/' ) ; const newPathPart = newPkg . replace ( /\./g , '/' ) ; try { const results = getMatchingFiles ( directory , oldPathPart ) ; results . forEach ( oldPath => { const newPath = oldPath . replace ( oldPathPart , newPathPart ) ; fs . moveSync ( oldPath , newPath ) ; } ) ; oldPkg . split ( '.' ) . reduce < string [ ] > ( ( parts , part , index , arr ) => { const pattern = [ ... arr . slice ( 0 , index ) , part ] . join ( '/' ) ; parts . push ( ... getMatchingFiles ( directory , pattern ) ) ; return parts ; } , [ ] ) . reverse ( ) . forEach ( dir => { const contents = fs . pathExistsSync ( dir ) && fs . readdirSync ( dir ) ; if ( Array . isArray ( contents ) && contents . length === 0 ) { fs . removeSync ( dir ) ; } } ) ; } catch ( err ) { logError ( 'renaming project files' , err ) ; process . exit ( 1 ) ; } logInfo ( `renamed project files in ${ directory }` ) ; } param { string } oldName param { string } newName param { ... string } pathComponents export function files ( oldName , newName , ... pathComponents ) { const directory = path . project . resolve ( ... pathComponents ) ; try { const results = getMatchingFiles ( directory , oldName ) ; results . forEach ( oldPath => { let newPath = oldPath . split ( '/' ) ; const n = newPath . length - 1 ; newPath [ n ] = newPath [ n ] . replace ( oldName , newName ) . replace ( oldName . toLocaleLowerCase ( ) , newName . toLocaleLowerCase ( ) ) ; newPath = newPath . join ( '/' ) ; fs . renameSync ( oldPath , newPath ) ; } ) ; } catch ( err ) { logError ( 'renaming project files' , err ) ; process . exit ( 1 ) ; } logInfo ( `renamed project files in ${ directory }` ) ; } function getMatchingFiles ( directory , oldName ) { const globOptions = { nosort : true , dot : true } ; const results = [ ... sync ( directory + '/**/*' + oldName + '*' , globOptions ) , ... sync ( directory + '/**/*' + oldName . toLocaleLowerCase ( ) + '*' , globOptions ) ] ; const uniqueResults = Array . from ( new Set ( results ) ) ; uniqueResults . sort ( ( a , b ) => { return b . length - a . length ; } ) ; return uniqueResults ; }