1
1
const _ = require ( 'lodash' ) ;
2
2
const fs = require ( 'fs' ) ;
3
3
4
+ const Handlebars = require ( 'handlebars' ) ;
4
5
const Logger = require ( './logger.js' ) ;
5
6
6
7
const logger = new Logger ( 'localfs' ) ;
7
- const Handlebars = require ( "handlebars" ) ;
8
-
9
8
10
9
function LocalFS ( ) { }
11
10
@@ -29,26 +28,52 @@ LocalFS.prototype.checkExists = function(name, output, showOutput) {
29
28
}
30
29
return true ;
31
30
}
31
+
32
+ if ( fs . existsSync ( `${ name } .hbs` ) ) {
33
+ if ( showOutput ) {
34
+ logger . showResult ( `A template file exists for ${ output } .` ) ;
35
+ }
36
+ return true ;
37
+ }
38
+
32
39
if ( showOutput ) {
33
40
logger . justShow ( `${ output } does not exists.` ) ;
34
41
}
42
+
35
43
return false ;
36
44
} ;
37
45
38
46
LocalFS . prototype . readFile = function ( name , _showOnError ) {
39
- let templateSrc = fs . readFileSync ( name , 'utf8' , function ( error , data ) {
47
+ let useTemplating = false ;
48
+
49
+ // if a file with a .hbs exist, use it for templating
50
+ if ( fs . existsSync ( `${ name } .hbs` ) ) {
51
+ name = `${ name } .hbs` ;
52
+ useTemplating = true ;
53
+ }
54
+
55
+ // Read the 'name' file (or the 'name.tml')
56
+ const src = fs . readFileSync ( name , 'utf8' , ( error , _data ) => {
40
57
if ( ! error ) {
41
58
logger . showResult ( `Read file ${ name } successfully.` ) ;
42
59
} else {
43
60
logger . showError ( `Error in reading file ${ name } ` ) ;
44
61
}
45
62
} ) ;
46
- if ( templateSrc === null ) {
47
- return null
63
+
64
+ // If the file was not found (or cannot be read)
65
+ if ( src === null ) {
66
+ return null ;
48
67
}
49
68
50
- var template = Handlebars . compile ( templateSrc )
51
- return template ( process . env )
69
+ // if the file is not a template file
70
+ if ( ! useTemplating ) {
71
+ return src ;
72
+ }
73
+ logger . showResult ( `Running templating engine on ${ name } ` ) ;
74
+ // If the file is a template file, let handlebars do its magic
75
+ const template = Handlebars . compile ( src ) ;
76
+ return template ( process . env ) ;
52
77
} ;
53
78
54
79
LocalFS . prototype . writeFile = function ( name , content ) {
@@ -91,7 +116,7 @@ LocalFS.prototype.writeStream = function(name) {
91
116
} ;
92
117
93
118
LocalFS . prototype . getFileName = function ( fileNameWithExtension ) {
94
- return fileNameWithExtension . replace ( / \. [ ^ / . ] + $ / , '' ) ;
119
+ return fileNameWithExtension . replace ( / \. . + $ / , '' ) ;
95
120
} ;
96
121
97
122
module . exports = LocalFS ;
0 commit comments