forked from smooks/unedifact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
80 lines (73 loc) · 2.43 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* A maven parallelization tool for building UN/EDIFACT directories
*
* To start it please make sure you have a node.js installed
*
* %> node build.js [maven parameters]
*
* In case no maven parameters are specified, '-o clean install' will be used.
* Standard output of forked child processes is stored under log directory
*
**/
var util = require('util'),
path = require('path'),
sys = require('sys'),
fs = require('fs'),
spawn = require('child_process').spawn,
exec = require('child_process').exec,
async = require('async');
var cmdParams = process.argv.slice(2);
var MAVEN_PARAMETERS = cmdParams.length ? cmdParams : ['-o', 'clean', 'install'];
// Run maven
var mvn = function(cwd, cb) {
path.exists(cwd, function(exists) {
if (exists) {
sys.puts('Starting mvn ' + cwd);
var procName = cwd.match('/(d[0-9]+./[a-z]+)')[1].replace('/','_');
var logFile = process.cwd() + '/logs/' + procName + '.log';
var logStream = fs.createWriteStream(logFile);
var proc = spawn('mvn', MAVEN_PARAMETERS , {"cwd" : cwd });
proc.stdout.pipe(logStream);
proc.on('exit', function(code) {
sys.puts('Completed mvn ' + cwd);
cb(code);
});
} else {
sys.puts('Skipping run for ' + cwd);
cb(null);
}
});
}
// Sequential function that builds a single directory
var buildDir = function(directory, callback) {
var root = process.cwd() + '/' + directory,
start = new Date();
console.log('Building directory ' + directory);
var tasks = [ root + '/mapping', root + '/binding', root + '/test' ];
async.forEachSeries(tasks, mvn, function(err) {
if (err) {
sys.puts('ERROR Happened when processing directory ' + directory);
}
callback();
console.log('Build for :', root, 'took\t\t', Math.round((new Date() - start) / 1000), 's');
});
}
// Here the program starts
var dirs = [],
cpus = require('os').cpus().length - 1,
logDir = process.cwd() + '/logs';
sys.puts('Starting a build process with ' + cpus + ' parallel threads');
sys.puts('Logs are stored in ' + logDir);
console.log('Launching maven parametrers "%s"', MAVEN_PARAMETERS.join(' '));
exec('rm -rf ' + logDir, function(error) {
// Let's create a log directory
fs.mkdirSync(logDir, 0776);
// Let's create a queue with given number of concurent executions
var q = async.queue(buildDir, cpus);
// Now let's push all directories to the queue
fs.readdirSync(process.cwd()).forEach(function(file) {
if (file.match('d[0-9]')) {
q.push(file);
}
});
});