1
1
var program = require ( 'commander' )
2
2
var fs = require ( 'fs-extra' )
3
3
var path = require ( 'path' )
4
+ var utils = require ( './utils/utils.js' )
5
+
4
6
var templateFileLocation = {
5
7
'yaml' : 'parsers/yaml/template.yml' ,
6
8
'json' : 'parsers/json/template.json'
7
9
}
8
10
11
+ var suppFileExtns = [ '.json' , '.yml' , '.yaml' ]
12
+
9
13
var _copyTemplateToPath = function ( templateFilePath , destFilePath ) {
10
14
let sourcePath = ''
11
15
try {
@@ -24,25 +28,26 @@ var _copyTemplateToPath = function (templateFilePath, destFilePath) {
24
28
} )
25
29
}
26
30
program
27
- . option ( '-t, --template <type >' , 'specify input template (json|yaml) [yaml] ' , ' yaml' )
31
+ . option ( '-t, --template <location >' , 'specify input template location ' , templateFileLocation . yaml )
28
32
. parse ( process . argv )
29
33
30
34
var initArgs = program . args
31
35
32
36
if ( initArgs . length > 1 ) {
33
- console . error ( 'Filename required' )
37
+ console . error ( 'Filename required. Please specify a valid' , suppFileExtns . join ( '/' ) , 'template location as argument. ')
34
38
process . exit ( 1 )
35
39
}
36
40
37
- if ( program . template === 'yaml' ) {
38
- _copyTemplateToPath ( templateFileLocation . yaml , initArgs [ 0 ] )
39
- } else if ( program . template === 'json' ) {
40
- _copyTemplateToPath ( templateFileLocation . json , initArgs [ 0 ] )
41
- } else {
42
- let type = path . extname ( program . template )
43
- if ( type !== '.yml' && type !== '.yaml' && type !== '.json' ) {
44
- console . error ( 'Error : Please select proper template.' )
45
- process . exit ( 1 )
46
- }
47
- _copyTemplateToPath ( program . template , initArgs [ 0 ] )
41
+ if ( utils . fileExists ( program . template ) === false ) {
42
+ console . error ( 'Please specify a valid' , suppFileExtns . join ( '/' ) , 'template file location.' )
43
+ process . exit ( 1 )
48
44
}
45
+
46
+ let fileExt = path . extname ( program . template )
47
+
48
+ if ( suppFileExtns . indexOf ( fileExt ) === - 1 ) {
49
+ console . error ( 'File extension not supported. Please use supported file extensions: ' + suppFileExtns . join ( ', ' ) )
50
+ process . exit ( 1 )
51
+ }
52
+
53
+ _copyTemplateToPath ( program . template , initArgs [ 0 ] )
0 commit comments