Coffee script, how do I debug that damn js line?
Update April 12th, 2012: Pull request adding error reporting to CoffeeScript with line mapping
We’ll call our script
coffeep, an enhance version of
coffee -p myfile.coffee would do, but with each line prefixed by its line number. Calling
coffeep myfile.coffee 20 will print the line 20 as well as the 5 preceding lines and the 5 following lines. Calling
coffeep myfile.coffee 20 30 will print the line 20 to 30.
This is certainly not rocket science but shall become part of your coffee swiss army knife. Leveraging
cat -n which given some content passed through stdin prefix each line with its line number starting at 1, our first version of coffeep looks like:
#!/bin/bash coffee -p $1 | cat -n
If you are not familiar with bash scripting,
$1 stands for the first argument passed to
coffeep, which in our case is our coffee file.
#!/bin/bash if [ $# -eq 1 ] then coffee -p $1 | cat -n elif [ $# -eq 2 ]; then head_count=$(($2+5)) tail_count=11 coffee -p $1 | cat -n | head -$head_count | tail -$tail_count elif [ $# -eq 3 ]; then head_count=$(($2+$3)) tail_count=$3 coffee -p $1 | cat -n | head -$head_count | tail -$tail_count fi
With some little arithmetic, we make use of
tail to first limit the output to
head_count first lines with
head, and finally limit the output to
tail_count last lines with
Finally, for convenience, just copy/paste the following code into your terminal and it shall create an executable file named “coffeep” in “/usr/local/bin” (accessible from your
$PATH, make sure to have the permission to modify this directory):
cat > /usr/local/bin/coffeep <<DELIM #!/bin/bash if [ $# -eq 1 ] then coffee -p $1 | cat -n elif [ $# -eq 2 ]; then head_count=$(($2+5)) tail_count=11 coffee -p $1 | cat -n | head -$head_count | tail -$tail_count elif [ $# -eq 3 ]; then head_count=$(($2+$3)) tail_count=$3 coffee -p $1 | cat -n | head -$head_count | tail -$tail_count fi DELIM chmod +x /usr/local/bin/coffeep