Repository: facebook/jest
File: scripts/buildTs.mjs (JavaScript)

Code before:
} catch (e) {
  console.error(
    chalk.inverse.red(' Unable to build TypeScript definition files '),
  );
  throw e;
}

console.log(chalk.inverse(' Validating TypeScript definition files '));

// we want to limit the number of processes we spawn
const cpus = Math.max(
  1,
  (typeof os.availableParallelism === 'function'
    ? os.availableParallelism()
    : os.cpus().length) - 1,
);

const typesReferenceDirective = '/// <reference types';
const typesNodeReferenceDirective = `${typesReferenceDirective}="node" />`;

try {
  const mutex = pLimit(cpus);
  await Promise.all(
    packagesWithTs.map(({packageDir, pkg}) =>
      mutex(async () => {
        const matched = await glob.sync('build/**/*.d.ts', {
          absolute: true,
          cwd: packageDir,
        });

        const files = await Promise.all(
          matched.map(file =>
            Promise.all([file, fs.promises.readFile(file, 'utf8')]),
          ),
        );

        const filesWithTypeReferences = files
          .filter(([, content]) => content.includes(typesReferenceDirective))
          .filter(hit => hit.length > 0);

        const filesWithReferences = filesWithTypeReferences
          .map(([name, content]) => [
            name,
            content
              .split('\n')
              .map(line => line.trim())
              .filter(line => line.includes(typesReferenceDirective))
              .filter(line => line !== typesNodeReferenceDirective)
              .join('\n'),
          ])
          .filter(([, content]) => content.length > 0)

Review comment (suggestion):
Also this should not work on Windows, because the resulting pattern will have `\` separators. Hence it should not select any files.

`glob` has the `cwd` option to solve this problem.

Code after:
} catch (e) {
  console.error(
    chalk.inverse.red(' Unable to build TypeScript definition files '),
  );
  throw e;
}

console.log(chalk.inverse(' Validating TypeScript definition files '));

// we want to limit the number of processes we spawn
const cpus = Math.max(
  1,
  (typeof os.availableParallelism === 'function'
    ? os.availableParallelism()
    : os.cpus().length) - 1,
);

const typesReferenceDirective = '/// <reference types';
const typesNodeReferenceDirective = `${typesReferenceDirective}="node" />`;

try {
  const mutex = pLimit(cpus);
  await Promise.all(
    packagesWithTs.map(({packageDir, pkg}) =>
      mutex(async () => {
        const matched = glob.sync('build/**/*.d.ts', {
          absolute: true,
          cwd: packageDir,
        });

        const files = await Promise.all(
          matched.map(file =>
            Promise.all([file, fs.promises.readFile(file, 'utf8')]),
          ),
        );

        const filesWithTypeReferences = files
          .filter(([, content]) => content.includes(typesReferenceDirective))
          .filter(hit => hit.length > 0);

        const filesWithReferences = filesWithTypeReferences
          .map(([name, content]) => [
            name,
            content
              .split('\n')
              .map(line => line.trim())
              .filter(line => line.includes(typesReferenceDirective))
              .filter(line => line !== typesNodeReferenceDirective)
              .join('\n'),
          ])
          .filter(([, content]) => content.length > 0)