Skip to content

Commit

Permalink
only treat package.json as changed if data changes
Browse files Browse the repository at this point in the history
Fix: #49
  • Loading branch information
isaacs committed Mar 9, 2024
1 parent f545901 commit fe10a62
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/watch.ts
Expand Up @@ -3,10 +3,24 @@
import chalk from 'chalk'
import { spawn } from 'child_process'
import { watch, WatchOptions } from 'chokidar'
import { readFileSync } from 'fs'
import { resolve, sep } from 'path'
import { fileURLToPath } from 'url'
import * as tshyConsole from './console.js'

const pjData = (): string => {
try {
return JSON.stringify(
JSON.parse(readFileSync('./package.json', 'utf8'))
)
/* c8 ignore start */
} catch {
return 'null'
}
/* c8 ignore stop */
}
let lastPJData: string = 'null'

export const options: WatchOptions = {
persistent: true,
ignoreInitial: true,
Expand All @@ -19,6 +33,7 @@ export const options: WatchOptions = {
return false
},
}

export const srcPJ = resolve('./src/package.json')
export const srcNM = resolve('./src/node_modules')
export const src = resolve('./src')
Expand All @@ -40,12 +55,19 @@ export default () => {
if (code || signal) tshyConsole.error({ code, signal })
else console.log(chalk.green('build success'), { code, signal })
if (needRebuild) build()
else setTimeout(() => (building = false), 50)
else building = false
})
}
watcher.on('all', (ev, path) => {
const r = resolve(path)
if (r === srcPJ) return
if (r === rootPJ) {
// check if the data actually changed
const newData = pjData()
/* c8 ignore next */
if (newData === lastPJData) return
lastPJData = newData
}
if (building) {
if (r !== rootPJ) needRebuild = true
return
Expand Down
1 change: 0 additions & 1 deletion test/watch.ts
Expand Up @@ -41,7 +41,6 @@ type SpawnResult = {
}
const spawnOK: SpawnResult = { code: 0, signal: null }
const spawnExitCode: SpawnResult = { code: 1, signal: null }
const spawnExitSignal: SpawnResult = { code: null, signal: 'SIGTERM' }
let spawnResult: SpawnResult = spawnOK

const mockSpawn = (
Expand Down

0 comments on commit fe10a62

Please sign in to comment.