-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds run command -> install and launch extension
- Loading branch information
Showing
14 changed files
with
633 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* @flow */ | ||
import buildExtension from './build'; | ||
import {ProgramOptions} from '../program'; | ||
import * as defaultFirefox from '../firefox'; | ||
import {withTempDir} from '../util/temp-dir'; | ||
import getValidatedManifest from '../util/manifest'; | ||
|
||
|
||
export default function run( | ||
{sourceDir}: ProgramOptions, | ||
{firefox=defaultFirefox}: Object = {}): Promise { | ||
|
||
console.log(`Running web extension from ${sourceDir}`); | ||
|
||
return getValidatedManifest(sourceDir) | ||
.then((manifestData) => withTempDir( | ||
(tmpDir) => | ||
Promise.all([ | ||
buildExtension({sourceDir, buildDir: tmpDir.path()}, | ||
{manifestData}), | ||
firefox.createProfile(), | ||
]) | ||
.then((result) => { | ||
let [buildResult, profile] = result; | ||
return firefox.installExtension( | ||
{ | ||
manifestData, | ||
extensionPath: buildResult.extensionPath, | ||
profile, | ||
}) | ||
.then(() => profile); | ||
}) | ||
.then((profile) => firefox.run(profile)) | ||
)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* @flow */ | ||
import nodeFs from 'fs'; | ||
import path from 'path'; | ||
import defaultFxRunner from 'fx-runner/lib/run'; | ||
import FirefoxProfile from 'firefox-profile'; | ||
import streamToPromise from 'stream-to-promise'; | ||
|
||
import * as fs from '../util/promised-fs'; | ||
import {onlyErrorsWithCode, WebExtError} from '../errors'; | ||
import {getPrefs as defaultPrefGetter} from './preferences'; | ||
|
||
|
||
export const defaultFirefoxEnv = { | ||
XPCOM_DEBUG_BREAK: 'stack', | ||
NS_TRACE_MALLOC_DISABLE_STACKS: '1', | ||
}; | ||
|
||
export function run( | ||
profile: FirefoxProfile, {fxRunner=defaultFxRunner}: Object = {}): Promise { | ||
|
||
console.log(`Running Firefox with profile at ${profile.path()}`); | ||
return fxRunner( | ||
{ | ||
'binary': null, | ||
'binary-args': null, | ||
'no-remote': true, | ||
'foreground': true, | ||
'profile': profile.path(), | ||
'env': { | ||
...process.env, | ||
...defaultFirefoxEnv, | ||
}, | ||
'verbose': true, | ||
}) | ||
.then((results) => { | ||
return new Promise((resolve) => { | ||
let firefox = results.process; | ||
|
||
console.log(`Executing Firefox binary: ${results.binary}`); | ||
console.log(`Executing Firefox with args: ${results.args.join(' ')}`); | ||
|
||
firefox.on('error', (error) => { | ||
// TODO: show a nice error when it can't find Firefox. | ||
// if (/No such file/.test(err) || err.code === 'ENOENT') { | ||
console.log(`Firefox error: ${error}`); | ||
throw error; | ||
}); | ||
|
||
firefox.stderr.on('data', (data) => { | ||
console.log(`stdout: ${data.toString().trim()}`); | ||
}); | ||
|
||
firefox.stdout.on('data', function(data) { | ||
console.log(`stderr: ${data.toString().trim()}`); | ||
}); | ||
|
||
firefox.on('close', () => { | ||
console.log('Firefox closed'); | ||
resolve(); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
|
||
export function createProfile( | ||
app: string = 'firefox', | ||
{getPrefs=defaultPrefGetter}: Object = {}): Promise { | ||
|
||
return new Promise((resolve) => { | ||
// The profile is created in a self-destructing temp dir. | ||
// TODO: add option to copy a profile. | ||
// https://github.com/mozilla/web-ext/issues/69 | ||
let profile = new FirefoxProfile(); | ||
|
||
// Set default preferences. | ||
// TODO: support custom preferences. | ||
// https://github.com/mozilla/web-ext/issues/88 | ||
let prefs = getPrefs(app); | ||
Object.keys(prefs).forEach((pref) => { | ||
profile.setPreference(pref, prefs[pref]); | ||
}); | ||
profile.updatePreferences(); | ||
|
||
resolve(profile); | ||
}); | ||
} | ||
|
||
|
||
class InstallationConfig { | ||
manifestData: Object; | ||
profile: FirefoxProfile; | ||
extensionPath: string; | ||
} | ||
|
||
export function installExtension( | ||
{manifestData, profile, extensionPath}: InstallationConfig): Promise { | ||
|
||
// This more or less follows | ||
// https://github.com/saadtazi/firefox-profile-js/blob/master/lib/firefox_profile.js#L531 | ||
// (which is broken for web extensions). | ||
// TODO: maybe uplift a patch that supports web extensions instead? | ||
|
||
return new Promise( | ||
(resolve) => { | ||
if (!profile.extensionsDir) { | ||
throw new WebExtError('profile.extensionsDir was unexpectedly empty'); | ||
} | ||
resolve(fs.stat(profile.extensionsDir)); | ||
}) | ||
.catch(onlyErrorsWithCode('ENOENT', () => { | ||
console.log(`Creating extensions directory: ${profile.extensionsDir}`); | ||
return fs.mkdir(profile.extensionsDir); | ||
})) | ||
.then(() => { | ||
let readStream = nodeFs.createReadStream(extensionPath); | ||
let id = manifestData.applications.gecko.id; | ||
|
||
// TODO: also support copying a directory of code to this | ||
// destination. That is, to name the directory ${id}. | ||
// https://github.com/mozilla/web-ext/issues/70 | ||
let destPath = path.join(profile.extensionsDir, `${id}.xpi`); | ||
let writeStream = nodeFs.createWriteStream(destPath); | ||
|
||
console.log(`Copying ${extensionPath} to ${destPath}`); | ||
readStream.pipe(writeStream); | ||
|
||
return Promise.all([ | ||
streamToPromise(readStream), | ||
streamToPromise(writeStream), | ||
]); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* @flow */ | ||
import {WebExtError} from '../errors'; | ||
|
||
|
||
export function getPrefs(app: string = 'firefox'): Object { | ||
let appPrefs = prefs[app]; | ||
if (!appPrefs) { | ||
throw new WebExtError(`Unsupported application: ${app}`); | ||
} | ||
return { | ||
...prefs.common, | ||
...appPrefs, | ||
}; | ||
} | ||
|
||
|
||
var prefs = {}; | ||
|
||
prefs.common = { | ||
// Allow debug output via dump to be printed to the system console | ||
'browser.dom.window.dump.enabled': true, | ||
// Warn about possibly incorrect code. | ||
'javascript.options.strict': true, | ||
'javascript.options.showInConsole': true, | ||
|
||
// Allow remote connections to the debugger. | ||
'devtools.debugger.remote-enabled' : true, | ||
|
||
// Turn off platform logging because it is a lot of info. | ||
'extensions.logging.enabled': false, | ||
|
||
// Disable extension updates and notifications. | ||
'extensions.checkCompatibility.nightly' : false, | ||
'extensions.update.enabled' : false, | ||
'extensions.update.notifyUser' : false, | ||
|
||
// From: | ||
// http://hg.mozilla.org/mozilla-central/file/1dd81c324ac7/build/automation.py.in//l372 | ||
// Only load extensions from the application and user profile. | ||
// AddonManager.SCOPE_PROFILE + AddonManager.SCOPE_APPLICATION | ||
'extensions.enabledScopes' : 5, | ||
// Disable metadata caching for installed add-ons by default. | ||
'extensions.getAddons.cache.enabled' : false, | ||
// Disable intalling any distribution add-ons. | ||
'extensions.installDistroAddons' : false, | ||
// Allow installing extensions dropped into the profile folder. | ||
'extensions.autoDisableScopes' : 10, | ||
|
||
// Disable app update. | ||
'app.update.enabled' : false, | ||
|
||
// Point update checks to a nonexistent local URL for fast failures. | ||
'extensions.update.url': 'http://localhost/extensions-dummy/updateURL', | ||
'extensions.blocklist.url': | ||
'http://localhost/extensions-dummy/blocklistURL', | ||
|
||
// Make sure opening about:addons won't hit the network. | ||
'extensions.webservice.discoverURL' : | ||
'http://localhost/extensions-dummy/discoveryURL', | ||
|
||
// Allow unsigned add-ons. | ||
'xpinstall.signatures.required' : false, | ||
}; | ||
|
||
// Prefs specific to Firefox for Android. | ||
prefs.fennec = { | ||
'browser.console.showInPanel': true, | ||
'browser.firstrun.show.uidiscovery': false, | ||
}; | ||
|
||
// Prefs specific to Firefox for desktop. | ||
prefs.firefox = { | ||
'browser.startup.homepage' : 'about:blank', | ||
'startup.homepage_welcome_url' : 'about:blank', | ||
'startup.homepage_welcome_url.additional' : '', | ||
'devtools.errorconsole.enabled' : true, | ||
'devtools.chrome.enabled' : true, | ||
|
||
// From: | ||
// http://hg.mozilla.org/mozilla-central/file/1dd81c324ac7/build/automation.py.in//l388 | ||
// Make url-classifier updates so rare that they won't affect tests. | ||
'urlclassifier.updateinterval' : 172800, | ||
// Point the url-classifier to a nonexistent local URL for fast failures. | ||
'browser.safebrowsing.provider.0.gethashURL' : | ||
'http://localhost/safebrowsing-dummy/gethash', | ||
'browser.safebrowsing.provider.0.keyURL' : | ||
'http://localhost/safebrowsing-dummy/newkey', | ||
'browser.safebrowsing.provider.0.updateURL' : | ||
'http://localhost/safebrowsing-dummy/update', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.