Skip to content

Commit e963056

Browse files
authored
refactor: use watchDebounced from @netlify/dev-utils (#7467)
* Replaced all usages of cli watchDebounced with dev-utils * removed unused imports in command-helpers.ts * Removed unused exports that are now in @netlify/dev-utils * Revising watchDebounced mock test * Fixed import group in registry.ts
1 parent aef99f7 commit e963056

File tree

4 files changed

+5
-76
lines changed

4 files changed

+5
-76
lines changed

src/lib/edge-functions/registry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { join } from 'path'
33
import { fileURLToPath } from 'url'
44

55
import type { Declaration, EdgeFunction, FunctionConfig, Manifest, ModuleGraph } from '@netlify/edge-bundler'
6+
import { watchDebounced } from '@netlify/dev-utils'
67

78
import BaseCommand from '../../commands/base-command.js'
89
import {
@@ -13,7 +14,6 @@ import {
1314
chalk,
1415
log,
1516
warn,
16-
watchDebounced,
1717
isNodeError,
1818
type NormalizedCachedConfigConfig,
1919
} from '../../utils/command-helpers.js'

src/lib/functions/registry.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { basename, extname, isAbsolute, join, resolve } from 'path'
44
import { env } from 'process'
55

66
import { type ListedFunction, listFunctions, type Manifest } from '@netlify/zip-it-and-ship-it'
7-
import type { MemoizeCache } from '@netlify/dev-utils'
7+
import { type MemoizeCache, watchDebounced } from '@netlify/dev-utils'
88
import extractZip from 'extract-zip'
99

1010
import {
@@ -15,7 +15,6 @@ import {
1515
NETLIFYDEVLOG,
1616
NETLIFYDEVWARN,
1717
warn,
18-
watchDebounced,
1918
type NormalizedCachedConfigConfig,
2019
} from '../../utils/command-helpers.js'
2120
import { getFrameworksAPIPaths } from '../../utils/frameworks-api.js'

src/utils/command-helpers.ts

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { once } from 'events'
21
import os from 'os'
32
import fs from 'fs'
43
import process from 'process'
@@ -7,10 +6,7 @@ import { format, inspect } from 'util'
76
import type { NetlifyAPI } from '@netlify/api'
87
import { getGlobalConfigStore } from '@netlify/dev-utils'
98
import { Chalk } from 'chalk'
10-
import chokidar from 'chokidar'
11-
import decache from 'decache'
129
import WSL from 'is-wsl'
13-
import debounce from 'lodash/debounce.js'
1410
import terminalLink from 'terminal-link'
1511

1612
import { startSpinner } from '../lib/spinner.js'
@@ -235,79 +231,13 @@ export const normalizeConfig = (config: CachedConfig['config']): NormalizedCache
235231
return publishOrigin === 'default' ? { ...config, build } : config
236232
}
237233

238-
const DEBOUNCE_WAIT = 100
239-
240-
interface WatchDebouncedOptions {
241-
depth?: number
242-
ignored?: (string | RegExp)[]
243-
onAdd?: (paths: string[]) => void
244-
onChange?: (paths: string[]) => void
245-
onUnlink?: (paths: string[]) => void
246-
}
247-
248-
/**
249-
* Adds a file watcher to a path or set of paths and debounces the events.
250-
*/
251-
export const watchDebounced = async (
252-
target: string | string[],
253-
{ depth, ignored = [], onAdd = noOp, onChange = noOp, onUnlink = noOp }: WatchDebouncedOptions,
254-
) => {
255-
const baseIgnores = [/\/(node_modules|.git)\//]
256-
const watcher = chokidar.watch(target, { depth, ignored: [...baseIgnores, ...ignored], ignoreInitial: true })
257-
258-
await once(watcher, 'ready')
259-
260-
let onChangeQueue: string[] = []
261-
let onAddQueue: string[] = []
262-
let onUnlinkQueue: string[] = []
263-
264-
const debouncedOnChange = debounce(() => {
265-
onChange(onChangeQueue)
266-
onChangeQueue = []
267-
}, DEBOUNCE_WAIT)
268-
const debouncedOnAdd = debounce(() => {
269-
onAdd(onAddQueue)
270-
onAddQueue = []
271-
}, DEBOUNCE_WAIT)
272-
const debouncedOnUnlink = debounce(() => {
273-
onUnlink(onUnlinkQueue)
274-
onUnlinkQueue = []
275-
}, DEBOUNCE_WAIT)
276-
277-
watcher
278-
.on('change', (path) => {
279-
// @ts-expect-error
280-
decache(path)
281-
onChangeQueue.push(path)
282-
debouncedOnChange()
283-
})
284-
.on('unlink', (path) => {
285-
// @ts-expect-error
286-
decache(path)
287-
onUnlinkQueue.push(path)
288-
debouncedOnUnlink()
289-
})
290-
.on('add', (path) => {
291-
// @ts-expect-error
292-
decache(path)
293-
onAddQueue.push(path)
294-
debouncedOnAdd()
295-
})
296-
297-
return watcher
298-
}
299-
300234
export const getTerminalLink = (text: string, url: string): string =>
301235
terminalLink(text, url, { fallback: () => `${text} (${url})` })
302236

303237
export const isNodeError = (err: unknown): err is NodeJS.ErrnoException => err instanceof Error
304238

305239
export const nonNullable = <T>(value: T): value is NonNullable<T> => value !== null && value !== undefined
306240

307-
export const noOp = () => {
308-
// no-op
309-
}
310-
311241
export interface APIError extends Error {
312242
status: number
313243
message: string

tests/unit/lib/functions/registry.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { mkdir, mkdtemp, writeFile } from 'fs/promises'
22
import { tmpdir } from 'os'
33
import { join } from 'path'
44

5+
import { watchDebounced } from '@netlify/dev-utils'
56
import { describe, expect, test, vi } from 'vitest'
67

78
import { FunctionsRegistry } from '../../../../src/lib/functions/registry.js'
8-
import { watchDebounced } from '../../../../src/utils/command-helpers.js'
99
import { getFrameworksAPIPaths } from '../../../../src/utils/frameworks-api.js'
1010

1111
const duplicateFunctions = [
@@ -36,8 +36,8 @@ const duplicateFunctions = [
3636
},
3737
]
3838

39-
vi.mock('../../../../src/utils/command-helpers.js', async () => {
40-
const helpers = await vi.importActual('../../../../src/utils/command-helpers.js')
39+
vi.mock('@netlify/dev-utils', async () => {
40+
const helpers = await vi.importActual('@netlify/dev-utils')
4141

4242
return {
4343
...helpers,

0 commit comments

Comments
 (0)