Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't run as a binary #44

Open
jccr opened this issue Mar 30, 2019 · 4 comments
Open

Can't run as a binary #44

jccr opened this issue Mar 30, 2019 · 4 comments

Comments

@jccr
Copy link
Member

jccr commented Mar 30, 2019

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 jccr changed the title Can't launch as a binary Can't run as a binary Mar 30, 2019
@danielweck
Copy link
Member

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):

'server': '
node --version && node "./dist/es8-es2017/src/http/server-cli.js"
'

web: node --version && npm --version && DEBUG=r2:* node ./dist/es8-es2017/src/http/server-cli.js ./misc/epubs

There are variants of the NPM script to automatically setup debug verbosity, and various other environment variables:

'server-debug': '
cross-env DEBUG=r2:*
npm run server
'
'server-debug-x': '
cross-env DEBUG=*
npm run server
'
'server-https-debug': '
cross-env DEBUG=r2:* PORT=443
npm run server
'
'server-https-debug-x': '
cross-env DEBUG=* PORT=443
npm run server
'

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
Copy link
Member

...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).

@jccr
Copy link
Member Author

jccr commented Jun 23, 2019

Same issue with r2-shared-js-cli

@danielweck
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants