You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cordova.apache.org by Carlos Santana <cs...@gmail.com> on 2014/08/05 22:18:48 UTC

Can I have all my hooks in one file and be event driven?

I started to write hooks, and I got lazy :-)

What about having a single hook nodejs script that listen on cordova events?

My script will look something like this:

function worklightBeforePrepare(){
console.log('Running Worklight worklightBeforePrepare()');
}
function worklightAfterCompile(){
console.log('Running Worklight worklightAfterCompile()');
}
function worklightHooks(cordova_lib){
cordova_lib.events.on('before_prepare',worklightBeforePrepare);
cordova_lib.events.on('after_compile',worklightAfterCompile);
}
module.exports = {
run: worklightHooks
}


Then when cordova cli starts it always runs my hook script:

var script = path.join(root,'hook/global/worklight_cordova_hook.js');
var hook = require(script);
hook.run(cordova_lib);


-- 
Carlos Santana
<cs...@gmail.com>

Re: Can I have all my hooks in one file and be event driven?

Posted by Carlos Santana <cs...@gmail.com>.
yep that was my feeling write a node module with a simple API (i.e. run)
what ever and then listen on events coming from cordova.

I like the node_modules searching, but I think this is something that npm
does better
But there still the problem who is going to put the files down for
"node_modules"

There might be a simpler way to register the hook with cordova, like have
declare it as a devDependency in cordovaUserProject/package.json or
cordovaUserProject/cordova.json
devDependencies :{
  cdv-hook-worklight:0.1.x
}

then let "cordova install" -> npm install take care of putting down, and
cordova lib just looks in devDependencies for things that start with
cdv-hook-*

I created two threads on purpose 1. today, 2. future :-)



On Tue, Aug 5, 2014 at 4:31 PM, Brian LeRoux <b...@brian.io> wrote:

> yes! this
>
> and related to your earlier thread: maybe hooks themselves should just be
> npm modules
>
> The hook lookups could walk up node_modules and look for modules with a
> package.json that have a "cordova" key… maybe this should go to the other
> thread
>
>
> On Tue, Aug 5, 2014 at 1:18 PM, Carlos Santana <cs...@gmail.com>
> wrote:
>
> > I started to write hooks, and I got lazy :-)
> >
> > What about having a single hook nodejs script that listen on cordova
> > events?
> >
> > My script will look something like this:
> >
> > function worklightBeforePrepare(){
> > console.log('Running Worklight worklightBeforePrepare()');
> > }
> > function worklightAfterCompile(){
> > console.log('Running Worklight worklightAfterCompile()');
> > }
> > function worklightHooks(cordova_lib){
> > cordova_lib.events.on('before_prepare',worklightBeforePrepare);
> > cordova_lib.events.on('after_compile',worklightAfterCompile);
> > }
> > module.exports = {
> > run: worklightHooks
> > }
> >
> >
> > Then when cordova cli starts it always runs my hook script:
> >
> > var script = path.join(root,'hook/global/worklight_cordova_hook.js');
> > var hook = require(script);
> > hook.run(cordova_lib);
> >
> >
> > --
> > Carlos Santana
> > <cs...@gmail.com>
> >
>



-- 
Carlos Santana
<cs...@gmail.com>

RE: Can I have all my hooks in one file and be event driven?

Posted by Myles Borins <my...@famo.us>.
We use a module pattern at famous that allows a package to be both required
within a script file, and executed from the shell. This makes it super
simple to write everything in line in the scripts section of package.json
which is a very clean approach.

I'm going to look into open sourcing this seed today, and would love to
help out with this hooks project if it would be welcomed.
On Aug 6, 2014 9:44 AM, "Sergey Grebnov (Akvelon)" <v-...@microsoft.com>
wrote:

> Like this. This should also work faster since we don't need to do files
> lookup (ro parse config.xml), etc .
>
> PS. In new hooks implementation with plugin support you can use the same
> file for different hooks and use ctx.hook to see for what hook event  is
> fired.
>
> -Sergey
> -----Original Message-----
> From: brian.leroux@gmail.com [mailto:brian.leroux@gmail.com] On Behalf Of
> Brian LeRoux
> Sent: Wednesday, August 6, 2014 1:01 AM
> To: dev@cordova.apache.org
> Subject: Re: Can I have all my hooks in one file and be event driven?
>
> +1
>
>
> On Tue, Aug 5, 2014 at 1:52 PM, Michal Mocny <mm...@chromium.org> wrote:
>
> > I like the idea of hooks as events.
> >
> > Re: publishing to npm, I like to idea of hook publishing, but do we
> > need another "package type"?  Platforms, plugins, and now hooks would
> > all have different process/documentation.  Why not just add hooks
> > support for plugins (I commented as such on the other thread first).
> >
> > -Michal
> >
> >
> > On Tue, Aug 5, 2014 at 4:31 PM, Brian LeRoux <b...@brian.io> wrote:
> >
> > > yes! this
> > >
> > > and related to your earlier thread: maybe hooks themselves should
> > > just be npm modules
> > >
> > > The hook lookups could walk up node_modules and look for modules
> > > with a package.json that have a "cordova" key… maybe this should go
> > > to the other thread
> > >
> > >
> > > On Tue, Aug 5, 2014 at 1:18 PM, Carlos Santana
> > > <cs...@gmail.com>
> > > wrote:
> > >
> > > > I started to write hooks, and I got lazy :-)
> > > >
> > > > What about having a single hook nodejs script that listen on
> > > > cordova events?
> > > >
> > > > My script will look something like this:
> > > >
> > > > function worklightBeforePrepare(){ console.log('Running Worklight
> > > > worklightBeforePrepare()'); } function worklightAfterCompile(){
> > > > console.log('Running Worklight worklightAfterCompile()'); }
> > > > function worklightHooks(cordova_lib){
> > > > cordova_lib.events.on('before_prepare',worklightBeforePrepare);
> > > > cordova_lib.events.on('after_compile',worklightAfterCompile);
> > > > }
> > > > module.exports = {
> > > > run: worklightHooks
> > > > }
> > > >
> > > >
> > > > Then when cordova cli starts it always runs my hook script:
> > > >
> > > > var script =
> > > > path.join(root,'hook/global/worklight_cordova_hook.js');
> > > > var hook = require(script);
> > > > hook.run(cordova_lib);
> > > >
> > > >
> > > > --
> > > > Carlos Santana
> > > > <cs...@gmail.com>
> > > >
> > >
> >
>

RE: Can I have all my hooks in one file and be event driven?

Posted by "Sergey Grebnov (Akvelon)" <v-...@microsoft.com>.
Like this. This should also work faster since we don't need to do files lookup (ro parse config.xml), etc .

PS. In new hooks implementation with plugin support you can use the same file for different hooks and use ctx.hook to see for what hook event  is fired.

-Sergey
-----Original Message-----
From: brian.leroux@gmail.com [mailto:brian.leroux@gmail.com] On Behalf Of Brian LeRoux
Sent: Wednesday, August 6, 2014 1:01 AM
To: dev@cordova.apache.org
Subject: Re: Can I have all my hooks in one file and be event driven?

+1


On Tue, Aug 5, 2014 at 1:52 PM, Michal Mocny <mm...@chromium.org> wrote:

> I like the idea of hooks as events.
>
> Re: publishing to npm, I like to idea of hook publishing, but do we 
> need another "package type"?  Platforms, plugins, and now hooks would 
> all have different process/documentation.  Why not just add hooks 
> support for plugins (I commented as such on the other thread first).
>
> -Michal
>
>
> On Tue, Aug 5, 2014 at 4:31 PM, Brian LeRoux <b...@brian.io> wrote:
>
> > yes! this
> >
> > and related to your earlier thread: maybe hooks themselves should 
> > just be npm modules
> >
> > The hook lookups could walk up node_modules and look for modules 
> > with a package.json that have a "cordova" key… maybe this should go 
> > to the other thread
> >
> >
> > On Tue, Aug 5, 2014 at 1:18 PM, Carlos Santana 
> > <cs...@gmail.com>
> > wrote:
> >
> > > I started to write hooks, and I got lazy :-)
> > >
> > > What about having a single hook nodejs script that listen on 
> > > cordova events?
> > >
> > > My script will look something like this:
> > >
> > > function worklightBeforePrepare(){ console.log('Running Worklight 
> > > worklightBeforePrepare()'); } function worklightAfterCompile(){ 
> > > console.log('Running Worklight worklightAfterCompile()'); } 
> > > function worklightHooks(cordova_lib){ 
> > > cordova_lib.events.on('before_prepare',worklightBeforePrepare);
> > > cordova_lib.events.on('after_compile',worklightAfterCompile);
> > > }
> > > module.exports = {
> > > run: worklightHooks
> > > }
> > >
> > >
> > > Then when cordova cli starts it always runs my hook script:
> > >
> > > var script = 
> > > path.join(root,'hook/global/worklight_cordova_hook.js');
> > > var hook = require(script);
> > > hook.run(cordova_lib);
> > >
> > >
> > > --
> > > Carlos Santana
> > > <cs...@gmail.com>
> > >
> >
>

Re: Can I have all my hooks in one file and be event driven?

Posted by Brian LeRoux <b...@brian.io>.
+1


On Tue, Aug 5, 2014 at 1:52 PM, Michal Mocny <mm...@chromium.org> wrote:

> I like the idea of hooks as events.
>
> Re: publishing to npm, I like to idea of hook publishing, but do we need
> another "package type"?  Platforms, plugins, and now hooks would all have
> different process/documentation.  Why not just add hooks support for
> plugins (I commented as such on the other thread first).
>
> -Michal
>
>
> On Tue, Aug 5, 2014 at 4:31 PM, Brian LeRoux <b...@brian.io> wrote:
>
> > yes! this
> >
> > and related to your earlier thread: maybe hooks themselves should just be
> > npm modules
> >
> > The hook lookups could walk up node_modules and look for modules with a
> > package.json that have a "cordova" key… maybe this should go to the other
> > thread
> >
> >
> > On Tue, Aug 5, 2014 at 1:18 PM, Carlos Santana <cs...@gmail.com>
> > wrote:
> >
> > > I started to write hooks, and I got lazy :-)
> > >
> > > What about having a single hook nodejs script that listen on cordova
> > > events?
> > >
> > > My script will look something like this:
> > >
> > > function worklightBeforePrepare(){
> > > console.log('Running Worklight worklightBeforePrepare()');
> > > }
> > > function worklightAfterCompile(){
> > > console.log('Running Worklight worklightAfterCompile()');
> > > }
> > > function worklightHooks(cordova_lib){
> > > cordova_lib.events.on('before_prepare',worklightBeforePrepare);
> > > cordova_lib.events.on('after_compile',worklightAfterCompile);
> > > }
> > > module.exports = {
> > > run: worklightHooks
> > > }
> > >
> > >
> > > Then when cordova cli starts it always runs my hook script:
> > >
> > > var script = path.join(root,'hook/global/worklight_cordova_hook.js');
> > > var hook = require(script);
> > > hook.run(cordova_lib);
> > >
> > >
> > > --
> > > Carlos Santana
> > > <cs...@gmail.com>
> > >
> >
>

Re: Can I have all my hooks in one file and be event driven?

Posted by Michal Mocny <mm...@chromium.org>.
I like the idea of hooks as events.

Re: publishing to npm, I like to idea of hook publishing, but do we need
another "package type"?  Platforms, plugins, and now hooks would all have
different process/documentation.  Why not just add hooks support for
plugins (I commented as such on the other thread first).

-Michal


On Tue, Aug 5, 2014 at 4:31 PM, Brian LeRoux <b...@brian.io> wrote:

> yes! this
>
> and related to your earlier thread: maybe hooks themselves should just be
> npm modules
>
> The hook lookups could walk up node_modules and look for modules with a
> package.json that have a "cordova" key… maybe this should go to the other
> thread
>
>
> On Tue, Aug 5, 2014 at 1:18 PM, Carlos Santana <cs...@gmail.com>
> wrote:
>
> > I started to write hooks, and I got lazy :-)
> >
> > What about having a single hook nodejs script that listen on cordova
> > events?
> >
> > My script will look something like this:
> >
> > function worklightBeforePrepare(){
> > console.log('Running Worklight worklightBeforePrepare()');
> > }
> > function worklightAfterCompile(){
> > console.log('Running Worklight worklightAfterCompile()');
> > }
> > function worklightHooks(cordova_lib){
> > cordova_lib.events.on('before_prepare',worklightBeforePrepare);
> > cordova_lib.events.on('after_compile',worklightAfterCompile);
> > }
> > module.exports = {
> > run: worklightHooks
> > }
> >
> >
> > Then when cordova cli starts it always runs my hook script:
> >
> > var script = path.join(root,'hook/global/worklight_cordova_hook.js');
> > var hook = require(script);
> > hook.run(cordova_lib);
> >
> >
> > --
> > Carlos Santana
> > <cs...@gmail.com>
> >
>

Re: Can I have all my hooks in one file and be event driven?

Posted by Brian LeRoux <b...@brian.io>.
yes! this

and related to your earlier thread: maybe hooks themselves should just be
npm modules

The hook lookups could walk up node_modules and look for modules with a
package.json that have a "cordova" key… maybe this should go to the other
thread


On Tue, Aug 5, 2014 at 1:18 PM, Carlos Santana <cs...@gmail.com> wrote:

> I started to write hooks, and I got lazy :-)
>
> What about having a single hook nodejs script that listen on cordova
> events?
>
> My script will look something like this:
>
> function worklightBeforePrepare(){
> console.log('Running Worklight worklightBeforePrepare()');
> }
> function worklightAfterCompile(){
> console.log('Running Worklight worklightAfterCompile()');
> }
> function worklightHooks(cordova_lib){
> cordova_lib.events.on('before_prepare',worklightBeforePrepare);
> cordova_lib.events.on('after_compile',worklightAfterCompile);
> }
> module.exports = {
> run: worklightHooks
> }
>
>
> Then when cordova cli starts it always runs my hook script:
>
> var script = path.join(root,'hook/global/worklight_cordova_hook.js');
> var hook = require(script);
> hook.run(cordova_lib);
>
>
> --
> Carlos Santana
> <cs...@gmail.com>
>