πŸ“„ File detail

utils/lockfile.ts

🧩 .tsπŸ“ 44 linesπŸ’Ύ 1,330 bytesπŸ“ text
← Back to All Files

🎯 Use case

This file lives under β€œutils/”, which covers cross-cutting helpers (shell, tempfiles, settings, messages, process input, …). On the API surface it exposes lock, lockSync, unlock, and check β€” mainly functions, hooks, or classes. Dependencies touch proper-lockfile. What the file header says: Lazy accessor for proper-lockfile. proper-lockfile depends on graceful-fs, which monkey-patches every fs method on first require (~8ms). Static imports of proper-lockfile pull this cost into the startup path even when no locking happens (e.g. `--help`). Import this module instead.

Generated from folder role, exports, dependency roots, and inline comments β€” not hand-reviewed for every path.

🧠 Inline summary

Lazy accessor for proper-lockfile. proper-lockfile depends on graceful-fs, which monkey-patches every fs method on first require (~8ms). Static imports of proper-lockfile pull this cost into the startup path even when no locking happens (e.g. `--help`). Import this module instead of `proper-lockfile` directly. The underlying package is only loaded the first time a lock function is actually called.

πŸ“€ Exports (heuristic)

  • lock
  • lockSync
  • unlock
  • check

πŸ“š External import roots

Package roots from from "…" (relative paths omitted).

  • proper-lockfile

πŸ–₯️ Source preview

/**
 * Lazy accessor for proper-lockfile.
 *
 * proper-lockfile depends on graceful-fs, which monkey-patches every fs
 * method on first require (~8ms). Static imports of proper-lockfile pull this
 * cost into the startup path even when no locking happens (e.g. `--help`).
 *
 * Import this module instead of `proper-lockfile` directly. The underlying
 * package is only loaded the first time a lock function is actually called.
 */

import type { CheckOptions, LockOptions, UnlockOptions } from 'proper-lockfile'

type Lockfile = typeof import('proper-lockfile')

let _lockfile: Lockfile | undefined

function getLockfile(): Lockfile {
  if (!_lockfile) {
    // eslint-disable-next-line @typescript-eslint/no-require-imports
    _lockfile = require('proper-lockfile') as Lockfile
  }
  return _lockfile
}

export function lock(
  file: string,
  options?: LockOptions,
): Promise<() => Promise<void>> {
  return getLockfile().lock(file, options)
}

export function lockSync(file: string, options?: LockOptions): () => void {
  return getLockfile().lockSync(file, options)
}

export function unlock(file: string, options?: UnlockOptions): Promise<void> {
  return getLockfile().unlock(file, options)
}

export function check(file: string, options?: CheckOptions): Promise<boolean> {
  return getLockfile().check(file, options)
}