Magnus
Magnus

Reputation: 7839

What does the init command do?

Several Node.js packages have the following two steps as their starting point (just using Jasmine as an example):

npm install --save-dev jasmine
./node_modules/.bin/jasmine init

The first statement is straightforward, but I could not for the life of me figure out what the second statement does under the hood. The Jasmine docs only say that it initializes it (I am searching for something more technical).

./node_modules/.bin/jasmine looks like this:

#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")

case `uname` in
    *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac

if [ -x "$basedir/node" ]; then
  "$basedir/node"  "$basedir/../jasmine/bin/jasmine.js" "$@"
  ret=$?
else 
  node  "$basedir/../jasmine/bin/jasmine.js" "$@"
  ret=$?
fi
exit $ret

In case helpful, I did this to clone and install the package locally:

Any pointers/documentation explaining that init would be deeply appreciated.

Edit:

Just to clarify, I know what init does (clear from testing and the Jasmine documentation), I just do not understand how it does it. I am basically trying to find out why init is needed behind the script name when running it from the CLI, and where the init code is located.

Upvotes: 0

Views: 898

Answers (2)

Magnus
Magnus

Reputation: 7839

I managed to finally solve this myself. If anyone comes across this in the future, the following is the explanation of ./node_modules/.bin/jasmine init.

  1. ./node_modules/.bin/jasmine init is executed from the command line
  2. This runs the jasmine Unix script in ./node_modules/.bin/ (init argument is not used yet)
  3. The script resolves path to jasmine.js (./node_modules/jasmine/bin/jasmine.js) and runs it
  4. Jasmine.js contains this code: var Command = require('../lib/command.js')
  5. Jasmine.js creates a new instance of the Command object (command) and executes: command.run(jasmine, process.argv.slice(2));
    • process.argv is an array of all arguments given when starting the application from command line. Recalling the command, one sees that slice(2) equals init
  6. The run function inside command.js launches initJasmine by having mapped init to initJasmine at the very top
  7. Finally, initJasmine makes the directory spec and all its contents

Hope that helps someone else in the future.

Upvotes: 1

Alexey Kucherenko
Alexey Kucherenko

Reputation: 904

jasmine init initializes a basic folder structure for Jasmine by creating a spec directory and configuration json for you. You can look for more details here https://github.com/jasmine/jasmine-npm

Upvotes: 0

Related Questions