Skip to content

Commit

Permalink
Fix missing shebang in main.ts which breaks execution via npx
Browse files Browse the repository at this point in the history
  • Loading branch information
jirutka committed Jul 29, 2024
1 parent 8605af3 commit 7d7695a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
11 changes: 10 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ const executable = () => ({
// license plugin.
renderChunk(code, _, opts) {
const magic = new MagicString(code)
magic.prepend('#!/usr/bin/env node\n')

const match = /^#!.*$/dm.exec(code)
if (match) {
// If code already contains a shebang, move it to the top.
magic.remove(match.index, match.index + match[0].length + 1)
magic.prepend(`${match[0]}\n`)
} else {
magic.prepend('#!/usr/bin/env node\n')
}

return {
code: magic.toString(),
map: opts.sourcemap !== false ? magic.generateMap() : null,
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env node
import { exit } from 'node:process'

import { Bool, type OptionsSchema, parseArgv } from './args-parser.js'
Expand Down

0 comments on commit 7d7695a

Please sign in to comment.