Skip to content

Commit ba75e54

Browse files
committed
feat: refactor tsdown.config.ts to improve file handling and add shebang support
1 parent a62d16a commit ba75e54

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

tsdown.config.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import { globSync } from 'node:fs'
1+
import { globSync, existsSync, readFileSync, writeFileSync } from 'node:fs'
2+
import { dirname, resolve } from 'node:path'
3+
import { fileURLToPath } from 'node:url'
24
import { defineConfig } from 'tsdown'
35

6+
const __filename = fileURLToPath(import.meta.url)
7+
const __dirname = dirname(__filename)
8+
49
const entry = globSync('src/**/*.ts')
510

611
export default defineConfig({
@@ -14,8 +19,24 @@ export default defineConfig({
1419
hooks: {
1520
'build:done': (ctx) => {
1621
const pkg = ctx.options.pkg
17-
const binFilesList = pkg.bin
18-
console.log(ctx)
22+
23+
const binFilesList =
24+
typeof pkg.bin === 'string'
25+
? [pkg.bin as string]
26+
: typeof pkg.bin === 'object'
27+
? [...new Set(Object.values(pkg.bin) as string[])]
28+
: []
29+
30+
binFilesList
31+
.map((file) => resolve(__dirname, file))
32+
.filter((file) => existsSync(file))
33+
.forEach((file) => {
34+
const content = readFileSync(file, 'utf8')
35+
const shebang = `#!/usr/bin/env node`
36+
37+
if (!content.startsWith(shebang))
38+
writeFileSync(file, `${shebang}\n${content}`)
39+
})
1940
}
2041
}
2142
})

0 commit comments

Comments
 (0)