English 中文(简体)
CoffeeScript - command-line utility
  • 时间:2024-12-22

CoffeeScript - Command-pne utipty


Previous Page Next Page  

On instalpng CoffeeScript on Node.js, we can access the coffee-command pne utipty. In here, the coffee command is the key command. Using various options of this command, we can compile and execute the CoffeeScript files.

You can see the pst of options of the coffee command using its -h or --help option. Open the Node.js command prompt and execute the following command in it.

c:>coffee -help

This command gives you the pst of various options of the coffee, along with the description of the operation performed by each of them as shown below.

coffeecommand Help

Compipng the CoffeeScript Code

The CoffeeScript files are saved with the extension .coffee. You can compile these files using the -c or --compile option of the coffee command as shown below.

c:>coffee -c filename.coffee

Example

Suppose there is a file in your system with the following CoffeeScript code which prints a message on the console.

name = "Raju"
console.log "Hello"+name+" Welcome to Tutorialspoint"

Note − The console.log() function prints the given string on the console.

To compile the above code, save it in a file with the name sample.coffee. Open the Node.js command prompt. Browse through the path where you have saved the file and compile it using the -c option of the coffee command of the coffee command-pne utipty as shown below.

c:> coffee -c sample.coffee

On executing the above command, the CoffeeScript compiler compiles the given file (sample.coffee) and saves it in the current location with a name sample.js as shown below.

JS File

If you open the sample.js file, you can observe the generated JavaScript as shown below.

// Generated by CoffeeScript 1.10.0
(function() {
  var name;
  name = "Raju";
  console.log("Hello " + name + " Welcome to Tutorialspoint");

}).call(this);

Executing the CoffeeScript code

You can execute a CoffeeScript file by simply passing the file name to the coffee command in the Node.js command prompt as follows.

c:> coffee sample.coffee

Example

For example, let us execute the sample.coffee file. For this, open the Node.js command prompt. Browse through the path where you have saved the file and execute the file by directly passing its name to the coffee command as shown below.

Execution

Watch and Compile

In some scenarios, there is a chance that we do a lot of changes to our scripts. Using the –w option of the coffee command, you watch your scripts for changes.

You can watch and compile a file simultaneously using the -wc option as shown below. When we use this option, the file will be recompiled each time you make changes in your script.

c:>coffee -wc file_name

Example

Suppose we have compiled a file named sample.coffee using the -wc option and we modified the script thrice. Each time we change the script, the .coffee file is recompiled leaving the Node.js command prompt as shown below.

Watch and compile

Setting the Output Directory

Using the -o option, we can set the output directory to place the compiled JavaScript files as shown below.

c:>coffee -o "Required path where we want our .js files" file_name

Example

Let us save the JavaScript code of the sample.coffee file in a folder named data in the E drive using the -o option by executing the following command in the command prompt.

c:>coffee -o E://data sample.coffee

Following is the snapshot of the given folder after executing the above command. Here you can observe the JavaScript file of the sample.coffee

Output Directory

Print the Compiled JavaScript

If we want to print the compiled javascript on the console itself, we have to use the -p option of the coffee command as shown below.

c:>coffee -p file_name

Example

For example, you can print the compiled JavaScript code of the sample.coffee file on the console using the -p option as shown below.

Print JavaScript

The REPL (Read Evaluate Print Loop)

CoffeeScript provides you an REPL-interactive shell. This shell is used to evaluate the CoffeeScript expressions. You can type any CoffeeScript code in this shell and get the result immediately. You can open REPL by executing the coffee command without any options as shown below.

Execute Coffee command

Using this shell, we can assign values to variables, create functions, and evaluate results. As shown in the following screenshot, if we call functions in REPL, it prints the value of the function. If we give an expression to it, it evaluates and prints the result of the expression. And if we simply type the statements in it, it prints the value of the last statement.

REPL Usage

In REPL, you can access multiple pne mode by pressing ctrl+v where you can evaluate the code with multiple pnes (pke functions) and you can get back to REPL mode from it by pressing ctrl+v again. Here is an example usage of the multi pne mode.

REPL Mutipne Function

Running CoffeeScript through Browser

We can run CoffeeScript using the <script> tag of the HTML just pke JavaScript as shown below.

<script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"
   type="text/javascript" charset="utf-8"></script>
<script type="text/coffeescript">
  # Some CoffeeScript
</script>

But for this, we have to import the pbrary in each apppcation and the CoffeeScript code will be interpreted pne by pne before the output is shown. This will slow down your apppcations, therefore this approach is not recommended.

Therefore, to use CoffeeScript in your apppcations, you need to pre-compile them using the Coffee command-pne utipty and then you can use the generated JavaScript in your apppcations.

Advertisements