forked from plaid/deprecated-async-problem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
most.js
42 lines (33 loc) · 1.1 KB
/
most.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
'use strict';
const fs = require('fs');
const path = require('path');
const most = require('most');
const R = require('ramda');
const S = require('sanctuary');
// join :: String -> String -> String
const join = R.curryN(2, path.join);
// readFile :: String -> String -> Promise Error String
const readFile = R.curry((encoding, filename) =>
new Promise((resolve, reject) =>
fs.readFile(filename, { encoding: encoding }, (err, data) =>
err != null ? reject(err) : resolve(data)
)));
// concatFiles :: String -> Promise Error String
const concatFiles = dir =>
R.pipe(R.pipe(readFile('utf8'), most.fromPromise),
R.map(S.lines),
R.map(R.map(join(dir))),
R.chain(R.pipe(R.map(readFile('utf8')), most.from)),
stream => stream.await(),
R.reduce(R.concat, '')
)(join(dir, 'index.txt'));
const main = () => {
concatFiles(process.argv[2]).then(value => {
process.stdout.write(value);
process.exit(0);
}, err => {
process.stderr.write(String(err) + '\n');
process.exit(1);
});
};
if (process.mainModule.filename === __filename) main();