Thursday, 11 May 2017

How to get path to my npm repo from scripts when installed globally

For my project, I have created a git on github containing some project related tools. The git contains a package.json and some binaries mapped to bin directives. While being a private repo and not published to any actual NPM repo server, it can be installed using

$ npm install -g git+ssh://git@github.com:<path to my tool git>

Most of the tools don't have any dependencies to other files in the module, and they work just fine. But one of the tools is a wrapper to start a local DynamoDB server, and my package.json contains a section like this:

"bin": {
    "ddb-local": "db/ddb_local.sh"
}

My db/db_local.sh file contains the java command to start DynamoDB:

#/bin/bash
java -Djava.library.path=DynamoDB/DynamoDBLocal_lib -jar DynamoDB/DynamoDBLocal.jar -inMemory

The DynamoDB folder is in the db folder which is in the root of my module:

<module root>
 |
 |- db
    |- DynamoDB
       |- DynamoDBLocal.jar
       |- DynamoDBLocal_lib
    |  ddb_local.sh
 |- package.json

After installation, ddb-local is linked from my node bin folder and can be invoked from anywhere, however the ddb_local.sh script will fail (unless the <path to my module>/db folder happens to be my current directory), since it tries to find files relative to the script, and the script will, in my case using NVM, be run from something like ~/.nvm/versions/node/v4.6.1/bin/

Now, if this was a node script, I would be able to require files in my own module easily, but since it is a bash script I am not sure what the best way is to produce a valid path to files in my module directory.

I know that my package is installed under ~/.nvm/versions/node/v4.6.1/lib/node_modules/<my module> and while I can create a relative path to my module's files using something like

`dirname "$0"`/../lib/node_modules/<module name>

this doesn't really seem to make sense. Surely there's an easy way to reference other scripts or files in your module from a script being globally installed as a binary? I have printed all environment variables etc, looking for something containing the path of my module, but with no luck.



via JHH

No comments:

Post a Comment