Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 65 additions & 40 deletions bin/transcribe
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -17,6 +18,7 @@ const {
alt,
append,
array,
chain,
compose: B,
flip,
fromMaybe,
Expand All @@ -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))
);
Comment on lines +55 to +58
Copy link
Collaborator

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. :)


// replace :: (String | RegExp) -> String -> String -> String
const replace = patt => repl => s => s.replace (patt, repl);

Expand Down Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably unnest some of this by extracting pieces:

Suggested change
// 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))
);
// overLines :: (Array String -> Array String) -> String -> String
const overLines = promap (lines) (unlines);
// parseFile :: Options -> String -> Future Error String
const parseFile = options => filename => {
const parse = parseLine = (options) (filename);
const reducer = line => pair (num => B (Pair (num + 1))
(append (parse (num) (line))));
const parseLines = B (snd) (reduce (flip (reducer)) (Pair (1) ([])));
return T (readFile (filename)) (map (overLines (parseLines)));
};


// 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)
Expand Down Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
(e => { console.error (String (e)); process.exit (1); })
(_ => { process.exit (0); })
(e => { console.error (e.stack); process.exit (1); })
(_ => { process.exit (0); })

(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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
(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)));
(T (transcribe (options) (program.args))
(chain (output =>
program.insertInto == null ?
writeStdOut (output) :
chain (writeFile (program.insertInto))
(map (replace (delimiters) ('$1\n\n' + output + '\n$2'))
(readFile (program.insertInto))))));

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"commander": "2.8.x",
"fluture": "14.0.0",
"sanctuary": "3.1.0"
},
"devDependencies": {
Expand Down