If you have a package.json at the base of the project any dependencies
installed in the node_modules folder will be resolved at any depth. You should
only need to do a single install at the base of hook. Do you have a copy of
the project I could take a peek at?
If the cordovaUserProject itself already has a package.json you should just add
the dependencies in there and they will automatically be added to the path when
required in by the scripts (and only need to be installed at the same time the
user does the initial bootstrap). If you were interested in abstracting these
scripts further it is possible to build node modules that expose binaries
through the ‘bin’ key in the package json. You would then be able to reference
these scripts directly in an npm script without specific paths, as they will be
installed into ./node_modules/.bin, which is shimmed into path of an npm
script.
This would allow you to add the following to the single package.json
scripts: {
bootstrap: ‘prepare’,
compile: ‘some bash script that compiles’,
post: ‘compile’,
build: ‘npm run bootstrap && npm run compile && npm run post’
}
then in your project (or any script) you could simply run ‘npm build’ and it
would all work!
Please let me know if any of that wasn’t clear. It is worth mentioning that
install scripts are generally considered an anti-pattern in the node community
(as it states in the docs https://www.npmjs.org/doc/misc/npm-scripts.html).
Clever concatenation of bash scripts is considered elegant though!
Best,
Myles Borins
On Aug 5, 2014, at 2:22 PM, Lorin Beer <[email protected]> wrote:
> a suggestion:
>
> as part of the hook script, have it attempt an npm install iff the
> dependencies are not present. You can hide the require calls in a
> try/catch, if caught, then run an install script in the relevant hook
> directories.
>
> If admin privileges are required for the install, prompt for credentials,
> or tell the user to change the directory permissions...
>
> Don't know about 'best practice', but it's light weight and doesn't force
> the user to execute an intermediate install step.
>
>
>
> On Tue, Aug 5, 2014 at 1:00 PM, Carlos Santana <[email protected]> wrote:
>
>> I'm writing new cordova hooks, and decided to do them in nodejs
>>
>> First problem I hit was dependencies for the hook scripts:
>> so far I have two scripts:
>> cordovaUserProject/hook/before_prepare/wl_b_prepare.js
>> cordovaUserProject/hook/after_compile/wl_a_compile.js
>>
>> I have both starting like this:
>> #!/usr/bin/env node
>>
>> var shell = require('shelljs'),
>> nopt = require('nopt');
>>
>> shell.echo('Running Worklight Cordova Hook');
>>
>>
>> I get errors because it can't find dependencies 'shelljs' and 'nopt'
>>
>> To resolve this I would need the user or another hook before this one to
>> install the node_modules in one of these places
>>
>> cordovaUserProject/node_modules/
>> cordovaUserProject/hook/node_modules/
>> cordovaUserProject/hook/before_prepare/node_modules/
>> cordovaUserProject/hook/after_compile/node_modules/
>>
>> What would be a best practice?
>> Who get's to install? (user or me)
>> Where to install?
>>
>>
>>
>> --
>> Carlos Santana
>> <[email protected]>
>>