Skip to content

⬅️ Back to Table of Contents

📄 pack-packages

📊 Analysis Summary

Metric Count
🔧 Functions 4
📦 Imports 5
📊 Variables & Constants 2
⚡ Async/Await Patterns 4
📐 Interfaces 2

📚 Table of Contents

🛠️ File Location:

📂 packages/integration-tests/tools/pack-packages.ts

📦 Imports

Name Source
TestProject vitest/node
pathToFileURL node:url
promisify node:util
yaml yaml
rootPackageJson ../../../package.json

Variables & Constants

Name Type Kind Value Exported
FIXTURES_DIR_BASENAME "fixtures" const 'fixtures'
NPMRC_CONTENT "node-linker=hoisted\n" const 'node-linker=hoisted\n'

Async/Await Patterns

Type Function Await Expressions Promise Chains
async-function setup project.globTestFiles(project.vitest.state.getPaths()), fs.readdir(PACKAGES_D... Promise.all, Promise.all
async-function teardown fs.rm(INTEGRATION_TEST_DIR, { recursive: true }) none
async-function getPnpmCatalog fs.readFile( path.join(ROOT_DIR, 'pnpm-workspace.yaml'), { encoding: 'utf-8' ... none
async-function getPnpmWorkspaceContent fs.readFile( path.join(ROOT_DIR, 'pnpm-workspace.yaml'), { encoding: 'utf-8' ... none

Functions

setup(project: TestProject): Promise<void>

Parameters:

  • project TestProject

Returns: Promise<void>

Calls:

  • ( await project.globTestFiles(project.vitest.state.getPaths()) ).testFiles.map
  • project.globTestFiles
  • project.vitest.state.getPaths
  • path.basename
  • fs.readdir
  • fs.mkdir
  • Object.fromEntries
  • `( await Promise.all( PACKAGES.map(async ({ name: pkg }) => { const packageDir = path.join(PACKAGES_DIR, pkg); const packagePath = path.join(packageDir, 'package.json');
      try {
        if (!(await fs.lstat(packagePath)).isFile()) {
          return;
        }
      } catch {
        return;
      }
    
      const packageJson: PackageJSON = (
        await import(pathToFileURL(packagePath).href, {
          with: { type: 'json' },
        })
      ).default;
    
      if ('private' in packageJson && packageJson.private === true) {
        return;
      }
    
      const result = await execFile('npm', ['pack', packageDir], {
        cwd: TAR_FOLDER,
        encoding: 'utf-8',
        shell: true,
      });
    
      if (typeof result.stdout !== 'string') {
        return;
      }
    
      const stdoutLines = result.stdout.trim().split('\n');
      const tarball = stdoutLines[stdoutLines.length - 1];
    
      return [
        packageJson.name,
        `file:${path.join(TAR_FOLDER, tarball)}`,
      ] as const;
    }),
    

    ) ).filter-Promise.all-PACKAGES.map-path.join-(await fs.lstat(packagePath)).isFile-fs.lstat-complex_call_2367-pathToFileURL (from node:url)-execFile-result.stdout.trim().split-getPnpmCatalog-getPnpmWorkspaceContent-fs.mkdtemp-fs.writeFile-JSON.stringify-testFileBaseNames.map-complex_call_4932-fs.cp-console.error-console.log-fs.rm`

Internal Comments:

// Ensure everything uses the locally packed versions instead of the NPM versions (x2)
// We install the tarballs here once so that pnpm can cache them globally. (x2)
// This solves 2 problems: (x2)
// 1. Tests can be run concurrently because they won't be trying to install (x2)
//    the same tarballs at the same time. (x2)
// 2. Installing the tarballs for each test becomes much faster as pnpm can (x2)
//    reuse them from its global content-addressable store. (x2)

Code
async (project: TestProject): Promise<void> => {
  const testFileBaseNames = (
    await project.globTestFiles(project.vitest.state.getPaths())
  ).testFiles.map(testFilePath => path.basename(testFilePath, '.test.ts'));

  const PACKAGES = await fs.readdir(PACKAGES_DIR, {
    encoding: 'utf-8',
    withFileTypes: true,
  });

  await fs.mkdir(FIXTURES_DESTINATION_DIR, { recursive: true });

  await fs.mkdir(TAR_FOLDER, { recursive: true });

  const tseslintPackages = Object.fromEntries(
    (
      await Promise.all(
        PACKAGES.map(async ({ name: pkg }) => {
          const packageDir = path.join(PACKAGES_DIR, pkg);
          const packagePath = path.join(packageDir, 'package.json');

          try {
            if (!(await fs.lstat(packagePath)).isFile()) {
              return;
            }
          } catch {
            return;
          }

          const packageJson: PackageJSON = (
            await import(pathToFileURL(packagePath).href, {
              with: { type: 'json' },
            })
          ).default;

          if ('private' in packageJson && packageJson.private === true) {
            return;
          }

          const result = await execFile('npm', ['pack', packageDir], {
            cwd: TAR_FOLDER,
            encoding: 'utf-8',
            shell: true,
          });

          if (typeof result.stdout !== 'string') {
            return;
          }

          const stdoutLines = result.stdout.trim().split('\n');
          const tarball = stdoutLines[stdoutLines.length - 1];

          return [
            packageJson.name,
            `file:${path.join(TAR_FOLDER, tarball)}`,
          ] as const;
        }),
      )
    ).filter(e => e != null),
  );

  const PNPM_CATALOG = await getPnpmCatalog();

  const PNPM_WORKSPACE_CONTENT = await getPnpmWorkspaceContent({
    // Ensure everything uses the locally packed versions instead of the NPM versions
    overrides: tseslintPackages,
  });

  const BASE_DEPENDENCIES: PackageJSON['devDependencies'] = {
    ...tseslintPackages,
    eslint: PNPM_CATALOG.eslint,
    typescript: PNPM_CATALOG.typescript,
    vitest: PNPM_CATALOG.vitest,
  };

  const temp = await fs.mkdtemp(path.join(INTEGRATION_TEST_DIR, 'temp'), {
    encoding: 'utf-8',
  });

  await fs.writeFile(
    path.join(temp, 'package.json'),
    JSON.stringify(
      {
        devDependencies: BASE_DEPENDENCIES,
        packageManager: rootPackageJson.packageManager,
        private: true,
      },
      null,
      2,
    ),
    { encoding: 'utf-8' },
  );

  await fs.writeFile(path.join(temp, '.npmrc'), NPMRC_CONTENT, {
    encoding: 'utf-8',
  });

  await fs.writeFile(
    path.join(temp, 'pnpm-workspace.yaml'),
    PNPM_WORKSPACE_CONTENT,
    { encoding: 'utf-8' },
  );

  // We install the tarballs here once so that pnpm can cache them globally.
  // This solves 2 problems:
  // 1. Tests can be run concurrently because they won't be trying to install
  //    the same tarballs at the same time.
  // 2. Installing the tarballs for each test becomes much faster as pnpm can
  //    reuse them from its global content-addressable store.
  await execFile('pnpm', ['install', '--no-frozen-lockfile'], {
    cwd: temp,
    shell: true,
  });

  await Promise.all(
    testFileBaseNames.map(async fixture => {
      const testFolder = path.join(FIXTURES_DESTINATION_DIR, fixture);

      const fixtureDir = path.join(FIXTURES_DIR, fixture);

      const fixturePackageJson: PackageJSON = (
        await import(
          pathToFileURL(path.join(fixtureDir, 'package.json')).href,
          { with: { type: 'json' } }
        )
      ).default;

      await fs.cp(fixtureDir, testFolder, { recursive: true });

      await fs.writeFile(
        path.join(testFolder, 'package.json'),
        JSON.stringify(
          {
            private: true,
            ...fixturePackageJson,
            devDependencies: {
              ...BASE_DEPENDENCIES,
              ...fixturePackageJson.devDependencies,
            },

            packageManager: rootPackageJson.packageManager,
          },
          null,
          2,
        ),
        { encoding: 'utf-8' },
      );

      await fs.writeFile(path.join(testFolder, '.npmrc'), NPMRC_CONTENT, {
        encoding: 'utf-8',
      });

      await fs.writeFile(
        path.join(testFolder, 'pnpm-workspace.yaml'),
        PNPM_WORKSPACE_CONTENT,
        { encoding: 'utf-8' },
      );

      const { stderr, stdout } = await execFile(
        'pnpm',
        ['install', '--no-frozen-lockfile'],
        {
          cwd: testFolder,
          shell: true,
        },
      );

      if (stderr) {
        console.error(stderr);

        if (stdout) {
          console.log(stdout);
        }
      }
    }),
  );

  await fs.rm(temp, { recursive: true });

  console.log('Finished packing local packages.');
}

teardown(): Promise<void>

Returns: Promise<void>

Calls:

  • fs.rm
Code
async (): Promise<void> => {
  if (process.env.KEEP_INTEGRATION_TEST_DIR !== 'true') {
    await fs.rm(INTEGRATION_TEST_DIR, { recursive: true });
  }
}

getPnpmCatalog(): Promise<Record<string, string>>

Returns: Promise<Record<string, string>>

Calls:

  • fs.readFile
  • path.join
  • yaml.parse
Code
async function getPnpmCatalog() {
  const pnpmWorkspace = await fs.readFile(
    path.join(ROOT_DIR, 'pnpm-workspace.yaml'),
    { encoding: 'utf-8' },
  );

  const parsed: PnpmWorkspace = yaml.parse(pnpmWorkspace);

  const expectedPackages = ['eslint', 'typescript', 'vitest'];

  for (const packageName of expectedPackages) {
    if (!(packageName in parsed.catalog)) {
      throw new Error(`Package ${packageName} not found in pnpm catalog`);
    }
  }

  return parsed.catalog;
}

getPnpmWorkspaceContent({ overrides, }: { overrides: Record<string, string>; }): Promise<string>

Parameters:

  • { overrides, } { overrides: Record<string, string>; }

Returns: Promise<string>

Calls:

  • fs.readFile
  • path.join
  • yaml.parse
  • yaml.stringify

Internal Comments:

// the ts7 fixture installs typescript releases newer than the root (x4)
// minimumReleaseAge allows; `@typescript/*` covers TS 7's native binaries (x4)

Code
async function getPnpmWorkspaceContent({
  overrides,
}: {
  overrides: Record<string, string>;
}): Promise<string> {
  const pnpmWorkspace = await fs.readFile(
    path.join(ROOT_DIR, 'pnpm-workspace.yaml'),
    { encoding: 'utf-8' },
  );

  const parsed = yaml.parse(pnpmWorkspace) as Record<string, unknown>;

  delete parsed.catalog;
  delete parsed.packages;

  parsed.overrides = overrides;

  // the ts7 fixture installs typescript releases newer than the root
  // minimumReleaseAge allows; `@typescript/*` covers TS 7's native binaries
  parsed.minimumReleaseAgeExclude = ['typescript', '@typescript/*'];

  return yaml.stringify(parsed);
}

Interfaces

PackageJSON

Interface Code
interface PackageJSON {
  devDependencies: Record<string, string>;
  name: string;
  private?: boolean;
}

Properties

Name Type Optional Description
devDependencies Record<string, string> not shown
name string not shown
private boolean not shown

PnpmWorkspace

Interface Code
interface PnpmWorkspace {
  catalog: Record<string, string>;
}

Properties

Name Type Optional Description
catalog Record<string, string> not shown

Generated by Syntax Scribe