From fe10a62303baf89c0f3f1a102b6bd1ff68b86a6a Mon Sep 17 00:00:00 2001 From: isaacs Date: Sat, 9 Mar 2024 08:41:35 -0800 Subject: [PATCH] only treat package.json as changed if data changes Fix: #49 --- src/watch.ts | 24 +++++++++++++++++++++++- test/watch.ts | 1 - 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/watch.ts b/src/watch.ts index c884317..dd12b0c 100644 --- a/src/watch.ts +++ b/src/watch.ts @@ -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, @@ -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') @@ -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 diff --git a/test/watch.ts b/test/watch.ts index fac0741..17456e5 100644 --- a/test/watch.ts +++ b/test/watch.ts @@ -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 = (