diff mbox

[v2,4/6] package/nodejs: Add /usr/lib/node_modules/.bin to PATH

Message ID 1435370498-25473-4-git-send-email-martin@barkynet.com
State Changes Requested
Headers show

Commit Message

Martin Bark June 27, 2015, 2:01 a.m. UTC
This patch adds /usr/lib/node_modules/.bin to PATH so that global
node_modules are accessible from the command line

Signed-off-by: Martin Bark <martin@barkynet.com>

---
Changes v1 -> v2
 - Improved commit message

Signed-off-by: Martin Bark <martin@barkynet.com>
---
 package/nodejs/nodejs.mk | 5 +++++
 1 file changed, 5 insertions(+)

Comments

Yann E. MORIN June 27, 2015, 10:44 p.m. UTC | #1
Martin, All,

On 2015-06-27 03:01 +0100, Martin Bark spake thusly:
> This patch adds /usr/lib/node_modules/.bin to PATH so that global
> node_modules are accessible from the command line
> 
> Signed-off-by: Martin Bark <martin@barkynet.com>
> 
> ---
> Changes v1 -> v2
>  - Improved commit message
> 
> Signed-off-by: Martin Bark <martin@barkynet.com>
> ---
>  package/nodejs/nodejs.mk | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/package/nodejs/nodejs.mk b/package/nodejs/nodejs.mk
> index 59e03b0..5d95f77 100644
> --- a/package/nodejs/nodejs.mk
> +++ b/package/nodejs/nodejs.mk
> @@ -111,6 +111,11 @@ define NODEJS_INSTALL_MODULES
>  		$(HOST_DIR)/usr/bin/npm install \
>  		$(NODEJS_MODULES_LIST) \
>  	)
> +
> +	# Add global node_modules to PATH
> +	$(INSTALL) -d -m 0755 $(TARGET_DIR)/etc/profile.d
> +	printf "export PATH=\x24PATH:/usr/lib/node_modules/.bin\n" \

Nice trick, but what about the slightly-more readable;

    printf 'PATH="$${PATH}:/usr/lib/node_modules/.bin"\n' \
        >$(TARGET_DIR)/etc/profile.d/node_modules.sh

  - because this is in a Makefile, so we need to $-escape the $.

  - because we do not want that $ to be interpreted by the shell (at build
    time!), we single-quote it.

  - because whoever knows what PATH is made of would be a fool (and
    because the world is cruel), we double-quote it (at runtime!)

Otherwise, what is the reason to have nodejs modules available as
commands from a login (!) shell? (Note: nodejs-noob speaking!)

Regards,
Yann E. MORIN.

> +		>$(TARGET_DIR)/etc/profile.d/node_modules.sh
>  endef
>  endif
>  
> -- 
> 2.1.4
> 
> _______________________________________________
> buildroot mailing list
> buildroot@busybox.net
> http://lists.busybox.net/mailman/listinfo/buildroot
Martin Bark June 29, 2015, 9:13 a.m. UTC | #2
Yann,

Yes I agree your way is easier to read, I'll update my patch and resubmit it.

The reason this is useful is because there lots of node.js packages
that are normal command line apps designed to be installed globally.
For example, if you install http-server
(https://github.com/indexzero/http-server) then you simple need to cd
into any directory, type ./http-server and it will server that
directory up for you.  Another example might be vtop
(https://github.com/MrRio/vtop), a graphical version of top.

If you install these apps globally then all the executables will be in
the /usr/lib/node_modules/.bin directory.  So this patch simply saves
you typing the full path, the same as for your desktop.

You can test this out by setting BR2_PACKAGE_NODEJS_MODULES_ADDITIONAL
to one of these packages.  For reference these is a nice list of some
useful apps here
https://github.com/sindresorhus/awesome-nodejs#command-line-apps.

Thanks

Martin

On 27 June 2015 at 23:44, Yann E. MORIN <yann.morin.1998@free.fr> wrote:
> Martin, All,
>
> On 2015-06-27 03:01 +0100, Martin Bark spake thusly:
>> This patch adds /usr/lib/node_modules/.bin to PATH so that global
>> node_modules are accessible from the command line
>>
>> Signed-off-by: Martin Bark <martin@barkynet.com>
>>
>> ---
>> Changes v1 -> v2
>>  - Improved commit message
>>
>> Signed-off-by: Martin Bark <martin@barkynet.com>
>> ---
>>  package/nodejs/nodejs.mk | 5 +++++
>>  1 file changed, 5 insertions(+)
>>
>> diff --git a/package/nodejs/nodejs.mk b/package/nodejs/nodejs.mk
>> index 59e03b0..5d95f77 100644
>> --- a/package/nodejs/nodejs.mk
>> +++ b/package/nodejs/nodejs.mk
>> @@ -111,6 +111,11 @@ define NODEJS_INSTALL_MODULES
>>               $(HOST_DIR)/usr/bin/npm install \
>>               $(NODEJS_MODULES_LIST) \
>>       )
>> +
>> +     # Add global node_modules to PATH
>> +     $(INSTALL) -d -m 0755 $(TARGET_DIR)/etc/profile.d
>> +     printf "export PATH=\x24PATH:/usr/lib/node_modules/.bin\n" \
>
> Nice trick, but what about the slightly-more readable;
>
>     printf 'PATH="$${PATH}:/usr/lib/node_modules/.bin"\n' \
>         >$(TARGET_DIR)/etc/profile.d/node_modules.sh
>
>   - because this is in a Makefile, so we need to $-escape the $.
>
>   - because we do not want that $ to be interpreted by the shell (at build
>     time!), we single-quote it.
>
>   - because whoever knows what PATH is made of would be a fool (and
>     because the world is cruel), we double-quote it (at runtime!)
>
> Otherwise, what is the reason to have nodejs modules available as
> commands from a login (!) shell? (Note: nodejs-noob speaking!)
>
> Regards,
> Yann E. MORIN.
>
>> +             >$(TARGET_DIR)/etc/profile.d/node_modules.sh
>>  endef
>>  endif
>>
>> --
>> 2.1.4
>>
>> _______________________________________________
>> buildroot mailing list
>> buildroot@busybox.net
>> http://lists.busybox.net/mailman/listinfo/buildroot
>
> --
> .-----------------.--------------------.------------------.--------------------.
> |  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
> | +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
> | +33 223 225 172 `------------.-------:  X  AGAINST      |  \e/  There is no  |
> | http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
> '------------------------------^-------^------------------^--------------------'
Yann E. MORIN June 29, 2015, 5:20 p.m. UTC | #3
Martin, All,

On 2015-06-29 10:13 +0100, Martin Bark spake thusly:
> Yes I agree your way is easier to read, I'll update my patch and resubmit it.

Great, thanls! :-)

> The reason this is useful is because there lots of node.js packages
> that are normal command line apps designed to be installed globally.
> For example, if you install http-server
> (https://github.com/indexzero/http-server) then you simple need to cd
> into any directory, type ./http-server and it will server that

You mean, http-server must be in the directory you want to serve? Or did
you mean that you just run /path/to/http-server and it will serve the
current directory?  (I guess the latter.)

> directory up for you.  Another example might be vtop
> (https://github.com/MrRio/vtop), a graphical version of top.
> 
> If you install these apps globally then all the executables will be in
> the /usr/lib/node_modules/.bin directory.  So this patch simply saves
> you typing the full path, the same as for your desktop.

OK, so why not make symlinks (or hadlinks) for each of them in /usr/bin
instead? Something like (untested):

    cd $(TARGET_DIR)/usr/bin; \
    for f in ../../usr/lib/node_modules/.bin/*; do \
        [ -f "$${f}" -a -x "$${f}" ] || continue; \
        ln -sf "$${f}" "$${f##*/}" || exit 1; \
    done

Regards,
Yann E. MORIN.
Martin Bark June 29, 2015, 7:31 p.m. UTC | #4
Yann,

On 29 June 2015 at 18:20, Yann E. MORIN <yann.morin.1998@free.fr> wrote:
> Martin, All,
>
> On 2015-06-29 10:13 +0100, Martin Bark spake thusly:
>> Yes I agree your way is easier to read, I'll update my patch and resubmit it.
>
> Great, thanls! :-)
>
>> The reason this is useful is because there lots of node.js packages
>> that are normal command line apps designed to be installed globally.
>> For example, if you install http-server
>> (https://github.com/indexzero/http-server) then you simple need to cd
>> into any directory, type ./http-server and it will server that
>
> You mean, http-server must be in the directory you want to serve? Or did
> you mean that you just run /path/to/http-server and it will serve the
> current directory?  (I guess the latter.)

Yes i meant the latter

>
>> directory up for you.  Another example might be vtop
>> (https://github.com/MrRio/vtop), a graphical version of top.
>>
>> If you install these apps globally then all the executables will be in
>> the /usr/lib/node_modules/.bin directory.  So this patch simply saves
>> you typing the full path, the same as for your desktop.
>
> OK, so why not make symlinks (or hadlinks) for each of them in /usr/bin
> instead? Something like (untested):
>
>     cd $(TARGET_DIR)/usr/bin; \
>     for f in ../../usr/lib/node_modules/.bin/*; do \
>         [ -f "$${f}" -a -x "$${f}" ] || continue; \
>         ln -sf "$${f}" "$${f##*/}" || exit 1; \
>     done
>

OK i can see that working too and it saves adjusting PATH.  I'll send
an updated patch using your method.

Thanks

Martin
diff mbox

Patch

diff --git a/package/nodejs/nodejs.mk b/package/nodejs/nodejs.mk
index 59e03b0..5d95f77 100644
--- a/package/nodejs/nodejs.mk
+++ b/package/nodejs/nodejs.mk
@@ -111,6 +111,11 @@  define NODEJS_INSTALL_MODULES
 		$(HOST_DIR)/usr/bin/npm install \
 		$(NODEJS_MODULES_LIST) \
 	)
+
+	# Add global node_modules to PATH
+	$(INSTALL) -d -m 0755 $(TARGET_DIR)/etc/profile.d
+	printf "export PATH=\x24PATH:/usr/lib/node_modules/.bin\n" \
+		>$(TARGET_DIR)/etc/profile.d/node_modules.sh
 endef
 endif