@@ -37,12 +37,11 @@ export default class Chatdown extends Command {
3737 } )
3838 }
3939
40+ let outputDir = flags . out ? path . resolve ( flags . out ) : null
41+
4042 if ( inputIsDirectory ) {
4143 let inputDir = flags . in ? flags . in . trim ( ) : ''
42- let outputDir = ( flags . out ) ? flags . out . trim ( ) : './'
43- if ( outputDir . substr ( 0 , 2 ) === './' ) {
44- outputDir = path . resolve ( process . cwd ( ) , outputDir . substr ( 2 ) )
45- }
44+
4645 const len = await this . processFiles ( inputDir , outputDir )
4746 if ( len === 0 ) {
4847 throw new CLIError ( 'No chat files found at: ' + flags . in )
@@ -51,9 +50,10 @@ export default class Chatdown extends Command {
5150 return
5251 } else {
5352 const fileContents = await this . getInput ( flags . in )
53+ const fileName = flags . in ? this . getFileName ( flags . in ) : ''
5454 if ( fileContents ) {
5555 const activities = await chatdown ( fileContents , flags )
56- const writeConfirmation = await this . writeOut ( activities )
56+ const writeConfirmation = await this . writeOut ( activities , fileName , outputDir )
5757 /* tslint:disable:strict-type-predicates */
5858 if ( typeof writeConfirmation === 'string' ) {
5959 process . stdout . write ( `${ chalk . green ( 'Successfully wrote file:' ) } ${ writeConfirmation } \n` )
@@ -91,21 +91,20 @@ export default class Chatdown extends Command {
9191 }
9292 }
9393
94+ private getFileName ( file : any ) {
95+ let fileName = path . basename ( file , path . extname ( file ) )
96+ return fileName
97+ }
98+
9499 private async processFiles ( inputDir : any , outputDir : any ) {
95100 return new Promise ( async ( resolve , reject ) => {
96101 let files = glob . sync ( inputDir , { ignore : [ '**/node_modules/**' ] } )
97102 /* tslint:disable:prefer-for-of */
98103 for ( let i = 0 ; i < files . length ; i ++ ) {
99104 try {
100- let fileName = files [ i ]
101- if ( files [ i ] . lastIndexOf ( '/' ) !== - 1 ) {
102- fileName = files [ i ] . substr ( files [ i ] . lastIndexOf ( '/' ) )
103- }
104- fileName = fileName . split ( '.' ) [ 0 ]
105+ const fileName = this . getFileName ( files [ i ] )
105106 let activities = await chatdown ( await utils . readTextFile ( files [ i ] ) )
106- let writeFile = `${ outputDir } /${ fileName } .transcript`
107- await fs . ensureFile ( writeFile )
108- await fs . writeJson ( writeFile , activities , { spaces : 2 } )
107+ await this . writeOut ( activities , fileName , outputDir )
109108 } catch ( e ) {
110109 if ( e . message . match ( / n o s u c h f i l e o r d i r e c t o r y / ) ) {
111110 reject ( new CLIError ( e . message ) )
@@ -119,7 +118,13 @@ export default class Chatdown extends Command {
119118 } )
120119 }
121120
122- private async writeOut ( activities : any ) {
121+ private async writeOut ( activities : any , fileName : string , outputDir : any ) {
122+ if ( fileName && outputDir ) {
123+ let writeFile = path . join ( outputDir , `${ fileName } .transcript` )
124+ await fs . ensureFile ( writeFile )
125+ await fs . writeJson ( writeFile , activities , { spaces : 2 } )
126+ return writeFile
127+ }
123128 const output = JSON . stringify ( activities , null , 2 )
124129 await new Promise ( done => process . stdout . write ( output , 'utf-8' , ( ) => done ( ) ) )
125130 return true
0 commit comments