import * as fs from 'fs' ; import * as nodepath from 'path' ; const cwd = nodepath . resolve ( process . cwd ( ) ) ; let nameStore = [ ] ; module function getImportsTree ( fullname ) { let importFullnames = [ ] ; isImported ( fullname ) ; let array = fs . readFileSync ( fullname ) . toString ( ) . split ( '\n' ) ; let parentPath = splitPath ( fullname ) ; for ( let i = 0 ; i < array . length ; i ++ ) { let line = array [ i ] ; if ( line . startsWith ( 'import' ) ) { let importName = getImportName ( line ) ; let importFullname = parentPath + nodepath . sep + importName ; if ( isImported ( importFullname ) ) continue ; importFullnames . push ( importFullname ) ; importFullnames = importFullnames . concat ( getImportsTree ( importFullname ) ) ; } } return importFullnames ; } function getImportName ( line ) { let importName = line . split ( '"' ) . slice ( - 2 , - 1 ) [ 0 ] ; return importName ; } method param { Object } initial param { String } input return { Object } function readFileLinesToObject ( initialFileMap , fullname ) { const array = fs . readFileSync ( fullname ) . toString ( ) . split ( '\n' ) ; isImported ( fullname ) ; const { fileMap , buffer } = array . reduce ( ( obj , line ) => { const { fileMap , buffer } = obj ; if ( line . startsWith ( 'import' ) ) { const newBuffer = buffer + '//' + line + '\n' ; const newFileMap = importFileToObject ( fileMap , fullname , line ) ; return { fileMap : newFileMap , buffer : newBuffer } ; } else { const fixedLine = line . replace ( '\r' , ' ' ) ; const newBuffer = buffer + fixedLine + '\n' ; return { fileMap , buffer : newBuffer } ; } } , { fileMap : initialFileMap , buffer : '' } ) ; return { ... fileMap , [ getShortName ( fullname ) ] : buffer } ; } method param { Array } initial param { String } input return { Array } function readFileLinesToArray ( initialFileArray , fullname ) { const array = fs . readFileSync ( fullname ) . toString ( ) . split ( '\n' ) ; isImported ( fullname ) ; const { fileArray , buffer } = array . reduce ( ( obj , line ) => { const { fileArray , buffer } = obj ; if ( line . startsWith ( 'import' ) ) { const newBuffer = buffer + '//' + line + '\n' ; const newFileArray = importFileToArray ( fileArray , fullname , line ) ; return { fileArray : newFileArray , buffer : newBuffer } ; } else { const fixedLine = line . replace ( '\r' , ' ' ) ; const newBuffer = buffer + fixedLine + '\n' ; return { fileArray , buffer : newBuffer } ; } } , { fileArray : initialFileArray , buffer : '' } ) ; return [ ... fileArray , [ getShortName ( fullname ) , buffer ] ] ; } method param { String } input return { String } function readFileLinesToString ( fullname ) { let buffer = '' ; isImported ( fullname ) ; let array = fs . readFileSync ( fullname ) . toString ( ) . split ( '\n' ) ; for ( let i = 0 ; i < array . length ; i ++ ) { let line = array [ i ] ; if ( line . startsWith ( 'import' ) ) { buffer += importFileToString ( fullname , line ) + '\n' ; } else { line = line . replace ( '\r' , ' ' ) ; buffer += line + '\n' ; } } return buffer ; } method param { Object } fileMap param { String } fullname param { String } line return { Object } function importFileToObject ( fileMap , fullname , line ) { let importName = line . replace ( /import[\s]+/i , '' ) . replace ( /\"/gi , '' ) . replace ( ';' , '' ) ; importName = importName . replace ( '\r' , '' ) ; if ( isImported ( importName ) ) { return fileMap ; } if ( importName . indexOf ( '/' ) == 0 ) { return readFileLinesToObject ( fileMap , nodepath . join ( cwd , importName ) ) ; } let parentPath = splitPath ( fullname ) ; return readFileLinesToObject ( fileMap , nodepath . join ( parentPath , importName ) ) ; } method param { Array } fileArray param { String } fullname param { String } line return { Array } function importFileToArray ( fileArray , fullname , line ) { let importName = line . replace ( /import[\s]+/i , '' ) . replace ( /\"/gi , '' ) . replace ( ';' , '' ) ; importName = importName . replace ( '\r' , '' ) ; if ( isImported ( importName ) ) { return fileArray ; } if ( importName . indexOf ( '/' ) == 0 ) { return readFileLinesToArray ( fileArray , nodepath . join ( cwd , importName ) ) ; } let parentPath = splitPath ( fullname ) ; return readFileLinesToArray ( fileArray , nodepath . join ( parentPath , importName ) ) ; } method param { Object } fileMap param { String } fullname param { String } line return { String } function importFileToString ( fullname , line ) { let buffer = '//' + line + '\n' ; let importName = line . replace ( /import[\s]+/i , '' ) . replace ( /\"/gi , '' ) . replace ( ';' , '' ) ; importName = importName . replace ( '\r' , '' ) ; if ( isImported ( importName ) ) { buffer += '// exists\n' ; return buffer ; } if ( importName . indexOf ( '/' ) == 0 ) { return buffer + readFileLinesToString ( nodepath . join ( cwd , importName ) ) ; } let parentPath = splitPath ( fullname ) ; return buffer + readFileLinesToString ( nodepath . join ( parentPath , importName ) ) ; } method param { String } fullname return { Boolean } function getShortName ( fullname ) { let array = fullname . split ( nodepath . sep ) ; array = array . length <= 1 ? fullname . split ( '/' ) : array ; return array . pop ( ) ; } function isImported ( fullname ) { const name = getShortName ( fullname ) ; if ( nameStore . indexOf ( name ) > - 1 ) { return true ; } nameStore . push ( name ) ; return false ; } method param { String } fullname return { Array } function splitPath ( fullname ) { let array = fullname . split ( nodepath . sep ) ; array = array . length <= 1 ? fullname . split ( '/' ) : array ; let path = array . slice ( 0 , array . length - 1 ) . join ( nodepath . sep ) ; return path ; } function combine ( filename , toObject ) { nameStore = [ ] ; return new Promise ( function ( resolve , reject ) { let string = '' ; if ( toObject ) { string = readFileLinesToObject ( { } , filename ) ; } else { string = readFileLinesToArray ( [ ] , filename ) ; } resolve ( string ) ; } ) ; } export default { combine , } ;