Repository: parcel-bundler/parcel
File: packages/reporters/lsp-reporter/src/LspReporter.js (JavaScript)

Code before:
  makeDeferredWithPromise<?BundleGraph<PackagedBundle>>();
let bundleGraph: Promise<?BundleGraph<PackagedBundle>> =
  bundleGraphDeferrable.promise;

let watchStarted = false;
let lspStarted = false;
let watchStartPromise;

async function watchLspActive(): Promise<FSWatcher> {
  const lspFileName = 'lsp-server';

  // Check for lsp-server when reporter is first started
  try {
    await fs.promises.access(
      path.join(BASEDIR, 'lsp-server'),
      fs.constants.F_OK,
    );
    lspStarted = true;
  } catch {
    //
  }

  return fs.watch(BASEDIR, (eventType: string, filename: string) => {
    switch (eventType) {
      case 'rename':
        if (filename === lspFileName) {
          fs.access(
            path.join(BASEDIR, 'lsp-server'),
            fs.constants.F_OK,
            err => {
              if (err) {
                lspStarted = false;
              } else {
                lspStarted = true;
              }
            },
          );
        }
    }
  });
}

async function doWatchStart() {
  await fs.promises.mkdir(BASEDIR, {recursive: true});

  // For each existing file, check if the pid matches a running process.
  // If no process matches, delete the file, assuming it was orphaned
  // by a process that quit unexpectedly.
  for (let filename of fs.readdirSync(BASEDIR)) {
    if (filename.endsWith('.json')) continue;
    let pid = parseInt(filename.slice('parcel-'.length), 10);

Review comment (suggestion):
```suggestion
        if (filename === LSP_SENTINEL_FILENAME) {
```

Code after:
  makeDeferredWithPromise<?BundleGraph<PackagedBundle>>();
let bundleGraph: Promise<?BundleGraph<PackagedBundle>> =
  bundleGraphDeferrable.promise;

let watchStarted = false;
let lspStarted = false;
let watchStartPromise;

const LSP_SENTINEL_FILENAME = 'lsp-server';
const LSP_SENTINEL_FILE = path.join(BASEDIR, LSP_SENTINEL_FILENAME);

async function watchLspActive(): Promise<FSWatcher> {
  // Check for lsp-server when reporter is first started
  try {
    await fs.promises.access(LSP_SENTINEL_FILE, fs.constants.F_OK);
    lspStarted = true;
  } catch {
    //
  }

  return fs.watch(BASEDIR, (eventType: string, filename: string) => {
    switch (eventType) {
      case 'rename':
        if (filename === LSP_SENTINEL_FILENAME) {
          fs.access(LSP_SENTINEL_FILE, fs.constants.F_OK, err => {
            if (err) {
              lspStarted = false;
            } else {
              lspStarted = true;
            }
          });
        }
    }
  });
}

async function doWatchStart() {
  await fs.promises.mkdir(BASEDIR, {recursive: true});

  // For each existing file, check if the pid matches a running process.
  // If no process matches, delete the file, assuming it was orphaned
  // by a process that quit unexpectedly.
  for (let filename of fs.readdirSync(BASEDIR)) {
    if (filename.endsWith('.json')) continue;
    let pid = parseInt(filename.slice('parcel-'.length), 10);
    let resultList = await lookupPid({pid});
    if (resultList.length > 0) continue;
    fs.unlinkSync(path.join(BASEDIR, filename));
    ignoreFail(() => fs.unlinkSync(path.join(BASEDIR, filename + '.json')));
  }