Skip to content

Commit

Permalink
feat: allow multiple lines of hashbangs (google#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
rockofox committed Jan 21, 2024
1 parent 906786a commit bd8b89c
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,20 @@ async function importPath(filepath: string, origin = filepath) {
const __filename = resolve(origin)
const __dirname = dirname(__filename)
const require = createRequire(origin)
const shebangRegex = /^#!(.*)*\n/
const file = await fs.readFile(filepath)

Object.assign(global, { __filename, __dirname, require })

if (shebangRegex.test(file.toString())) {
return writeAndImport(
await file.toString().replace(shebangRegex, ''),
join(dirname(filepath), basename(filepath) + '.mjs'),
origin
)
}


await import(url.pathToFileURL(filepath).toString())
}

Expand Down

0 comments on commit bd8b89c

Please sign in to comment.