syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

Question:
How to use ShellJS?

I use Lineman to build websites.

It is a framework technology which depends on Node.js

An important component of Lineman is Grunt which offers a script driven administrative interface.

When I work with Grunt I often enhance it with a module named ShellJS.

ShellJS allows me to write Grunt tasks which can call shell commands.

Assuming that I have followed this post lineman_angular_install , to start a Lineman-Angular app on my laptop, an example of a shell script I call from Grunt is displayed below:
#!/bin/bash

# haml.bash

# This script should run haml against all haml files in app/haml_templates/

cd app/haml_templates/
for hfile in *.haml
do
  tfile=`echo $hfile | sed '1s/.haml$/.html/'`
  haml $hfile ../templates/${tfile}
done

exit
The above script transforms Haml files into HTML files.

It assumes the Haml files are in app/haml_templates and that the HTML files are in app/templates.

To call the above shell-script, I add syntax to a file in the tasks folder of my lineman app:
# tasks/haml_bash.coffee
module.exports = (grunt) ->
  grunt.registerTask "haml_bash", "Run haml.bash", (target) ->
    shell = require 'shelljs'
    # synchronously run shell script
    if (shell.exec 'bash haml.bash').code != 0
      shell.echo 'Error: haml.bash failed'
      shell.exit 1
    console.log 'haml_bash done now'
The above script depends on ShellJS which I install with this shell command:
npm install --save-dev shelljs
Also I add syntax to config/application.coffee:

module.exports = (lineman) ->
  app = lineman.config.application
  {
    appendTasks: common: app.appendTasks.common.concat('haml_bash')
    server: pushState: true
  }
The above syntax ensures that Grunt will call haml.bash at appropriate times as I enhance my Lineman app.

A simple post which describes installation of Haml is listed below:

ruby_gem_home_gem_path

I learned how to use Haml by studying this content:

http://haml.info/docs.html

I learned how to write syntax for tasks/haml_bash.coffee by studying this content:

http://gruntjs.com/creating-tasks

https://github.com/arturadib/shelljs#coffeescript

I learned how to write syntax for config/application.coffee by studying this content:

http://linemanjs.com/#configuring-lineman

When I mix Node.js , Lineman , Grunt , CoffeeScript , Haml , and ShellJS , I am happy with the efficiency of my development environment.


syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me