r2-streamer-js icon indicating copy to clipboard operation
r2-streamer-js copied to clipboard

Can't run as a binary

Open jccr opened this issue 6 years ago • 4 comments

I'm trying to use this in the webpub-viewer project to replace the in-tree copy. I added r2-streamer-js as a devDependency and thought I could use it as a binary in one of my project's scripts.

It is not working unfortunately.

> [email protected] streamed ./webpub-viewer
> r2-streamer-js-server ./examples/streamed/epubs/

./webpub-viewer/node_modules/.bin/r2-streamer-js-server: line 1: use strict: command not found
./webpub-viewer/node_modules/.bin/r2-streamer-js-server: line 2: syntax error near unexpected token `exports,'
./webpub-viewer/node_modules/.bin/r2-streamer-js-server: line 2: `Object.defineProperty(exports, "__esModule", { value: true });'

I believe you need to prepend the declared "bin" script with a node "shebang". Example: https://github.com/isaacs/rimraf/blob/master/bin.js (Line 1)

After manually adding #!/usr/bin/env node to the dist bin script it works as expected.

jccr avatar Mar 30 '19 00:03 jccr

Good idea. Originally, the primary integration use-case for this r2-streamer-js package was really just the "desktop app" (i.e. programmatic invocation), whereas the standalone Heroku/Now.sh server deployments were really just for experimenting / showcasing features.

Right now the NodeJS CLI is invoked from an NPM task/script, or a Procfile script (depending on the target cloud/build service): https://github.com/readium/r2-streamer-js/blob/04d02a088f8fb1bd36a48f5aa0900c0c40d5f63c/package-scripts.cson#L263-L265 https://github.com/readium/r2-streamer-js/blob/04d02a088f8fb1bd36a48f5aa0900c0c40d5f63c/Procfile#L1

There are variants of the NPM script to automatically setup debug verbosity, and various other environment variables: https://github.com/readium/r2-streamer-js/blob/04d02a088f8fb1bd36a48f5aa0900c0c40d5f63c/package-scripts.cson#L267-L285

The main code entry point is this TypeScript file: https://github.com/readium/r2-streamer-js/blob/develop/src/http/server-cli.ts

...which gets transpiled to the supported ECMAScript variants (e.g. ./dist/es8-es2017). To be honest I am not sure where to place the #!/usr/bin/env node line. We would have to test different build techniques ... for example we could add a hard-coded Javascript file for the default "bin" CLI script (no TypeScript involved at all), which a build step would copy across into the dist folder, and the package.json could reference this as the default "binary" to be automatically symbolic-link'ed when NPM installing?

danielweck avatar Apr 02 '19 21:04 danielweck

...alternatively the build system could patch the generated ./dist/XXXX/src/http/server-cli.js (where XXXX would be es5, es6-es2015, es7-es2016, es8-es2017) to insert the #!/usr/bin/env node shebang at the top of the file(s).

danielweck avatar Apr 02 '19 21:04 danielweck

Same issue with r2-shared-js-cli

jccr avatar Jun 23 '19 20:06 jccr

Here's an example of a project where the #!/usr/bin/env node is added to a special "bootstrapper" file for CLI usage: https://github.com/daisy/ace/blob/master/packages/ace-cli/bin/ace.js

https://github.com/daisy/ace/blob/be5d8a4154a2a38a07b28475f9e0538d91a0bab3/packages/ace-cli/package.json#L20

Note that the lib folder is generated at build time (a bit like the dist folder of r2-xxx-js packages), which explains this too: https://github.com/daisy/ace/blob/be5d8a4154a2a38a07b28475f9e0538d91a0bab3/packages/ace-cli/package.json#L19

danielweck avatar Jul 11 '19 22:07 danielweck