-
Notifications
You must be signed in to change notification settings - Fork 6
use Fluture #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
use Fluture #36
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -5,7 +5,8 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||
const fs = require ('fs'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
const program = require ('commander'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
const {create, env} = require ('sanctuary'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
const Future = require ('fluture'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
const sanctuary = require ('sanctuary'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
const pkg = require ('../package.json'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -17,6 +18,7 @@ const { | |||||||||||||||||||||||||||||||||||||||||||||||||||
alt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
append, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
array, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
chain, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
compose: B, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
flip, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
fromMaybe, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -26,18 +28,35 @@ const { | |||||||||||||||||||||||||||||||||||||||||||||||||||
map, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
pair, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
pipe, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
promap, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
reduce, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
snd, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
splitOn, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
stripPrefix, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
traverse, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
unfoldr, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
unlines, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} = create ({checkTypes: false, env}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} = sanctuary.unchecked; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
const map2 = B (map) (map); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
const map3 = B (map) (map2); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
const map4 = B (map) (map3); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
// readFile :: String -> Future Error String | ||||||||||||||||||||||||||||||||||||||||||||||||||||
const readFile = path => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Future.node (callback => fs.readFile (path, 'utf8', callback)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
// writeFile :: String -> String -> Future Error Undefined | ||||||||||||||||||||||||||||||||||||||||||||||||||||
const writeFile = path => data => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Future.node (callback => fs.writeFile (path, data, callback)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
// writeStdOut :: String -> Future Error Undefined | ||||||||||||||||||||||||||||||||||||||||||||||||||||
const writeStdOut = data => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Future.node (callback => process.stdout.write (data, 'utf8', callback)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
// replace :: (String | RegExp) -> String -> String -> String | ||||||||||||||||||||||||||||||||||||||||||||||||||||
const replace = patt => repl => s => s.replace (patt, repl); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -89,25 +108,29 @@ const parseLine = options => filename => num => pipe ([ | |||||||||||||||||||||||||||||||||||||||||||||||||||
fromMaybe (''), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
// parseFile :: Options -> String -> String | ||||||||||||||||||||||||||||||||||||||||||||||||||||
const parseFile = options => filename => | ||||||||||||||||||||||||||||||||||||||||||||||||||||
unlines (snd (reduce (flip (line => | ||||||||||||||||||||||||||||||||||||||||||||||||||||
pair (num => B (Pair (num + 1)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(append (parseLine (options) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(filename) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(num) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(line)))))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(Pair (1) ([])) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(lines (fs.readFileSync (filename, 'utf8'))))); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
// transcribe :: Options -> Array String -> String | ||||||||||||||||||||||||||||||||||||||||||||||||||||
const transcribe = options => pipe ([ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
map (parseFile (options)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
joinWith ('\n\n'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
replace (/\n{3,}/g) ('\n\n'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
replace (/^\n+/) (''), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
replace (/\n+$/) ('\n'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
// parseFile :: Options -> String -> Future Error String | ||||||||||||||||||||||||||||||||||||||||||||||||||||
const parseFile = options => filename => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
map (promap (lines) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(unlines) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(B (snd) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(reduce (flip (line => | ||||||||||||||||||||||||||||||||||||||||||||||||||||
pair (num => B (Pair (num + 1)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(append (parseLine (options) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(filename) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(num) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(line)))))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(Pair (1) ([]))))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(readFile (filename)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+111
to
+124
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd probably unnest some of this by extracting pieces:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
// transcribe :: Options -> Array String -> Future Error String | ||||||||||||||||||||||||||||||||||||||||||||||||||||
const transcribe = options => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
B (map (pipe ([joinWith ('\n\n'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
replace (/\n{3,}/g) ('\n\n'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
replace (/^\n+/) (''), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
replace (/\n+$/) ('\n')]))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(traverse (Future) (parseFile (options))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
program | ||||||||||||||||||||||||||||||||||||||||||||||||||||
.version (pkg.version) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -140,22 +163,24 @@ if (!valid) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
// defaultTo :: a -> a? -> a | ||||||||||||||||||||||||||||||||||||||||||||||||||||
const defaultTo = x => y => y == null ? x : y; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
// output :: String | ||||||||||||||||||||||||||||||||||||||||||||||||||||
const output = | ||||||||||||||||||||||||||||||||||||||||||||||||||||
transcribe ({headingLevel: Number (defaultTo ('3') (program.headingLevel)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
headingPrefix: defaultTo ('//#') (program.headingPrefix), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
prefix: defaultTo ('//.') (program.prefix), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
url: program.url}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(program.args); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
if (program.insertInto == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
process.stdout.write (output); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
// Read the file, insert the output, and write to the file again | ||||||||||||||||||||||||||||||||||||||||||||||||||||
fs.writeFileSync ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
program.insertInto, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
replace (/(<!--transcribe-->)[\s\S]*?(<!--[/]transcribe-->)/) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
('$1\n\n' + output + '\n$2') | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(fs.readFileSync (program.insertInto, 'utf8')) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
// options :: Options | ||||||||||||||||||||||||||||||||||||||||||||||||||||
const options = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
headingLevel: Number (defaultTo ('3') (program.headingLevel)), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
headingPrefix: defaultTo ('//#') (program.headingPrefix), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
prefix: defaultTo ('//.') (program.prefix), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
url: program.url, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
// delimiters :: RegExp | ||||||||||||||||||||||||||||||||||||||||||||||||||||
const delimiters = /(<!--transcribe-->)[\s\S]*?(<!--[/]transcribe-->)/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
Future.fork | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(e => { console.error (String (e)); process.exit (1); }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(_ => { process.exit (0); }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+178
to
+179
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You know what the type of the thing your Future will reject with is, so you may not need the string cast.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
(chain (output => | ||||||||||||||||||||||||||||||||||||||||||||||||||||
program.insertInto == null ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||
writeStdOut (output) : | ||||||||||||||||||||||||||||||||||||||||||||||||||||
chain (writeFile (program.insertInto)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(map (replace (delimiters) ('$1\n\n' + output + '\n$2')) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(readFile (program.insertInto)))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
(transcribe (options) (program.args))); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+180
to
+186
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use thrush as a poor-man's pipe operator to make the data flow and indentation a bit more natural.
Suggested change
|
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.
Hey, I've known for a while that
process.stdout.write
is non-blocking, but I never knew it takes a callback. :)