Skip to content

Commit cc2c2b9

Browse files
committed
implement a synced version instead of an async. fixes #1
1 parent 27593f0 commit cc2c2b9

File tree

1 file changed

+47
-42
lines changed

1 file changed

+47
-42
lines changed

index.js

+47-42
Original file line numberDiff line numberDiff line change
@@ -25,59 +25,64 @@ exports.ConfigFile = function(filePath) {
2525

2626
this.contents = null;
2727

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);
3733

3834
that.contents = data.toString();
39-
var program;
4035

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;
4541
}
42+
}
4643

47-
that.type = 'empty';
48-
if (program.type === 'Program') {
49-
program.body.forEach(function(statement) {
44+
var program;
5045

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) {
5355

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);
5769
return false;
5870
}
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+
});
7272

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;
7883
};
7984

80-
this.write = function(callback) {
85+
this.write = function() {
8186
var contents;
8287

8388
if (this.type === 'empty' || this.type === 'create-if-not-exists') {
@@ -92,7 +97,7 @@ exports.ConfigFile = function(filePath) {
9297
contents = that.contents.substring(0, that.range[0]) + that.buildConfig() + that.contents.substring(that.range[1]);
9398
}
9499

95-
fs.outputFile(filePath, contents, callback);
100+
fs.outputFileSync(filePath, contents);
96101
};
97102

98103
/**

0 commit comments

Comments
 (0)