Using web-ext in Gulp

I know the web-ext tool can be used from the command line, but I’d like to build it into our Gulp task process. We have some devs that aren’t as familiar with the command line and running node commands. So we would like to stream line the process a bit and give them simple gulp task to run.

I’ve gotten as far as:

var webext = require('web-ext');
webext.main(); 

I looked in the web-ext node directory at the src/program.js file

the .main() function takes

  absolutePackageDir: string,
  {
    getVersion = defaultVersionGetter, commands = defaultCommands, argv,
    runOptions = {},
  }

So I passed an absolute path to the dist/ directory and fiddled around with trying to pass different parameters, but to no avail. I’m not sure how to properly pass different arguments to it such as run, build, sign and their sub arguments such as changing the source directory to dist/ and setting it to run in Firefox Dev Edition and things like that.

Any ideas?

I was able to find the answer in their GitHub issues.

https://github.com/mozilla/web-ext/issues/683#issuecomment-267789917

Using the Node module Yargs to get arguments I added this to Gulp

gulp.task('web-ext:run', function () {
	var args = {shouldExitProgram: false};
	
	// use aliases they're shorter and will map to the application
	// Allows you to specify a particular version of Firefox to run the WebExtension in.
	// The value can be an absolute path to the Firefox executable or it can be an alias string.
	// If this is not specified, it will attempt to run the extension inside the default installation of Firefox on the system.
	args['firefox'] = argv.firefox || 'firefoxdeveloperedition';
	
	// Allows you to specify a base Firefox profile to run the WebExtension in.
	// This is specified as a string containing your profile name or an absolute path to its directory.
	// The profile you specify is copied into a new temporary profile and some settings are added that are required for web-ext to function.
	// If a profile is not specified, it will run the extension using a new temporary profile.
	if(argv.firefoxProfile || argv.p) {
		args['firefox-profile'] = argv.firefoxProfile || argv.p
	}
	
	// Do not automatically reload the extension in the browser as you edit and save source files
	if(argv.noReload) {
		args['no-reload'] = true;
	}
	
	// Pre-install the extension into the profile before starting the browser. 
	// This is a way to support Firefox versions less than 49 since they do not support remote installation. 
	// Specifying this option implies --no-reload
	if(argv.preInstall) {
		args['pre-install'] = true;
	}
	
	if(argv.sourceDir) {
		args['source-dir'] = argv.sourceDir;
	}
	
	if(argv.artifactsDir) {
		args['artifacts-dir'] = argv.artifactsDir;
	}
	
	webext ( 'run', {
		runOptions : args
	} ).catch((error) => console.error('CAUGHT', error));
});