text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  const fs = require("then-fs");
  const pathLib = require("path");
  const debugLib = require("debug");
  const chalk_1 = require("chalk");
  const _ = require("lodash");
  const debug = debugLib('snyk');
  const DETECTABLE_FILES = [
      'yarn.lock',
      'package-lock.json',
      'package.json',
      'Gemfile',
      'Gemfile.lock',
      'pom.xml',
      'build.gradle',
      'build.gradle.kts',
      'build.sbt',
      'Pipfile',
      'requirements.txt',
      'Gopkg.lock',
      'vendor/vendor.json',
      'obj/project.assets.json',
      'project.assets.json',
      'packages.config',
      'paket.dependencies',
      'composer.lock',
  ];
  // when file is specified with --file, we look it up here
  const DETECTABLE_PACKAGE_MANAGERS = {
      'Gemfile': 'rubygems',
      'Gemfile.lock': 'rubygems',
      '.gemspec': 'rubygems',
      'package-lock.json': 'npm',
      'pom.xml': 'maven',
      'build.gradle': 'gradle',
      'build.gradle.kts': 'gradle',
      'build.sbt': 'sbt',
      'yarn.lock': 'yarn',
      'package.json': 'npm',
      'Pipfile': 'pip',
      'requirements.txt': 'pip',
      'Gopkg.lock': 'golangdep',
      'vendor.json': 'govendor',
      'project.assets.json': 'nuget',
      'packages.config': 'nuget',
      'project.json': 'nuget',
      'paket.dependencies': 'paket',
      'composer.lock': 'composer',
  };
  function isPathToPackageFile(path) {
      for (const fileName of DETECTABLE_FILES) {
          if (_.endsWith(path, fileName)) {
              return true;
          }
      }
      return false;
  }
  exports.isPathToPackageFile = isPathToPackageFile;
  function detectPackageManager(root, options) {
      // If user specified a package manager let's use it.
      if (options.packageManager) {
          return options.packageManager;
      }
      // The package manager used by a docker container is not known ahead of time
      if (options.docker) {
          return undefined;
      }
      let packageManager;
      let file;
      if (isLocalFolder(root)) {
          if (options.file) {
              if (localFileSuppliedButNotFound(root, options.file)) {
                  throw new Error('Could not find the specified file: ' + options.file +
                      '\nPlease check that it exists and try again.');
              }
              file = options.file;
              packageManager = detectPackageManagerFromFile(file);
          }
          else {
              debug('no file specified. Trying to autodetect in base folder ' + root);
              file = detectPackageFile(root);
              if (file) {
                  packageManager = detectPackageManagerFromFile(file);
              }
          }
      }
      else {
          debug('specified paramater is not a folder, trying to lookup as repo');
          const registry = options.registry || 'npm';
          packageManager = detectPackageManagerFromRegistry(registry);
      }
      if (!packageManager) {
          throw new Error('Could not detect supported target files in ' + root +
              '.\nPlease see our documentation for supported languages and ' +
              'target files: ' +
              chalk_1.default.underline('https://support.snyk.io/getting-started/languages-support') +
              ' and make sure you are in the right directory.');
      }
      return packageManager;
  }
  exports.detectPackageManager = detectPackageManager;
  // User supplied a "local" file, but that file doesn't exist
  function localFileSuppliedButNotFound(root, file) {
      return file && fs.existsSync(root) &&
          !fs.existsSync(pathLib.resolve(root, file));
  }
  function isLocalFolder(root) {
      try {
          return fs.lstatSync(root).isDirectory();
      }
      catch (e) {
          return false;
      }
  }
  function detectPackageFile(root) {
      for (const file of DETECTABLE_FILES) {
          if (fs.existsSync(pathLib.resolve(root, file))) {
              debug('found package file ' + file + ' in ' + root);
              return file;
          }
      }
      debug('no package file found in ' + root);
  }
  exports.detectPackageFile = detectPackageFile;
  function detectPackageManagerFromFile(file) {
      let key = pathLib.basename(file);
      if (/\.gemspec$/.test(key)) {
          key = '.gemspec';
      }
      if (!(key in DETECTABLE_PACKAGE_MANAGERS)) {
          // we throw and error here because the file was specified by the user
          throw new Error('Could not detect package manager for file: ' + file);
      }
      return DETECTABLE_PACKAGE_MANAGERS[key];
  }
  function detectPackageManagerFromRegistry(registry) {
      return registry;
  }
  //# sourceMappingURL=detect.js.map