|
1 | 1 | /* @flow */
|
2 | 2 | import buildExtension from './build';
|
3 | 3 | import * as defaultFirefox from '../firefox';
|
| 4 | +import defaultFirefoxConnector from '../firefox/remote'; |
| 5 | +import {onlyErrorsWithCode} from '../errors'; |
4 | 6 | import {withTempDir} from '../util/temp-dir';
|
5 | 7 | import {createLogger} from '../util/logger';
|
6 | 8 | import getValidatedManifest from '../util/manifest';
|
| 9 | +import defaultSourceWatcher from '../watcher'; |
7 | 10 |
|
8 | 11 | const log = createLogger(__filename);
|
9 | 12 |
|
10 | 13 |
|
| 14 | +export function defaultWatcherCreator( |
| 15 | + {profile, client, sourceDir, artifactsDir, createRunner, |
| 16 | + onSourceChange=defaultSourceWatcher}: Object): Object { |
| 17 | + return onSourceChange({ |
| 18 | + sourceDir, artifactsDir, onChange: () => createRunner( |
| 19 | + (runner) => runner.buildExtension() |
| 20 | + .then((buildResult) => runner.install(buildResult, {profile})) |
| 21 | + .then(() => { |
| 22 | + log.debug('Attempting to reload extension'); |
| 23 | + const addonId = runner.manifestData.applications.gecko.id; |
| 24 | + log.debug(`Reloading add-on ID ${addonId}`); |
| 25 | + return client.reloadAddon(addonId); |
| 26 | + }) |
| 27 | + .catch((error) => { |
| 28 | + log.error(error.stack); |
| 29 | + throw error; |
| 30 | + }) |
| 31 | + ), |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +export function defaultReloadStrategy( |
| 37 | + {firefox, profile, sourceDir, artifactsDir, createRunner}: Object, |
| 38 | + {connectToFirefox=defaultFirefoxConnector, |
| 39 | + maxRetries=25, retryInterval=120, |
| 40 | + createWatcher=defaultWatcherCreator}: Object = {}): Promise { |
| 41 | + var watcher; |
| 42 | + var client; |
| 43 | + var retries = 0; |
| 44 | + |
| 45 | + firefox.on('close', () => { |
| 46 | + if (client) { |
| 47 | + client.disconnect(); |
| 48 | + } |
| 49 | + if (watcher) { |
| 50 | + watcher.close(); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + function establishConnection() { |
| 55 | + return new Promise((resolve, reject) => { |
| 56 | + connectToFirefox() |
| 57 | + .then((connectedClient) => { |
| 58 | + log.debug('Connected to the Firefox debugger'); |
| 59 | + client = connectedClient; |
| 60 | + watcher = createWatcher({ |
| 61 | + profile, client, sourceDir, artifactsDir, createRunner, |
| 62 | + }); |
| 63 | + resolve(); |
| 64 | + }) |
| 65 | + .catch(onlyErrorsWithCode('ECONNREFUSED', (error) => { |
| 66 | + if (retries >= maxRetries) { |
| 67 | + log.debug('Connect to Firefox debugger: too many retries'); |
| 68 | + throw error; |
| 69 | + } else { |
| 70 | + setTimeout(() => { |
| 71 | + retries ++; |
| 72 | + log.debug( |
| 73 | + `Retrying Firefox (${retries}); connection error: ${error}`); |
| 74 | + resolve(establishConnection()); |
| 75 | + }, retryInterval); |
| 76 | + } |
| 77 | + })) |
| 78 | + .catch((error) => { |
| 79 | + log.error(error.stack); |
| 80 | + reject(error); |
| 81 | + }); |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + return establishConnection(); |
| 86 | +} |
| 87 | + |
| 88 | + |
11 | 89 | export default function run(
|
12 |
| - {sourceDir, firefoxBinary, firefoxProfile}: Object, |
13 |
| - {firefox=defaultFirefox}: Object = {}): Promise { |
| 90 | + {sourceDir, artifactsDir, firefoxBinary, firefoxProfile, noReload}: Object, |
| 91 | + {firefox=defaultFirefox, reloadStrategy=defaultReloadStrategy} |
| 92 | + : Object = {}): Promise { |
14 | 93 |
|
15 | 94 | log.info(`Running web extension from ${sourceDir}`);
|
16 | 95 |
|
17 |
| - return getValidatedManifest(sourceDir) |
18 |
| - .then((manifestData) => withTempDir( |
19 |
| - (tmpDir) => |
20 |
| - Promise.all([ |
21 |
| - buildExtension({sourceDir, artifactsDir: tmpDir.path()}, |
22 |
| - {manifestData}), |
23 |
| - new Promise((resolve) => { |
24 |
| - if (firefoxProfile) { |
25 |
| - log.debug(`Copying Firefox profile from ${firefoxProfile}`); |
26 |
| - resolve(firefox.copyProfile(firefoxProfile)); |
27 |
| - } else { |
28 |
| - log.debug('Creating new Firefox profile'); |
29 |
| - resolve(firefox.createProfile()); |
30 |
| - } |
31 |
| - }), |
32 |
| - ]) |
33 |
| - .then((result) => { |
34 |
| - let [buildResult, profile] = result; |
35 |
| - return firefox.installExtension( |
36 |
| - { |
37 |
| - manifestData, |
38 |
| - extensionPath: buildResult.extensionPath, |
39 |
| - profile, |
40 |
| - }) |
41 |
| - .then(() => profile); |
| 96 | + function createRunner(callback) { |
| 97 | + return getValidatedManifest(sourceDir) |
| 98 | + .then((manifestData) => withTempDir( |
| 99 | + (tmpDir) => { |
| 100 | + const runner = new ExtensionRunner({ |
| 101 | + sourceDir, |
| 102 | + firefox, |
| 103 | + firefoxBinary, |
| 104 | + tmpDirPath: tmpDir.path(), |
| 105 | + manifestData, |
| 106 | + firefoxProfile, |
| 107 | + }); |
| 108 | + return callback(runner); |
| 109 | + } |
| 110 | + )); |
| 111 | + } |
| 112 | + |
| 113 | + return createRunner( |
| 114 | + (runner) => runner.buildExtension() |
| 115 | + .then((buildResult) => runner.install(buildResult)) |
| 116 | + .then((profile) => runner.run(profile).then((firefox) => { |
| 117 | + return {firefox, profile}; |
| 118 | + })) |
| 119 | + .then(({firefox, profile}) => { |
| 120 | + if (noReload) { |
| 121 | + log.debug('Extension auto-reloading has been disabled'); |
| 122 | + } else { |
| 123 | + log.debug('Reloading extension when the source changes'); |
| 124 | + reloadStrategy( |
| 125 | + {firefox, profile, sourceDir, artifactsDir, createRunner}); |
| 126 | + } |
| 127 | + return firefox; |
| 128 | + }) |
| 129 | + ); |
| 130 | +} |
| 131 | + |
| 132 | + |
| 133 | +export class ExtensionRunner { |
| 134 | + sourceDir: string; |
| 135 | + tmpDirPath: string; |
| 136 | + manifestData: Object; |
| 137 | + firefoxProfile: Object; |
| 138 | + firefox: Object; |
| 139 | + firefoxBinary: string; |
| 140 | + |
| 141 | + constructor({firefox, sourceDir, tmpDirPath, manifestData, |
| 142 | + firefoxProfile, firefoxBinary}: Object) { |
| 143 | + this.sourceDir = sourceDir; |
| 144 | + this.tmpDirPath = tmpDirPath; |
| 145 | + this.manifestData = manifestData; |
| 146 | + this.firefoxProfile = firefoxProfile; |
| 147 | + this.firefox = firefox; |
| 148 | + this.firefoxBinary = firefoxBinary; |
| 149 | + } |
| 150 | + |
| 151 | + buildExtension(): Promise { |
| 152 | + const {sourceDir, tmpDirPath, manifestData} = this; |
| 153 | + return buildExtension({sourceDir, artifactsDir: tmpDirPath}, |
| 154 | + {manifestData}); |
| 155 | + } |
| 156 | + |
| 157 | + getProfile(): Promise { |
| 158 | + const {firefox, firefoxProfile} = this; |
| 159 | + return new Promise((resolve) => { |
| 160 | + if (firefoxProfile) { |
| 161 | + log.debug(`Copying Firefox profile from ${firefoxProfile}`); |
| 162 | + resolve(firefox.copyProfile(firefoxProfile)); |
| 163 | + } else { |
| 164 | + log.debug('Creating new Firefox profile'); |
| 165 | + resolve(firefox.createProfile()); |
| 166 | + } |
| 167 | + }); |
| 168 | + } |
| 169 | + |
| 170 | + install(buildResult: Object, {profile}: Object = {}): Promise { |
| 171 | + const {firefox, manifestData} = this; |
| 172 | + return Promise.resolve(profile ? profile : this.getProfile()) |
| 173 | + .then((profile) => firefox.installExtension( |
| 174 | + { |
| 175 | + manifestData, |
| 176 | + extensionPath: buildResult.extensionPath, |
| 177 | + profile, |
42 | 178 | })
|
43 |
| - .then((profile) => firefox.run(profile, {firefoxBinary})) |
44 |
| - )); |
| 179 | + .then(() => profile)); |
| 180 | + } |
| 181 | + |
| 182 | + run(profile: Object): Promise { |
| 183 | + const {firefox, firefoxBinary} = this; |
| 184 | + return firefox.run(profile, {firefoxBinary}); |
| 185 | + } |
45 | 186 | }
|
0 commit comments