dimanche 1 février 2015

Creating a shebang pointing portably to an interpreter in the folder of a script


I have a JS file (file.js) that I want to have executed as a command line shell script through nodejs (iojs actually); I'm using MINGW Git Bash on Windows.


The standard way to do it is to put the following shebang in the JS file:



#!/usr/bin/env node


However, I want to pass a cmd line flag to node to activate V8 harmony features, i.e. I want an equivalent of



node --harmony_arrow_functions file.js


when run just as



./file.js


without the need to pass the flag manually.


Using



#!/usr/bin/env node --harmony_arrow_functions


does not work.


After considering http://ift.tt/1zJNfOP and http://ift.tt/1uPiJLY I created the following solution:



$ ls
file.js nodeharmony.sh

$ cat nodeharmony.sh
#!/bin/sh
exec node --harmony_arrow_functions "$@"

$ head -1 file.js
#!/bin/sh nodeharmony.sh # or ./nodeharmony.sh, doesn't matter

$ ./file.js # this works fine


But the problem is that when I execute it from another folder, the shell tries to find nodeharmony.sh in the current working directory, not the directory of file.js:



$ cd ..
$ ./subfolder/file.js
/bin/sh: ./nodeharmony.sh: No such file or directory


Is there a way to create a portable shebang in file.js such that I can run file.js from whatever folder, without resorting to having my custom interpreter (nodeharmony.sh) available in the PATH?



Aucun commentaire:

Enregistrer un commentaire