Building a Writer
$web-only$
Thankfully because of the existence of resw
completing the console.log
debugging tool is going to be trivial. The primary entry point for resw
is the Writer
struct, which has a method write_part
that will take a &mut self
and &ProgramPart
, so we can use that in our for loop to write out the parts as they are parsed. That might look like this.
}; fn main() { let mut args = ::std::env::args(); let _ = args.next(); let file_name = args .next() .unwrap_or(String::from("./examples/insert_logging.js")); let js = read_to_string(file_name).expect("Unable to find js file");
With that complete we can see how well it works for us. Let's use the following example JavaScript.
function Thing(stuff) {
this.stuff = stuff;
}
let x = new Thing('argument');
Just as a simple test we could enter the following into our terminal
$ echo "function Thing(stuff) {
this.stuff = stuff;
}
let x = new Thing('argument');
" | console_logify
function Thing(stuff) {
console.log('Thing', stuff);
this.stuff = stuff;
}
let x = new Thing('argument!');
That looks exactly like the output we were looking for. Let's double check that it will behave as expected by piping the output to node
$ echo "function Thing(stuff) {
this.stuff = stuff;
}
let x = new Thing('argument');
" | console_logify | node -
Thing argument
It worked!
$web-only-end$ $slides-only$
Demo
$slides-only-end$