forked from plaid/deprecated-async-problem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tasks.js
51 lines (42 loc) · 1.39 KB
/
tasks.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 fs = require('fs');
const path = require('path');
const Task = require('data.task');
const R = require('ramda');
const S = require('sanctuary');
// join :: String -> String -> String
const join = R.curryN(2, path.join);
// data Text = Buffer | String
// readFile :: String -> String -> Task Error Text
const readFile = R.curry((encoding, filename) =>
new Task((rej, res) => {
fs.readFile(filename, {encoding: encoding}, (err, data) => {
if (err != null) {
rej(err);
} else {
res(data);
}
});
})
);
// concatFiles :: String -> Task Error String
const concatFiles = dir =>
S.pipe([readFile('utf8'), // :: Task Error String
R.map(S.lines), // :: Task Error [String]
R.map(R.map(join(dir))), // :: Task Error [String]
R.map(R.map(readFile('utf8'))), // :: Task Error [Task Error String]
R.chain(R.sequence(Task.of)), // :: Task Error [String]
R.map(R.join(''))], // :: Task Error String
join(dir, 'index.txt'));
const main = () => {
concatFiles(process.argv[2])
.fork(err => {
process.stderr.write(String(err) + '\n');
process.exit(1);
},
data => {
process.stdout.write(data);
process.exit(0);
});
};
if (process.mainModule.filename === __filename) main();