This repository was archived by the owner on Feb 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
bluebird-promisell version #29
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const Promise = require('bluebird'); | ||
const P = require('bluebird-promisell'); | ||
const R = require('ramda'); | ||
const S = require('sanctuary'); | ||
|
||
// data Text = Buffer | String | ||
// readFile :: String -> String -> Promise Error Text | ||
const readFile = R.curry((encoding, filename) => | ||
new Promise((resolve, reject) => { | ||
fs.readFile(filename, {encoding: encoding}, (err, data) => { | ||
if (err != null) { | ||
reject(err); | ||
} else { | ||
resolve(data); | ||
} | ||
}); | ||
}) | ||
); | ||
|
||
// readFileWithDirAndName :: String -> String -> String -> Promise Error String | ||
const readFileWithDirAndName = R.curry((encoding, dir, name) => { | ||
const filePath = path.join(dir, name); | ||
return readFile(encoding)(filePath); | ||
}); | ||
|
||
// readFilesWithDirAndNames :: String -> [String] -> Promise Error [String] | ||
const readFilesWithDirAndNames = R.compose(P.traversep, readFileWithDirAndName); | ||
|
||
// concatFiles :: String -> Promise Error String | ||
const concatFiles = dir => { | ||
const indexFileP = readFileWithDirAndName('utf8', dir, 'index.txt'); | ||
const fileNamesP = P.liftp1(S.lines)(indexFileP); | ||
const readFilesWithNames = readFilesWithDirAndNames('utf8', dir); | ||
const filesP = P.liftp1(readFilesWithNames)(fileNamesP); | ||
return P.liftp1(R.join(''))(filesP); | ||
}; | ||
|
||
|
||
const main = () => { | ||
concatFiles(process.argv[2]) | ||
.then(data => { | ||
process.stdout.write(data); | ||
process.exit(0); | ||
}, | ||
err => { | ||
process.stderr.write(String(err) + '\n'); | ||
process.exit(1); | ||
}); | ||
}; | ||
|
||
if (process.mainModule.filename === __filename) main(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's inconsistent for
readFileWithDirAndName
to specify the encoding whilereadFile
requires the caller to provide it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I get it correctly. Are you suggestion making the encoding as an option, like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking that if
readFile
takesencoding
as an argument, so shouldreadFileWithDirAndName
. Conversely, ifreadFileWithDirAndName
does not takeencoding
as an argument, neither shouldreadFile
. It seems to me these two functions should be consistent in this respect unless there's a reason for them not to be.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Yeah, it makes sense. 👍
Updated!