import { execFileSync } from 'child_process'; import fs from 'fs'; import os from 'os'; import path from 'path'; type LocalPackOptions = { projectRoot?: string; outputDir?: string; packageName?: string; version?: string; }; export const createLocalPackageTarball = async ( options: LocalPackOptions = {}, ): Promise => { const projectRoot = options.projectRoot ?? process.cwd(); const outputDir = options.outputDir ?? projectRoot; const packageJsonPath = path.join(projectRoot, 'package.json'); const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')) as { version?: string; }; const version = options.version ?? packageJson.version; if (!version) { throw new Error('Unable to determine package version from package.json.'); } const packageName = options.packageName ?? 'test-uniswap-position-quoter'; const tempDir = fs.mkdtempSync( path.join(os.tmpdir(), 'uniswap-position-quoter-pack-'), ); execFileSync('npm', ['pack', '--silent', '--pack-destination', tempDir], { cwd: projectRoot, stdio: 'pipe', }); const tarballCandidates = fs .readdirSync(tempDir) .filter((fileName) => fileName.endsWith('.tgz')); if (tarballCandidates.length !== 1) { fs.rmSync(tempDir, { recursive: true, force: true }); throw new Error('Unexpected npm pack output. Expected a single .tgz file.'); } const tarballName = `${packageName}-v${version}.tgz`; const sourceTarballPath = path.join(tempDir, tarballCandidates[0]); const outputTarballPath = path.join(outputDir, tarballName); fs.renameSync(sourceTarballPath, outputTarballPath); fs.rmSync(tempDir, { recursive: true, force: true }); return outputTarballPath; }; const run = async (): Promise => { const tarballPath = await createLocalPackageTarball({ projectRoot: process.cwd(), }); process.stdout.write(`${tarballPath}\n`); }; if (require.main === module) { run().catch((error: Error) => { process.stderr.write(`${error.message}\n`); process.exitCode = 1; }); }