Skip to content

Commit

Permalink
build(watcher): added build:watch script
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurchishin committed Nov 8, 2023
1 parent 2178232 commit ea745ac
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
},
"types": "./dist/index.d.ts",
"scripts": {
"build": "bun run scripts/build.ts",
"build": "bun run scripts/build-command.ts",
"build:watch": "bun run scripts/build-watch-command.ts",
"type-check": "tsc --noEmit",
"type-check:watch": "bun run type-check -- --watch",
"test": "bun test",
Expand Down
3 changes: 3 additions & 0 deletions scripts/build-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import build from './build'

await build()
35 changes: 35 additions & 0 deletions scripts/build-watch-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* eslint-disable no-console,security/detect-non-literal-fs-filename */
import { watch } from 'node:fs'
import { resolve } from 'node:path'
import build from './build'

const srcDir = resolve(import.meta.dir, '../src')

try {
console.info('Building...')
await build()
console.info('Watching for changes...')
} catch (error) {
console.error('Failed to build', error)
}

const watcher = watch(srcDir, { recursive: true }, () => {
console.info('Rebuilding...')
build()
.then(() => {
console.info('Watching for changes...')

return null
})
.catch(error => {
console.error('Failed to build', error)
})
})

process.on('SIGINT', () => {
// close watcher when Ctrl-C is pressed
console.log('Closing watcher...')
watcher.close()

process.exit(0)
})
5 changes: 3 additions & 2 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ const bundle = async (minify: boolean) => {
}
}

await bundle(false)
await bundle(true)
const build = () => Promise.all([bundle(false), bundle(true)])

export default build

0 comments on commit ea745ac

Please sign in to comment.