11

I'm wondering if there are any node framework or node lib out there that I can use to write a shell scripts? For example, I have bash shell program to install Graphite and OpenTSDB RRD tools, I would like to use node.js for it, is it possible?

Thanks

3 Answers 3

11

Take a look at shelljs - it implemets shell and gnu coreutils-like functions. Together with coffeescript it can look very similar to a shell script:

if not which 'git'
  echo 'Sorry, this script requires git'
  exit 1

# Copy files to release dir
mkdir '-p', 'out/Release'
cp '-R', 'stuff/*', 'out/Release'

# Replace macros in each .js file
cd 'lib'
for file in ls '*.js'
  sed '-i', 'BUILD_VERSION', 'v0.1.2', file
  sed '-i', /.*REMOVE_THIS_LINE.*\n/, '', file
  sed '-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, cat 'macro.js', file
cd '..'

# Run external tool synchronously
if (exec 'git commit -am "Auto-commit"').code != 0
  echo 'Error: Git commit failed'
  exit 1
Sign up to request clarification or add additional context in comments.

1 Comment

thanks @Andrey Sidorov, look very promising, I'm going to try out this lib.
3

You should check out grunt which is a toolkit for helping people write build and other scripts in node.js. There are a ton of plugins to help you easily do interesting things.

That being said, if you know Bash, I'd just stick with bash.

Check out this interesting thread on Twitter about Bash vs. Grunt scripting

What I Use Grunt For

  • Running Lint Tools
  • Running JS Unit Tests
  • Running Pre-Processors (sass, require.js, uglify, etc.)

What I use Capistrano* For

  • Deploying code to production environments

What I use Bash** for

  • Setting up servers and running them, etc.

  • * capistrano + git or chef or whatever
  • ** bash or whatever other tools you'd want to use

3 Comments

would you please explain more why you would stick with bash if I already know Bash? Is there any advantages of using bash over grunt?
1. Executing binary programs on your server is significantly harder in asynchronous javascript that it is in bash. 2. Grunt/node.js are still pre 1.0 and have changing APIs (grunt more so than node) 3. Bash is a general purpose tool available on many systems whereas node.js and grunt are not likely to be installed on many systems.
Docker docker.com cuts a lot of cruft out of all of this though. (** and *)
2

There are many, among the most popular there's commander

npm install --save commander

Then writing the command is pretty easy:

#!/usr/bin/env node

var program = require('commander');

program
  .version('0.0.1')
  .option('-f, --foo', 'enable some foo')
  .option('-b, --bar', 'enable some bar')
  .option('-B, --baz', 'enable some baz');

program.on('--foo', function(){
  console.log('Stuff!');
});

1 Comment

This is not quite I'm looking for. I'm looking for a node framework that can help me to write shell script. For example: I have this script written in bash github.com/gdbtek/setup-graphite/blob/master/ubuntu.bash and I would like to make this in node. If it is too much using Node.js, I think it would be better for me to stay in bash scripting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.