Skip to content

Commit 091ea4e

Browse files
refactor: use bun init instead of copying template config files (#24)
* refactor: use bun init instead of copying template config files - Remove template package.json, tsconfig.json, and .gitignore files - Update init command to run `bun init -y` to generate project config - Add proper error handling for when Bun is not installed - Install @types/node dependency after project initialization This approach is cleaner as it lets Bun generate the appropriate configuration files instead of maintaining static template copies. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> * ci: add Bun setup to GitHub Actions workflows The init command now uses 'bun init -y' to create the TypeScript project, so Bun needs to be installed in the CI environment for tests to pass. Added oven-sh/setup-bun@v2 action to all test jobs (test, smoke, integration). 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
1 parent 8e63b74 commit 091ea4e

File tree

5 files changed

+61
-55
lines changed

5 files changed

+61
-55
lines changed

.github/workflows/test.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ jobs:
2020
with:
2121
node-version: ${{ matrix.node }}
2222
cache: npm
23+
- uses: oven-sh/setup-bun@v2
24+
with:
25+
bun-version: latest
2326
- run: npm ci
2427
- run: npm run build
2528
- run: npm test
@@ -43,6 +46,9 @@ jobs:
4346
with:
4447
node-version: 20
4548
cache: npm
49+
- uses: oven-sh/setup-bun@v2
50+
with:
51+
bun-version: latest
4652
- run: npm ci
4753
- run: npm run build
4854
- run: npm run test:smoke
@@ -55,6 +61,9 @@ jobs:
5561
with:
5662
node-version: 20
5763
cache: npm
64+
- uses: oven-sh/setup-bun@v2
65+
with:
66+
bun-version: latest
5867
- run: npm ci
5968
- run: npm run build
6069
- run: npm run test:integration

src/commands/init.ts

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ This command sets up basic Claude Code hooks in your project:
7272
// Update or create settings.json
7373
await this.updateSettings()
7474

75-
// Run bun install to install dependencies
75+
// Install required dependencies
7676
spinner.text = 'Installing dependencies...'
77-
await this.runBunInstall()
77+
await this.installDependencies()
7878

7979
spinner.succeed('Hooks setup complete!')
8080

@@ -89,7 +89,13 @@ This command sets up basic Claude Code hooks in your project:
8989

9090
// Provide more detailed error messages
9191
if (error instanceof Error) {
92-
if (error.message.includes('EACCES') || error.message.includes('permission')) {
92+
if (error.message.includes('Bun is not installed')) {
93+
console.error(chalk.red('\n❌ Bun Not Found:'))
94+
console.error(chalk.yellow(' Bun is required to initialize the hook system.'))
95+
console.error(chalk.gray(' Please install Bun first:'))
96+
console.error(chalk.cyan(' curl -fsSL https://bun.sh/install | bash'))
97+
console.error(chalk.gray('\n After installing, make sure Bun is in your PATH and run this command again.'))
98+
} else if (error.message.includes('EACCES') || error.message.includes('permission')) {
9399
console.error(chalk.red('\n❌ Permission Error:'))
94100
console.error(chalk.yellow(' You do not have permission to write to this directory.'))
95101
console.error(chalk.gray(' Try running with elevated permissions or check directory ownership.'))
@@ -113,22 +119,55 @@ This command sets up basic Claude Code hooks in your project:
113119
const rootDir = path.join(distDir, '..', '..')
114120
const templatesDir = path.join(rootDir, 'templates')
115121

116-
// Copy all hook template files
122+
// First, run bun init to create a proper TypeScript project
123+
await this.runBunInit()
124+
125+
// Then copy our hook template files
117126
await fs.copy(path.join(templatesDir, 'hooks', 'lib.ts'), '.claude/hooks/lib.ts')
118127
await fs.copy(path.join(templatesDir, 'hooks', 'session.ts'), '.claude/hooks/session.ts')
119128
await fs.copy(path.join(templatesDir, 'hooks', 'index.ts'), '.claude/hooks/index.ts')
129+
}
130+
131+
private async runBunInit(): Promise<void> {
132+
const {spawn} = await import('node:child_process')
120133

121-
// Copy TypeScript configuration files
122-
await fs.copy(path.join(templatesDir, 'hooks', 'package.json'), '.claude/hooks/package.json')
123-
await fs.copy(path.join(templatesDir, 'hooks', 'tsconfig.json'), '.claude/hooks/tsconfig.json')
124-
await fs.copy(path.join(templatesDir, 'hooks', '.gitignore'), '.claude/hooks/.gitignore')
134+
return new Promise((resolve, reject) => {
135+
const child = spawn('bun', ['init', '-y'], {
136+
cwd: '.claude/hooks',
137+
stdio: 'pipe',
138+
shell: false,
139+
})
140+
141+
let _stderr = ''
142+
143+
child.stderr?.on('data', (data) => {
144+
_stderr += data.toString()
145+
})
146+
147+
child.on('error', (error) => {
148+
if (error.message.includes('ENOENT')) {
149+
reject(new Error('Bun is not installed. Please install Bun first: https://bun.sh'))
150+
} else {
151+
reject(new Error(`Failed to run bun init: ${error.message}`))
152+
}
153+
})
154+
155+
child.on('exit', (code) => {
156+
if (code === 0) {
157+
resolve()
158+
} else {
159+
reject(new Error(`bun init failed with exit code ${code}: ${_stderr}`))
160+
}
161+
})
162+
})
125163
}
126164

127-
private async runBunInstall(): Promise<void> {
165+
private async installDependencies(): Promise<void> {
128166
const {spawn} = await import('node:child_process')
129167

168+
// Install required type definitions
130169
return new Promise((resolve, reject) => {
131-
const child = spawn('bun', ['install'], {
170+
const child = spawn('bun', ['add', '-d', '@types/node'], {
132171
cwd: '.claude/hooks',
133172
stdio: 'pipe',
134173
shell: false,
@@ -141,11 +180,11 @@ This command sets up basic Claude Code hooks in your project:
141180
})
142181

143182
child.on('error', (error) => {
144-
// If bun is not installed, we continue anyway
183+
// If bun is not installed, we've already warned about it
145184
if (error.message.includes('ENOENT')) {
146185
resolve()
147186
} else {
148-
reject(new Error(`Failed to run bun install: ${error.message}`))
187+
reject(new Error(`Failed to install dependencies: ${error.message}`))
149188
}
150189
})
151190

@@ -154,7 +193,7 @@ This command sets up basic Claude Code hooks in your project:
154193
resolve()
155194
} else {
156195
// Non-zero exit code but not a critical failure
157-
// User can manually run bun install later
196+
// User can manually install dependencies later
158197
resolve()
159198
}
160199
})

templates/hooks/.gitignore

Lines changed: 0 additions & 4 deletions
This file was deleted.

templates/hooks/package.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

templates/hooks/tsconfig.json

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)