Skip to content

Conversation

ariporad
Copy link
Contributor

@ariporad ariporad commented May 5, 2016

No description provided.

import shell from 'shelljs';

const commandList = Object.keys(shell)
.filter(cmd => typeof shell[cmd] === 'function' && cmd !== 'ShellString');
Copy link
Contributor Author

@ariporad ariporad May 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, this would be a part of shelljs itself.

Also, it would be nice if each command had a .help property which contained documentation. Maybe we could generate the documentation from that.

Or... (WARNING: Crazy Ahead):

Currently the docs are like this:

//@ Blah Blah Blah
function _blah() {
    do_something();
}

What if we made them like this:

function _blah() {
    //@ Blah Blah Blah
    do_something();
}

AFAIK, it would work entirely the same, documentup doesn't actually care about the function, but then we could parse it like this:

var lines = shell[cmd].toString().split('\n').slice(1, -1); // Remove the first and last lines.
var docLines = [];
for (var i = 0; i < lines.length; ++i) {
    if (lines[i].strip().slice(0, 3) === '//@') {
        docLines.push(lines[i].strip().slice(3));
    } else {
        break;
    }
}
shell[cmd].help = docLines.join('\n');

@nfischer
Copy link
Member

nfischer commented May 5, 2016

@ariporad Can you add unit tests for this as well?

Also, the output looks off on my Linux box. I only see one character per line, which isn't ideal. Here's a screenshot of running shx help:

screenshot from 2016-05-05 16-45-37

@levithomason
Copy link
Contributor

levithomason commented May 5, 2016

I really think we should consider yargs here. It has solved all these issues and is well maintained. It allows defining commands, help, descriptions, examples, etc. It also a robust feature for specifying which args are required on which commands.

The output of help is also borked right now:
[redacted obnoxious output]

@ariporad
Copy link
Contributor Author

ariporad commented May 5, 2016

@levithomason: The problem with yargs is that we have to more formally define the commands, vs. just calling shelljs.

Also, I fixed the output.

const commandList = Object.keys(shell)
.filter(cmd => typeof shell[cmd] === 'function' && cmd !== 'ShellString');

const help = [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm totally open to help with the actual help message.

Copy link
Contributor

@levithomason levithomason May 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use a template string instead, they are multi-line:

const help = `
  shx: A wrapper for shelljs UNIX commands.

  Usage: shx <command> [options]

  Example:

      $ shx ls .
      foo.txt
      bar.txt
      baz.js

      $ shx rm -rf *.txt
      $ shx ls .
      baz.js

  Commands:

${commandList.map(cmd => `    - ${cmd}`).join('\n')}
`;

I added a Usage: part in there, feel free to rework this.

@levithomason
Copy link
Contributor

I get the same output. Also, for starters, we could just loop through the shelljs commands and add the relevant parts to the yargs config. As shelljs exposed more info, we could beef that up.

@nfischer
Copy link
Member

nfischer commented May 5, 2016

I don't know if we necessarily need yargs, since we're just trying to output a static string. Doesn't matter much to me though.

@nfischer
Copy link
Member

nfischer commented May 5, 2016

Output definitely needs to be changed though, since it implies that shx cd is allowed (which it isn't).

@levithomason
Copy link
Contributor

Output working, sorry had to rebuild!

@ariporad
Copy link
Contributor Author

ariporad commented May 6, 2016

Should be fixed.

Thanks! Ari
On Thu, May 5, 2016 at 5:02 PM, Levi Thomason notifications@github.com wrote:
Output working, sorry had to rebuild!


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub [https://github.com//pull/49#issuecomment-217314054]

@@ -0,0 +1,29 @@
import shell from 'shelljs';

const badCommands = ['ShellString', 'cd', 'pushd', 'popd', 'dirs'];
Copy link
Contributor

@levithomason levithomason May 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a CMD_BLACKLIST already defined in shx.js. Suggest pulling that and the ERROR_CODES into a config.js or something so it can imported into both shx.js and help.js.

Then you can filter based on the same list.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Thanks! Ari
On Thu, May 5, 2016 at 5:11 PM, Levi Thomason notifications@github.com wrote:
In src/help.js [https://github.com//pull/49#discussion_r62274779] :

@@ -0,0 +1,29 @@
+import shell from 'shelljs';
+
+const badCommands = ['ShellString', 'cd', 'pushd', 'popd', 'dirs'];

There is a CMD_BLACKLIST already defined in shx.js . Suggest pulling that and the ERROR_CODES into a config.js or something so it can imported into both places.

Then you can filter based on the same list.


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub [https://github.com//pull/49/files/c476547d608b00becee7cd1584e944c2d134c034#r62274779]

@levithomason
Copy link
Contributor

Looking good, tested node 4/5/6. Just needs a trailing comma cleaned up in the commands list:

    - chmod
    - touch
    - tempdir
    - error,

@ariporad
Copy link
Contributor Author

ariporad commented May 6, 2016

Done.x

@levithomason
Copy link
Contributor

nit picky so feel free to ignore, but there are two line breaks at the end of the commands list. otherwise, LGTM.

    - tempdir
    - error


[system] in shx/ on add-help 

@levithomason
Copy link
Contributor

levithomason commented May 6, 2016

EDIT submitted before done

Would be nice to print the help after each error in shx as well:

  if (!fnName) {
    console.error('Error: Missing ShellJS command name');
    console.error(help());
    return EXIT_CODES.SHX_ERROR;
  }

  // validate command
  if (typeof shell[fnName] !== 'function') {
    console.error(`Error: Invalid ShellJS command: ${fnName}.`);
    console.error(help());
    return EXIT_CODES.SHX_ERROR;
  } else if (CMD_BLACKLIST.indexOf(fnName) > -1) {
    console.error(`Warning: shx ${fnName} is not supported`);
    console.error(help());
    return EXIT_CODES.SHX_ERROR;
  }

Then, we get helpful errors instead of just errors.

@nfischer
Copy link
Member

nfischer commented May 6, 2016

I think it's most useful to have the full help only after shx and shx someNonexistentCommand. In the case of shx exec (an example blacklisted command) this could have a suggestion to run shx help in addition to the current message.

I like this because it keeps output succinct for simple mistakes (like shx cd), but shows how to get the full verbose info if that's necessary. That's just my thinking.

@levithomason
Copy link
Contributor

@nfischer that is definitely superior. I'd sign off on that.

@nfischer
Copy link
Member

@shelljs/contributors any further work on fixing the broken build/writing tests for this? I may be able to take up some work on this tonight or tomorrow.

@levithomason
Copy link
Contributor

Been slammed here, will try to take a look tonight. No guarantees. If @ariporad is down, I say whoever can push it over the finish line should go for it.

@ariporad
Copy link
Contributor Author

I'll try to work on this later tonight.

Also shows help on `shx nonExistantCommand`
@ariporad
Copy link
Contributor Author

Ok, tests added, and it should print help on shx, shx help, and shx nonexistantcommand. Prints a message to use help on shx exec (or any other blacklisted command).

@levithomason
Copy link
Contributor

Per @nfischer's comment, i think it would be best to not show the full help when missing a command or an invalid command is used, but instead, just show the error plus something like "For more info: shx help"

Other than that, this looks great!

@codecov-io
Copy link

codecov-io commented May 11, 2016

Current coverage is 90.32%

Merging #49 into master will increase coverage by +6.11%

@@             master        #49   diff @@
==========================================
  Files             2          4     +2   
  Lines            19         31    +12   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits             16         28    +12   
  Misses            3          3          
  Partials          0          0          

Powered by Codecov. Last updated by be59e0f...9321db6

@levithomason
Copy link
Contributor

Pulled and tested, LGTM. Going to merge this.

@levithomason levithomason merged commit b893002 into master May 11, 2016
@levithomason levithomason deleted the add-help branch May 11, 2016 18:12
@nfischer
Copy link
Member

Awesome, thanks for updating the tests 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants