|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +const most = require('most'); |
| 7 | +const R = require('ramda'); |
| 8 | +const S = require('sanctuary'); |
| 9 | + |
| 10 | + |
| 11 | +// join :: String -> String -> String |
| 12 | +const join = R.curryN(2, path.join); |
| 13 | + |
| 14 | +// readFile :: String -> String -> Promise Error String |
| 15 | +const readFile = R.curry((encoding, filename) => |
| 16 | + new Promise((resolve, reject) => |
| 17 | + fs.readFile(filename, { encoding: encoding }, (err, data) => |
| 18 | + err != null ? reject(err) : resolve(data) |
| 19 | + ))); |
| 20 | + |
| 21 | +// concatFiles :: String -> Promise Error String |
| 22 | +const concatFiles = dir => |
| 23 | + R.pipe(R.pipe(readFile('utf8'), most.fromPromise), |
| 24 | + R.map(S.lines), |
| 25 | + R.map(R.map(join(dir))), |
| 26 | + R.chain(R.pipe(R.map(readFile('utf8')), most.from)), |
| 27 | + stream => stream.await(), |
| 28 | + R.reduce(R.concat, '') |
| 29 | + )(join(dir, 'index.txt')); |
| 30 | + |
| 31 | + |
| 32 | +const main = () => { |
| 33 | + concatFiles(process.argv[2]).then(value => { |
| 34 | + process.stdout.write(value); |
| 35 | + process.exit(0); |
| 36 | + }, err => { |
| 37 | + process.stderr.write(String(err) + '\n'); |
| 38 | + process.exit(1); |
| 39 | + }); |
| 40 | +}; |
| 41 | + |
| 42 | +if (process.mainModule.filename === __filename) main(); |
0 commit comments