简体   繁体   中英

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:

  • mkdir testProj : Creates new project folder
  • code testProj --add : Adds folder to workspace
  • cd testProj
  • npm init : Creates package.json
  • npm install [--save-dev] : Installs dependencies
  • ./node_modules/.bin/ init : What does this do, specifically?
    • Short: npx init

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM