Skip to content
This repository was archived by the owner on Jan 6, 2021. It is now read-only.

Commit 16ae042

Browse files
committed
Fixed sequence of commands execution
1 parent 0310a8d commit 16ae042

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

app.js

+39-16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ var log = require("./logging");
88
var cfg_dir = __dirname + '/config/';
99
var cfg_map = {};
1010

11+
//Format function, move somewhere
12+
String.prototype.fmt = function(hash) {
13+
var s = this.toString();
14+
if(typeof hash == 'object') {
15+
for(var k in hash){
16+
s = s.split('{' + k + '}').join(hash[k]);
17+
}
18+
}
19+
return s;
20+
};
21+
1122
var processFile = function(fileName){
1223
var filePath = cfg_dir + fileName;
1324
var fileExt = path.extname(fileName);
@@ -118,23 +129,35 @@ http.createServer(function(request, response) {
118129
}
119130

120131
if(cfg.commands.length){
121-
for(var i in cfg.commands){
122-
var commandArray = cfg.commands[i];
123-
var commandString = commandArray.join(' ');
124-
var result = child_process.spawn(commandArray.shift(), commandArray, spawn_options);
132+
var executed = [];
133+
134+
var processCommand = function(cmd) {
135+
var commandString = cmd.join(' ');
136+
var result = child_process.spawn(cmd.shift(), cmd, spawn_options);
125137

126-
result.stdout.on('data', (function(c){
127-
return function (data) {
128-
log('Data from "' + c + '": ' + data, request.url + '.info');
129-
};
130-
})(commandString));
131-
132-
result.stderr.on('data', (function(c){
133-
return function (data) {
134-
log('Error in "' + c + '": ' + data, request.url + '.error');
135-
};
136-
})(commandString));
137-
}
138+
result.stdout.on('data', resultCallback(commandString, 'Data from "{command}": {data}', request.url + '.info'));
139+
result.stderr.on('data', resultCallback(commandString, 'Error in "{command}": {data}', request.url + '.error'));
140+
};
141+
142+
var resultCallback = function(cmd_string, format, file) {
143+
return function(data) {
144+
executed.push(cmd_string);
145+
146+
log(format.fmt({
147+
command: cmd_string,
148+
data: data
149+
}));
150+
151+
var next = cfg.commands.shift();
152+
if(next && executed.indexOf(next.join(' ')) == -1) {
153+
processCommand(next);
154+
}
155+
};
156+
};
157+
158+
processCommand(cfg.commands.shift());
159+
} else {
160+
return log('No commands to execute.', request.url + '.info');
138161
}
139162
});
140163

0 commit comments

Comments
 (0)