Skip to content

Instantly share code, notes, and snippets.

@ForgottenProgramme
Created February 14, 2021 11:14
Show Gist options
  • Save ForgottenProgramme/3e1003861ed071969e56fe7ea1c9e8e8 to your computer and use it in GitHub Desktop.
Save ForgottenProgramme/3e1003861ed071969e56fe7ea1c9e8e8 to your computer and use it in GitHub Desktop.
/*
Sample JS implementation of Todo CLI that you can attempt to port:
https://gist.github.com/jasim/99c7b54431c64c0502cfe6f677512a87
*/
/* Returns date with the format: 2021-02-04 */
let getToday: unit => string = %raw(`
function() {
let date = new Date();
return new Date(date.getTime() - (date.getTimezoneOffset() * 60000))
.toISOString()
.split("T")[0];
}
`)
type fsConfig = {encoding: string, flag: string}
/* https://nodejs.org/api/fs.html#fs_fs_existssync_path */
@bs.module("fs") external existsSync: string => bool = "existsSync"
/* https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options */
@bs.module("fs")
external readFileSync: (string, fsConfig) => string = "readFileSync"
/* https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options */
@bs.module("fs")
external appendFileSync: (string, string, fsConfig) => unit = "appendFileSync"
@bs.module("fs")
external writeFileSync: (string, string, fsConfig) => unit = "writeFileSync"
/* https://nodejs.org/api/os.html#os_os_eol */
@bs.module("os") external eol: string = "EOL"
let encoding = "utf8"
/*
NOTE: The code below is provided just to show you how to use the
date and file functions defined above. Remove it to begin your implementation.
*/
Js.log("Hello! today is " ++ getToday())
if existsSync("todo.txt") {
Js.log("Todo file exists.")
} else {
writeFileSync("todo.txt", "This is todo!" ++ eol, {encoding: encoding, flag: "w"})
Js.log("Todo file created.")
}
let TODO_FILE = 'todo.txt'
let DONE_FILE = 'done.txt'
let HELP_TEXT = `Usage :-
$ ./todo add "todo item" # Add a new todo
$ ./todo ls # Show remaining todos
$ ./todo del NUMBER # Delete a todo
$ ./todo done NUMBER # Complete a todo
$ ./todo help # Show usage
$ ./todo report # Statistics`
type command =
| Help
| Ls
| Add(option<string>)
| Del(option<int>)
| Done(option<int>)
| Report
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment