All files / sdk/utils projectUtils.ts

90% Statements 9/10
100% Branches 1/1
100% Functions 1/1
90% Lines 9/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 238x 8x           8x 28x   28x 112x   112x 28x     84x          
import * as path from "path";
import * as fs from "fs";
 
/**
 * Finds the directory containing the package.json file by traversing up the directory tree.
 * @returns {string | null} The absolute path to the directory containing package.json, or null if not found.
 */
export function getPackageJsonDir(): string | null {
  let currentDir = __dirname;
 
  while (currentDir !== path.parse(currentDir).root) {
    const packageJsonPath = path.join(currentDir, "package.json");
 
    if (fs.existsSync(packageJsonPath)) {
      return currentDir;
    }
 
    currentDir = path.dirname(currentDir);
  }
 
  return null;
}