forked from slavaGanzin/async-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazy-either.js
46 lines (37 loc) · 1.14 KB
/
lazy-either.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
'use strict';
const fs = require('fs');
const path = require('path');
const LazyEither = require('lazy-either');
const R = require('ramda');
const S = require('sanctuary');
// join :: String -> String -> String
const join = R.curryN(2, path.join);
// readFile :: String -> String -> LazyEither Error String
const readFile = R.curry((encoding, filename) =>
new LazyEither(resolve => {
fs.readFile(
filename,
{encoding: encoding},
(err, data) => resolve(err != null ? S.Left(err) : S.Right(data))
);
}));
// concatFiles :: String -> LazyEither Error String
const concatFiles = dir =>
S.pipe([readFile('utf8'),
R.map(S.lines),
R.map(R.map(join(dir))),
R.chain(R.traverse(LazyEither.of, readFile('utf8'))),
R.map(R.join(''))],
join(dir, 'index.txt'))
const main = () => {
concatFiles(process.argv[2]).value(either => {
if (either.isRight) {
process.stdout.write(either.value);
process.exit(0);
} else {
process.stderr.write(String(either.value) + '\n');
process.exit(1);
}
});
};
if (process.mainModule.filename === __filename) main();