-
Notifications
You must be signed in to change notification settings - Fork 0
/
compressor.js
53 lines (47 loc) · 1.32 KB
/
compressor.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
'use strict';
const path = require('path'),
fs = require('mz/fs'),
_ = require('lodash'),
cp = require('child_process'),
util = require('util')
const sourceDir = path.resolve(process.env.SOURCE_DIR);
const targetDir = path.resolve(process.env.TARGET_DIR);
const compress = (file) => {
const tarPath = file + '.tar.gz';
cp.execSync(util.format("tar -czf %s -C %s -T %s", tarPath, sourceDir, file));
return tarPath;
}
fs.readdir(targetDir).then(function(items) {
return _
.chain(items)
.filter(function(item) {
return item.match(/\.txt$/)
})
.map(function(item) {
return item.replace(/\..+$/, '')
})
.map(function(name) {
return path.join(targetDir, name + '.txt');
})
.value()
}).then(function(fileList) {
console.log("Files found:", fileList.length);
process.on('message', (msg) => {
console.log("Compressor received:", msg);
switch (msg.cmd) {
case 'request':
if (fileList.length === 0) {
console.log('Queue is empty');
} else {
const newFile = fileList.shift();
console.log("Picked:", newFile);
process.send({ cmd: 'new', id: msg.id, indexFile: newFile, file: compress(newFile) });
}
break;
case 'exit':
process.exit(0);
break;
}
});
process.send({ cmd: 'init' });
})