11/* @flow */
2- import buildExtension from './build' ;
32import * as defaultFirefox from '../firefox' ;
43import defaultFirefoxConnector from '../firefox/remote' ;
54import { onlyErrorsWithCode } from '../errors' ;
6- import { withTempDir } from '../util/temp-dir' ;
75import { createLogger } from '../util/logger' ;
86import getValidatedManifest from '../util/manifest' ;
97import defaultSourceWatcher from '../watcher' ;
@@ -12,13 +10,12 @@ const log = createLogger(__filename);
1210
1311
1412export function defaultWatcherCreator (
15- { profile , client, sourceDir, artifactsDir, createRunner,
13+ { client, sourceDir, artifactsDir, createRunner,
1614 onSourceChange= defaultSourceWatcher } : Object ) : Object {
1715 return onSourceChange ( {
18- sourceDir, artifactsDir, onChange : ( ) => createRunner (
19- ( runner ) => runner . buildExtension ( )
20- . then ( ( buildResult ) => runner . install ( buildResult , { profile} ) )
21- . then ( ( ) => {
16+ sourceDir, artifactsDir, onChange : ( ) => {
17+ return createRunner ( )
18+ . then ( ( runner ) => {
2219 log . debug ( 'Attempting to reload extension' ) ;
2320 const addonId = runner . manifestData . applications . gecko . id ;
2421 log . debug ( `Reloading add-on ID ${ addonId } ` ) ;
@@ -27,40 +24,39 @@ export function defaultWatcherCreator(
2724 . catch ( ( error ) => {
2825 log . error ( error . stack ) ;
2926 throw error ;
30- } )
31- ) ,
27+ } ) ;
28+ } ,
3229 } ) ;
3330}
3431
3532
3633export 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 ;
34+ { firefox, client, profile, sourceDir, artifactsDir, createRunner} : Object ,
35+ { createWatcher= defaultWatcherCreator } : Object = { } ) {
36+ let watcher ;
4437
4538 firefox . on ( 'close' , ( ) => {
46- if ( client ) {
47- client . disconnect ( ) ;
48- }
49- if ( watcher ) {
50- watcher . close ( ) ;
51- }
39+ client . disconnect ( ) ;
40+ watcher . close ( ) ;
41+ } ) ;
42+
43+ watcher = createWatcher ( {
44+ client , sourceDir , artifactsDir , createRunner ,
5245 } ) ;
46+ }
47+
48+
49+ export function defaultFirefoxClient (
50+ { connectToFirefox= defaultFirefoxConnector ,
51+ maxRetries= 25 , retryInterval= 120 } : Object = { } ) {
52+ var retries = 0 ;
5353
5454 function establishConnection ( ) {
5555 return new Promise ( ( resolve , reject ) => {
5656 connectToFirefox ( )
5757 . then ( ( connectedClient ) => {
5858 log . debug ( 'Connected to the Firefox debugger' ) ;
59- client = connectedClient ;
60- watcher = createWatcher ( {
61- profile, client, sourceDir, artifactsDir, createRunner,
62- } ) ;
63- resolve ( ) ;
59+ resolve ( connectedClient ) ;
6460 } )
6561 . catch ( onlyErrorsWithCode ( 'ECONNREFUSED' , ( error ) => {
6662 if ( retries >= maxRetries ) {
@@ -88,72 +84,76 @@ export function defaultReloadStrategy(
8884
8985export default function run (
9086 { sourceDir, artifactsDir, firefoxBinary, firefoxProfile, noReload} : Object ,
91- { firefox= defaultFirefox , reloadStrategy= defaultReloadStrategy }
87+ { firefoxClient= defaultFirefoxClient , firefox= defaultFirefox ,
88+ reloadStrategy= defaultReloadStrategy }
9289 : Object = { } ) : Promise {
9390
9491 log . info ( `Running web extension from ${ sourceDir } ` ) ;
9592
96- function createRunner ( callback ) {
93+ function createRunner ( ) {
9794 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- ) ) ;
95+ . then ( ( manifestData ) => {
96+ return new ExtensionRunner ( {
97+ sourceDir,
98+ firefox,
99+ firefoxBinary,
100+ manifestData,
101+ firefoxProfile,
102+ } ) ;
103+ } ) ;
111104 }
112105
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- ) ;
106+ return createRunner ( )
107+ . then ( ( runner ) => {
108+ return runner . getProfile ( ) . then ( ( profile ) => {
109+ return { runner, profile} ;
110+ } ) ;
111+ } )
112+ . then ( ( { runner, profile} ) => {
113+ return runner . run ( profile ) . then ( ( firefox ) => {
114+ return { runner, profile, firefox} ;
115+ } ) ;
116+ } )
117+ . then ( ( config ) => {
118+ return firefoxClient ( ) . then ( ( client ) => {
119+ return { client, ...config } ;
120+ } ) ;
121+ } )
122+ . then ( ( config ) => {
123+ const { runner, client} = config ;
124+ return runner . install ( client ) . then ( ( ) => {
125+ return config ;
126+ } ) ;
127+ } )
128+ . then ( ( { firefox, profile, client} ) => {
129+ if ( noReload ) {
130+ log . debug ( 'Extension auto-reloading has been disabled' ) ;
131+ } else {
132+ log . debug ( 'Reloading extension when the source changes' ) ;
133+ reloadStrategy ( { firefox, profile, client, sourceDir,
134+ artifactsDir, createRunner} ) ;
135+ }
136+ return firefox ;
137+ } ) ;
130138}
131139
132140
133141export class ExtensionRunner {
134142 sourceDir : string ;
135- tmpDirPath: string ;
136143 manifestData: Object ;
137144 firefoxProfile: Object ;
138145 firefox: Object ;
139146 firefoxBinary: string ;
140147
141- constructor ( { firefox, sourceDir, tmpDirPath , manifestData,
148+ constructor ( { firefox, sourceDir, manifestData,
142149 firefoxProfile, firefoxBinary} : Object ) {
143150 this . sourceDir = sourceDir ;
144- this . tmpDirPath = tmpDirPath ;
145151 this . manifestData = manifestData ;
146152 this . firefoxProfile = firefoxProfile ;
147153 this . firefox = firefox ;
148154 this . firefoxBinary = firefoxBinary ;
149155 }
150156
151- buildExtension ( ) : Promise {
152- const { sourceDir, tmpDirPath, manifestData} = this ;
153- return buildExtension ( { sourceDir, artifactsDir : tmpDirPath } ,
154- { manifestData} ) ;
155- }
156-
157157 getProfile ( ) : Promise {
158158 const { firefox, firefoxProfile} = this ;
159159 return new Promise ( ( resolve ) => {
@@ -167,16 +167,8 @@ export class ExtensionRunner {
167167 } ) ;
168168 }
169169
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,
178- } )
179- . then ( ( ) => profile ) ) ;
170+ install ( client : Object ) : Promise {
171+ return client . installTemporaryAddon ( this . sourceDir ) ;
180172 }
181173
182174 run ( profile : Object ) : Promise {
0 commit comments