question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Insert bin prefix for script packages

See original GitHub issue

Current Behavior

The output of index.js begins with use strict which if the package is intended to be a script, an error outputs.

Desired Behavior

When a package.json includes a bin parameter, prefix the index.js file with #!/usr/bin/env node

Who does this impact? Who is this for?

This is for users who wish to use tsdx for creating packages that are intended to be used in the command line.

Additional context

If it doesn’t make sense to base the behavior on the bin parameter in the package.json, it might make sense instead to make this a new template type, but it’s so similar to node, that this might be more effort than necessary.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:9
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
karlhorkycommented, Jan 17, 2021

We currently use TSDX to build Preflight, a command-line tool for the @upleveled students to check their project work before submitting.

The way that we’re dealing with it is to have a separate file in a bin folder, similar to the code below (although ours uses ES Modules):

#!/usr/bin/env node

'use strict';

require('../src/cli').run(process.argv.slice(2));

This is also how Prettier handles it - see also their package.json

Maybe this should be the recommendation from TSDX until there are other viable options?

1reaction
agilgur5commented, Oct 1, 2020

So I’m working on a fix to this by making the dev/prod entry an actual Rollup plugin per https://github.com/formium/tsdx/issues/793#issuecomment-673952338 (it being out-of-band causes some issues like https://github.com/formium/tsdx/issues/351#issuecomment-657284291, also other folks could potentially use it) and recently had a breakthrough on how to make it work with multiple inputs (mostly by heavily reading the Rollup docs again, which I’ve been doing for upstream fixes in rollup-plugin-typescript2 like https://github.com/ezolenko/rollup-plugin-typescript2/pull/221 anyway). I feel pretty close but still need to do some testing, particularly against #367

In the meantime, here’s a one-liner workaround in Bash for this that one can use in a post-build script:

echo -e '#!/usr/bin/env node\n' "$(cat dist/index.js)" > dist/index.js
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I install an executable in $PREFIX/bin using Pkg?
The installed script can keep the path to Julia project and julia executable. Users would run ScriptInstaller.add("PACKAGE") which creates a dedicated project, ...
Read more >
scripts - npm Docs
To create "pre" or "post" scripts for any scripts defined in the "scripts" section of the package.json , simply create another script with...
Read more >
2. Writing the Setup Script — Python 3.11.1 documentation
The Distutils' own setup script, shown here, is used to install the package into Python 1.5.2.) #!/usr/bin/env python from distutils.core import setup ...
Read more >
Installing and running Node.js bin scripts - 2ality
Locally installing a package with bin scripts means installing it as a dependency inside a package. The scripts are only accessible within that ......
Read more >
npm install not working with --prefix - node.js - Stack Overflow
"Main" package.json contains all scripts (for Heroku and for me) and dependiencies for server. client is Angular2 app that's why it has own ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found