Skip to content

refactor: expose plugin utils & add initial tests#484

Merged
ariporad merged 2 commits intomasterfrom
refactor-plugin-utils
Jul 27, 2016
Merged

refactor: expose plugin utils & add initial tests#484
ariporad merged 2 commits intomasterfrom
refactor-plugin-utils

Conversation

@nfischer
Copy link
Copy Markdown
Member

After more experimentation, I realized that #482 wasn't a perfect approach, but that this would be a better one. Even after #482, shell.error() was actually broken. This fixes that issue.

This also starts differentiating internal implementations (src/common.js) from exposed parts of the plugin API (plugin.js). This is of course still far from a complete plugin API, but this should be enough to make simple plugins (and still get benefits like globbing, shelljs-style error signaling, etc.).

All implementations are still within src/common.js, and internal commands still rely on that directly, but external plugins are intended to use var plugin = require('shelljs/plugin') to get functions like plugin.register(). This also makes it easy to modify the plugin API without breaking ShellJS commands.

I also started unit tests for the plugin API. This creates one sample plugin (with all the bells and whistles like automatic option parsing) and then makes sure it works properly. We may also want to create a simpler plugin (i.e. no automatic option-parsing, no wrapping the return value, etc.) and test that.

If you have any suggestions on naming conventions, or think there's a better approach to this issue, let me know. Like I said, this is still far from complete, but suggestions are welcome.

// http://github.com/arturadib/shelljs
//

var commonPath = './src/common';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why are we deleting this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This reverts #482, which I realized was not the best approach. We could leave #482 in, but it's just extra-complicated code once this gets merged, so I think it's best to revert.

@nfischer
Copy link
Copy Markdown
Member Author

@ariporad I just fixed the issue with node v0.10-12. This should be good now.

@nfischer
Copy link
Copy Markdown
Member Author

For reference, the bug I found with the approach in #482 is that there are two different instances of common.state: one that the plugin has access to and modifies, and one that the ShellJS instance has access to.

A plugin might signal an error using plugin.error('some message'), however any calls to shell.error() after that would not see that error message, since shell.error() accesses one common.state and plugin.error() modified a different one.

@ariporad
Copy link
Copy Markdown
Contributor

LGTM!

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants