Approach:
- The file starts with shell code.
- That code uses Node.js to execute the file in ESM mode, after it removes the initial non-JavaScript lines.
- Node.js does not currently have CLI flags for achieving what we do in this step. Therefore, we have to pipe to the
node
executable.
- Node.js does not currently have CLI flags for achieving what we do in this step. Therefore, we have to pipe to the
When editing this file, we want to use the JavaScript mode of our IDE or editor. Therefore, we try to “hide” the shell code from JavaScript as much as possible.
Suggestions for improvements welcome!
Very nice! A couple of tiny mods to
myscript.bat
you might consider? Changing%0
to%~f0
in line 3 would allow invoking from the Windows command line using simplymyscript
rather thanmyscript.bat
. Also changing0
in line 4 to%errorlevel%
would propagate any exit code to a parent process: