Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: You can now use web-ext programatically, e.g. webExt.cmd.run() #1028

Merged
merged 3 commits into from Aug 4, 2017

Conversation

kumar303
Copy link
Contributor

@kumar303 kumar303 commented Aug 3, 2017

Fixes #683

This only supports minimal programmatic use. You can execute raw functions without any argument validation, like:

import webExt from 'web-ext';

webExt.cmd.run({shouldExitProgram: false, firefox: '...', sourceDir: '...'})
  .then((extensionRunner) => {
    console.log(extensionRunner);
    // extensionRunner.reloadAllExtensions();
    // extensionRunner.exit();
  });

One would have to set up verbose logging manually:

webExt.util.logger.consoleStream.makeVerbose();
webExt.cmd.run({shouldExitProgram: false, firefox: '...', sourceDir: '...'});

You can also disable the use of standard input:

webExt.cmd.run({shouldExitProgram: false, noInput: true});

Here is an example of disabling manifest validation if you want to work with an add-on that's not a WebExtension:

webExt.cmd.run({shouldExitProgram: false}, {
  getValidatedManifest: () => ({
    name: 'auto-generated-name',
    version: '1.0.0',
  }),
});

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling fbba629 on kumar303:js-api-iss683 into e48ac75 on mozilla:master.

@kumar303 kumar303 requested a review from rpl August 3, 2017 20:54
@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 5200ea8 on kumar303:js-api-iss683 into e48ac75 on mozilla:master.

Copy link
Contributor

@chrmod chrmod left a comment

Choose a reason for hiding this comment

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

not sure why the logger is needed. should there be any doc for it?

@kumar303
Copy link
Contributor Author

kumar303 commented Aug 3, 2017

not sure why the logger is needed.

I was thinking that the caller may want to have some control over the log output. The could also replace webExt.util.logger.consoleStream.write with a no-op if desired.

should there be any doc for it?

Yeah, definitely. The way we do it is once the issue lands and web-ext gets deployed with the change, we document it on MDN.

@kumar303
Copy link
Contributor Author

kumar303 commented Aug 3, 2017

... although, hmm, maybe the README should have some docs since this usage is more developer focused.

@chrmod
Copy link
Contributor

chrmod commented Aug 3, 2017

if there would be a way to silence/capture the logs through logger object, it is definitely very useful for embedding purposes.

Dev oriented docs could be published at readthedocs, what do you think?

@kumar303
Copy link
Contributor Author

kumar303 commented Aug 3, 2017

Dev oriented docs could be published at readthedocs, what do you think?

I don't want to maintain and manage another service. I think we can try it in our current documentation and see how hard it is.


export {main};
// This only exposes util/logger so far.
// Do we need anything else?
Copy link
Member

Choose a reason for hiding this comment

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

@kumar303 👍 Nice! This is pretty neat!

The change seems ok to me, but I'm wondering if we should make some tweak to the defaultReloadStrategy, mostly to make the caller of the API able to disable or customize the "interactive" behavior (e.g. reading from stdin when used as a library doesn't seem a great idea, it is probably something that the caller may want to easily disable and maybe manage with an API object which allow it to know when a change has been detected, trigger the addon reload and exit the runner).

What do you think?

@kumar303
Copy link
Contributor Author

kumar303 commented Aug 4, 2017

Thanks @rpl , good idea about disabling input. Could you take another look? I added noInput as a global param to all commands but I didn't hook it up in yargs yet. We could do that in a separate patch so that we have a global --no-input option.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling df599b7 on kumar303:js-api-iss683 into e48ac75 on mozilla:master.

@rpl
Copy link
Member

rpl commented Aug 4, 2017

@kumar303 👍 looks great

Let's file the following as followups:

  • expose noInput through yargs (maybe also aliased to '--no-interactive'?)
  • return a useful API object from cmd.run (e.g. an object which provides a reload() method which reloads the extension which rejects if the reload fails, with the same error logged in the console, and an exit() method which exits the managed Firefox instance and the watcher)

@kumar303
Copy link
Contributor Author

kumar303 commented Aug 4, 2017

return a useful API object from cmd.run

It's currently returning extensionRunner which already does what you're proposing, right? I see methods like reloadAllExtensions() and exit().

@kumar303
Copy link
Contributor Author

kumar303 commented Aug 4, 2017

Feature request for --no-input: #1030

@kumar303 kumar303 merged commit c432f23 into mozilla:master Aug 4, 2017
@kumar303 kumar303 deleted the js-api-iss683 branch August 4, 2017 15:38
@rpl
Copy link
Member

rpl commented Aug 4, 2017

It's currently returning extensionRunner which already does what you're proposing, right? I see methods like reloadAllExtensions() and exit().

currently we are returning firefoxApp, which was what we used to return before the refactoring,
but I agree that returning the extensionRunner would do it.

It seems to be what #1027 is aiming to, though, and so there is no need to file another issue for it.

Rahmeen14 added a commit to Rahmeen14/web-ext that referenced this pull request Oct 19, 2017
Rahmeen14 added a commit to Rahmeen14/web-ext that referenced this pull request Oct 19, 2017
Rahmeen14 added a commit to Rahmeen14/web-ext that referenced this pull request Oct 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants