import fs from 'fs';
import path from 'path';

function findRecursive(directory) {
  if (directory === '/') {
    throw new Error('No project root found, looking for a directory with a package.json file.');
  }

  const pathToPackageJson = path.join(directory, 'package.json');

  if (fs.existsSync(pathToPackageJson)) {
    return directory;
  }

  return findRecursive(path.dirname(directory));
}

function makeAbsolute(pathToFile) {
  if (pathToFile.startsWith('/')) {
    return pathToFile;
  }

  return path.join(process.cwd(), pathToFile);
}

export default function findProjectRoot(pathToFile) {
  return findRecursive(path.dirname(makeAbsolute(pathToFile)));
}

import fs from 'fs';
import path from 'path';

function findRecursive(directory) {
  if (directory === '/') {
    throw new Error('No project root found, looking for a directory with a package.json file.');
  }

  const pathToPackageJson = path.join(directory, 'package.json');

  if (fs.existsSync(pathToPackageJson)) {
    return directory;
  }

  return findRecursive(path.dirname(directory));
}

function makeAbsolute(pathToFile) {
  if (path.isAbsolute(pathToFile)) {
    return pathToFile;
  }

  return path.join(process.cwd(), pathToFile);
}

export default function findProjectRoot(pathToFile) {
  return findRecursive(path.dirname(makeAbsolute(pathToFile)));
}

Use path.isAbsolute for Windows compatibility
Use path.isAbsolute for Windows compatibility

Replaced `pathToFile.startsWith('/')` with `path.isAbsolute`

Closes #397