lockfile.ts
1 /** 2 * Lazy accessor for proper-lockfile. 3 * 4 * proper-lockfile depends on graceful-fs, which monkey-patches every fs 5 * method on first require (~8ms). Static imports of proper-lockfile pull this 6 * cost into the startup path even when no locking happens (e.g. `--help`). 7 * 8 * Import this module instead of `proper-lockfile` directly. The underlying 9 * package is only loaded the first time a lock function is actually called. 10 */ 11 12 import type { CheckOptions, LockOptions, UnlockOptions } from 'proper-lockfile' 13 14 type Lockfile = typeof import('proper-lockfile') 15 16 let _lockfile: Lockfile | undefined 17 18 function getLockfile(): Lockfile { 19 if (!_lockfile) { 20 // eslint-disable-next-line @typescript-eslint/no-require-imports 21 _lockfile = require('proper-lockfile') as Lockfile 22 } 23 return _lockfile 24 } 25 26 export function lock( 27 file: string, 28 options?: LockOptions, 29 ): Promise<() => Promise<void>> { 30 return getLockfile().lock(file, options) 31 } 32 33 export function lockSync(file: string, options?: LockOptions): () => void { 34 return getLockfile().lockSync(file, options) 35 } 36 37 export function unlock(file: string, options?: UnlockOptions): Promise<void> { 38 return getLockfile().unlock(file, options) 39 } 40 41 export function check(file: string, options?: CheckOptions): Promise<boolean> { 42 return getLockfile().check(file, options) 43 }