import path from 'path';
import {statNoException} from '../promise-array';
import BuildDiscoverBase from '../build-discover-base';
import {findActualExecutable} from 'spawn-rx';

const d = require('debug')('surf:build-discover-npm');

export default class DangerBuildDiscoverer extends BuildDiscoverBase {
  constructor(rootDir) {
    super(rootDir);
    
    // Danger runs concurrently with other builds
    this.shouldAlwaysRun = true;
  }

  async getAffinityForRootDir() {
    let dangerFile = path.join(this.rootDir, 'Dangerfile');
    let exists = await statNoException(dangerFile);

    if (process.env.SURF_DISABLE_DANGER) return 0;
    
    // If we can't find Ruby or Bundler in PATH, bail
    if (!['ruby', 'bundler'].find((x) => findActualExecutable(x, []).cmd !== x)) {
      d(`Can't find Ruby and Bundler in PATH, bailing`);
      return 0;
    }
    
    if (exists) { d(`Found Dangerfile at ${dangerFile}`); }
    return exists ? 100 : 0;
  }

  async getBuildCommand() {
    let cmds = [
      { cmd: 'bundle', args: ['exec', 'danger']}
    ];
    
    if (!process.env.SURF_BUILD_NAME) {
      cmds[0].args.push('local');
    }
  
    if (!process.env.DANGER_GITHUB_API_TOKEN) {
      process.env.DANGER_GITHUB_API_TOKEN = process.env.GITHUB_TOKEN;
    }
  
    return {cmds};
  }
}

import path from 'path';
import {statNoException} from '../promise-array';
import BuildDiscoverBase from '../build-discover-base';
import {findActualExecutable} from 'spawn-rx';

const d = require('debug')('surf:build-discover-npm');

export default class DangerBuildDiscoverer extends BuildDiscoverBase {
  constructor(rootDir) {
    super(rootDir);

    // Danger runs concurrently with other builds
    this.shouldAlwaysRun = true;
  }

  async getAffinityForRootDir() {
    let dangerFile = path.join(this.rootDir, 'Dangerfile');
    let exists = await statNoException(dangerFile);

    if (process.env.SURF_DISABLE_DANGER) return 0;
    if (!exists) return;

    // If we can't find Ruby or Bundler in PATH, bail
    if (!['ruby', 'bundle'].every((x) => findActualExecutable(x, []).cmd !== x)) {
      console.log(`A Dangerfile exists but can't find Ruby and Bundler in PATH, skipping`);
      return 0;
    }

    d(`Found Dangerfile at ${dangerFile}`);
    return exists ? 100 : 0;
  }

  async getBuildCommand() {
    let cmds = [
      { cmd: 'bundle', args: ['exec', 'danger']}
    ];
    
    if (!process.env.SURF_BUILD_NAME) {
      cmds[0].args.push('local');
    }
  
    if (!process.env.DANGER_GITHUB_API_TOKEN) {
      process.env.DANGER_GITHUB_API_TOKEN = process.env.GITHUB_TOKEN;
    }
  
    return {cmds};
  }
}

Fix up detecting if Bundler is installed