import ui from '../lib/ui';
import Command from '../lib/command';
import forIn from 'lodash/forIn';
import padEnd from 'lodash/padEnd';

export default class RootCommand extends Command {

  static hidden = true;

  flags = {
    version: {
      description: `
        Print the version of Denali used in the current project, or the global
        version if invoked outside a project
      `,
      type: Boolean
    },
    help: {
      description: 'Show this help message',
      type: Boolean
    }
  };

  run({ params, flags }, argTokens, commands) {
    if (flags.version) {
      this.printVersion();
    } else {
      this.printHelp(commands);
    }
  }

  printVersion() {
    let pkg = require('./package.json');
    ui.info(`denali: ${ pkg.dependencies.denali }`);
    ui.info(`node: ${ process.version }`);
  }

  printHelp(commands) {
    ui.info(`
usage: denali [command] [args]

Available commands:
`.trim());

    let pad = Object.keys(commands).reduce((longest, name) => Math.max(longest, name.length), 0);

    forIn(commands, (CommandClass, name) => {
      if (CommandClass.hidden !== true) {
        ui.info(`  ${ padEnd(name, pad) }   ${ CommandClass.description }`);
      }
    });
  }

}

@@ -1,12 +1,68 @@
+import path from 'path';%0Aimport resolve from 'resolve';%0A
 import ui fr
@@ -77,16 +77,16 @@
 ib/ui';%0A
-
 import C
@@ -184,16 +184,66 @@
 padEnd';
+%0Aimport tryRequire from '../../utils/try-require';
 %0A%0Aexport
@@ -797,23 +797,148 @@
 let 
-pkg = require('
+localPkg;%0A    try %7B%0A      let localDir = resolve.sync('denali', %7B basedir: process.cwd() %7D);%0A      localPkg = require(path.join(localDir, '.
 ./pa
@@ -953,57 +953,245 @@
 on')
+)
 ;%0A    
-ui.info(%60denali: $%7B pkg.dependencies.denali
+%7D catch (e) %7B /* ignore failed lookups */ %7D%0A    let globalPkg = require('../../../package.json');%0A    if (localPkg) %7B%0A      ui.info(%60denali (local): $%7B localPkg.version %7D%60);%0A    %7D%0A    ui.info(%60denali (global): $%7B globalPkg.version
  %7D%60)

Fix local and global denali version