@@ -25,59 +25,64 @@ exports.ConfigFile = function(filePath) {
25
25
26
26
this . contents = null ;
27
27
28
- this . read = function ( callback ) {
29
- fs . readFile ( filePath , function ( err , data ) {
30
- if ( err ) {
31
- if ( err . code === 'ENOENT' && that . type === 'create-if-not-exists' ) {
32
- return callback ( null , that . config ) ;
33
- } else {
34
- return callback ( err , null ) ;
35
- }
36
- }
28
+ // returns the config object from the read file
29
+ this . read = function ( ) {
30
+ var callback = function ( ) { } ;
31
+ try {
32
+ var data = fs . readFileSync ( filePath ) ;
37
33
38
34
that . contents = data . toString ( ) ;
39
- var program ;
40
35
41
- try {
42
- program = esprima . parse ( that . contents , { range : true } ) ;
43
- } catch ( ex ) {
44
- return callback ( 'could not read: ' + filePath + ' because it has syntax errors: ' + ex , null ) ;
36
+ } catch ( err ) {
37
+ if ( err . code === 'ENOENT' && that . type === 'create-if-not-exists' ) {
38
+ return that . config ;
39
+ } else {
40
+ throw err ;
45
41
}
42
+ }
46
43
47
- that . type = 'empty' ;
48
- if ( program . type === 'Program' ) {
49
- program . body . forEach ( function ( statement ) {
44
+ var program ;
50
45
51
- if ( statement . expression && statement . expression . type === 'CallExpression' ) {
52
- var call = statement . expression ;
46
+ try {
47
+ program = esprima . parse ( that . contents , { range : true } ) ;
48
+ } catch ( ex ) {
49
+ throw new Error ( 'could not read: ' + filePath + ' because it has syntax errors: ' + ex ) ;
50
+ }
51
+
52
+ that . type = 'empty' ;
53
+ if ( program . type === 'Program' ) {
54
+ program . body . forEach ( function ( statement ) {
53
55
54
- if ( call . callee . type === 'MemberExpression' && ( call . callee . object . name === 'requirejs' || call . callee . object . name === 'require' ) && call . callee . property . name === 'config' ) {
55
- that . type = call . callee . object . name === 'require' ? 'require' : 'requirejs' ;
56
- that . readObjectExpression ( call . arguments [ 0 ] , callback ) ;
56
+ if ( statement . expression && statement . expression . type === 'CallExpression' ) {
57
+ var call = statement . expression ;
58
+
59
+ if ( call . callee . type === 'MemberExpression' && ( call . callee . object . name === 'requirejs' || call . callee . object . name === 'require' ) && call . callee . property . name === 'config' ) {
60
+ that . type = call . callee . object . name === 'require' ? 'require' : 'requirejs' ;
61
+ that . readObjectExpression ( call . arguments [ 0 ] , callback ) ;
62
+ return false ;
63
+ }
64
+ } else if ( statement . type === 'VariableDeclaration' ) {
65
+ statement . declarations . forEach ( function ( declarator ) {
66
+ if ( declarator . id . name === 'require' ) {
67
+ that . type = 'var' ;
68
+ that . readObjectExpression ( declarator . init , callback ) ;
57
69
return false ;
58
70
}
59
- } else if ( statement . type === 'VariableDeclaration' ) {
60
- statement . declarations . forEach ( function ( declarator ) {
61
- if ( declarator . id . name === 'require' ) {
62
- that . type = 'var' ;
63
- that . readObjectExpression ( declarator . init , callback ) ;
64
- return false ;
65
- }
66
- } ) ;
67
-
68
- if ( that . type === 'var' ) return false ;
69
- }
70
- } ) ;
71
- }
71
+ } ) ;
72
72
73
- if ( that . type === 'empty' ) {
74
- that . config = { } ;
75
- callback ( null , that . config ) ;
76
- }
77
- } ) ;
73
+ if ( that . type === 'var' ) return false ;
74
+ }
75
+ } ) ;
76
+ }
77
+
78
+ if ( that . type === 'empty' ) {
79
+ that . config = { } ;
80
+ }
81
+
82
+ return that . config ;
78
83
} ;
79
84
80
- this . write = function ( callback ) {
85
+ this . write = function ( ) {
81
86
var contents ;
82
87
83
88
if ( this . type === 'empty' || this . type === 'create-if-not-exists' ) {
@@ -92,7 +97,7 @@ exports.ConfigFile = function(filePath) {
92
97
contents = that . contents . substring ( 0 , that . range [ 0 ] ) + that . buildConfig ( ) + that . contents . substring ( that . range [ 1 ] ) ;
93
98
}
94
99
95
- fs . outputFile ( filePath , contents , callback ) ;
100
+ fs . outputFileSync ( filePath , contents ) ;
96
101
} ;
97
102
98
103
/**
0 commit comments