@@ -5,7 +5,22 @@ const helpers = require('./../parser/lufile/helpers')
55const luObject = require ( './../parser/lufile/classes/luObject' )
66/* tslint:disable:prefer-for-of no-unused*/
77
8- export async function getLuFiles ( input : string | undefined , recurse = false ) : Promise < Array < any > > {
8+ export async function getLuObjects ( stdin : string , input : string | undefined , recurse = false ) {
9+ let luObjects : any = [ ]
10+ if ( stdin ) {
11+ luObjects . push ( new luObject ( 'stdin' , stdin ) )
12+ } else {
13+ let luFiles = await getLuFiles ( input , recurse )
14+ for ( let i = 0 ; i < luFiles . length ; i ++ ) {
15+ let luContent = await getContentFromFile ( luFiles [ i ] )
16+ luObjects . push ( new luObject ( path . resolve ( luFiles [ i ] ) , luContent ) )
17+ }
18+ }
19+
20+ return luObjects
21+ }
22+
23+ async function getLuFiles ( input : string | undefined , recurse = false ) : Promise < Array < any > > {
924 let filesToParse = [ ]
1025 let fileStat = await fs . stat ( input )
1126 if ( fileStat . isFile ( ) ) {
@@ -25,28 +40,13 @@ export async function getLuFiles(input: string | undefined, recurse = false): Pr
2540 return filesToParse
2641}
2742
28- export async function getLuObjects ( stdin : string , input : string | undefined , recurse = false ) {
29- let luObjects : any = [ ]
30- if ( stdin ) {
31- luObjects . push ( new luObject ( 'stdin' , stdin ) )
32- } else {
33- let luFiles = await getLuFiles ( input , recurse )
34- for ( let i = 0 ; i < luFiles . length ; i ++ ) {
35- let luContent = await readLuFile ( luFiles [ i ] )
36- luObjects . push ( new luObject ( path . resolve ( luFiles [ i ] ) , luContent ) )
37- }
38- }
39-
40- return luObjects
41- }
42-
4343export async function getContentFromFile ( file : string ) {
4444 // catch if input file is a folder
4545 if ( fs . lstatSync ( file ) . isDirectory ( ) ) {
46- throw new CLIError ( 'Sorry, "' + file + '" is a directory! Please try a LUIS/ QnA Maker JSON file as input. ' )
46+ throw new CLIError ( 'Sorry, "' + file + '" is a directory! Unable to read as a file' )
4747 }
4848 if ( ! fs . existsSync ( path . resolve ( file ) ) ) {
49- throw new CLIError ( 'Sorry unable to open [' + file + ']' )
49+ throw new CLIError ( 'Sorry [' + file + '] does not exist ' )
5050 }
5151 let fileContent
5252 try {
@@ -58,7 +58,7 @@ export async function getContentFromFile(file: string) {
5858}
5959
6060export async function generateNewFilePath ( outFileName : string , inputfile : string , isLu : boolean , prefix = '' ) : Promise < string > {
61- let base = ! path . isAbsolute ( outFileName ) ? path . join ( process . cwd ( ) , outFileName ) : outFileName
61+ let base = path . resolve ( outFileName )
6262 let extension = path . extname ( base )
6363 if ( extension ) {
6464 let root = path . dirname ( base )
@@ -77,27 +77,49 @@ export async function generateNewFilePath(outFileName: string, inputfile: string
7777}
7878
7979export async function generateNewTranslatedFilePath ( fileName : string , translatedLanguage : string , output : string ) : Promise < string > {
80- let newPath = ''
81- if ( ! path . isAbsolute ( output ) ) {
82- newPath = path . join ( process . cwd ( ) , '' )
83- }
84-
80+ let newPath = path . resolve ( output )
8581 newPath = path . join ( output , translatedLanguage )
8682 await fs . mkdirp ( newPath )
8783 return path . join ( newPath , fileName )
8884}
8985
90- async function readLuFile ( file : string ) {
91- if ( ! fs . existsSync ( path . resolve ( file ) ) ) {
92- throw new CLIError ( `Sorry unable to open [${ file } ]` )
93- }
94- let fileContent
95- try {
96- fileContent = await utils . readTextFile ( file )
97- } catch ( err ) {
98- throw new CLIError ( `Sorry, error reading file: ${ file } ` )
86+ export function validatePath ( outputPath : string , defaultFileName : string , forceWrite = false ) : string {
87+ let completePath = path . resolve ( outputPath )
88+ const containingDir = path . dirname ( completePath )
89+
90+ // If the cointaining folder doesnt exist
91+ if ( ! fs . existsSync ( containingDir ) ) throw new CLIError ( `Containing directory path doesn't exist: ${ containingDir } ` )
92+
93+ const baseElement = path . basename ( completePath )
94+ const pathAlreadyExist = fs . existsSync ( completePath )
95+
96+ // If the last element in the path is a file
97+ if ( baseElement . includes ( '.' ) ) {
98+ return pathAlreadyExist && ! forceWrite ? enumerateFileName ( completePath ) : completePath
9999 }
100- return fileContent
100+
101+ // If the last element in the path is a folder
102+ if ( ! pathAlreadyExist ) throw new CLIError ( `Target directory path doesn't exist: ${ completePath } ` )
103+ completePath = path . join ( completePath , defaultFileName )
104+ return fs . existsSync ( completePath ) && ! forceWrite ? enumerateFileName ( completePath ) : completePath
105+ }
106+
107+ function enumerateFileName ( filePath : string ) : string {
108+ const fileName = path . basename ( filePath )
109+ const containingDir = path . dirname ( filePath )
110+
111+ if ( ! fs . existsSync ( containingDir ) ) throw new CLIError ( `Containing directory path doesn't exist: ${ containingDir } ` )
112+
113+ const extension = path . extname ( fileName )
114+ const baseName = path . basename ( fileName , extension )
115+ let nextNumber = 0
116+ let newPath = ''
117+
118+ do {
119+ newPath = path . join ( containingDir , baseName + `(${ ++ nextNumber } )` + extension )
120+ } while ( fs . existsSync ( newPath ) )
121+
122+ return newPath
101123}
102124
103125export async function detectLuContent ( stdin : string , input : string ) {
0 commit comments