@@ -9,45 +9,46 @@ import * as os from 'os';
99import * as fs from 'fs' ;
1010import * as mkdirp from 'mkdirp' ;
1111import { tmpName } from 'tmp' ;
12- import { IDriver , connect as connectDriver , IDisposable , IElement , Thenable } from './puppeteer-driver' ;
12+ import { IDriver , connect as connectElectronDriver , IDisposable , IElement , Thenable } from './driver' ;
13+ import { connect as connectPuppeteerDriver , launch } from './puppeteer-driver' ;
1314import { Logger } from '../logger' ;
1415import { ncp } from 'ncp' ;
1516import { URI } from 'vscode-uri' ;
1617
1718const repoPath = path . join ( __dirname , '../../../..' ) ;
1819
19- // function getDevElectronPath(): string {
20- // const buildPath = path.join(repoPath, '.build');
21- // const product = require(path.join(repoPath, 'product.json'));
22-
23- // switch (process.platform) {
24- // case 'darwin':
25- // return path.join(buildPath, 'electron', `${product.nameLong}.app`, 'Contents', 'MacOS', 'Electron');
26- // case 'linux':
27- // return path.join(buildPath, 'electron', `${product.applicationName}`);
28- // case 'win32':
29- // return path.join(buildPath, 'electron', `${product.nameShort}.exe`);
30- // default:
31- // throw new Error('Unsupported platform.');
32- // }
33- // }
34-
35- // function getBuildElectronPath(root: string): string {
36- // switch (process.platform) {
37- // case 'darwin':
38- // return path.join(root, 'Contents', 'MacOS', 'Electron');
39- // case 'linux': {
40- // const product = require(path.join(root, 'resources', 'app', 'product.json'));
41- // return path.join(root, product.applicationName);
42- // }
43- // case 'win32': {
44- // const product = require(path.join(root, 'resources', 'app', 'product.json'));
45- // return path.join(root, `${product.nameShort}.exe`);
46- // }
47- // default:
48- // throw new Error('Unsupported platform.');
49- // }
50- // }
20+ function getDevElectronPath ( ) : string {
21+ const buildPath = path . join ( repoPath , '.build' ) ;
22+ const product = require ( path . join ( repoPath , 'product.json' ) ) ;
23+
24+ switch ( process . platform ) {
25+ case 'darwin' :
26+ return path . join ( buildPath , 'electron' , `${ product . nameLong } .app` , 'Contents' , 'MacOS' , 'Electron' ) ;
27+ case 'linux' :
28+ return path . join ( buildPath , 'electron' , `${ product . applicationName } ` ) ;
29+ case 'win32' :
30+ return path . join ( buildPath , 'electron' , `${ product . nameShort } .exe` ) ;
31+ default :
32+ throw new Error ( 'Unsupported platform.' ) ;
33+ }
34+ }
35+
36+ function getBuildElectronPath ( root : string ) : string {
37+ switch ( process . platform ) {
38+ case 'darwin' :
39+ return path . join ( root , 'Contents' , 'MacOS' , 'Electron' ) ;
40+ case 'linux' : {
41+ const product = require ( path . join ( root , 'resources' , 'app' , 'product.json' ) ) ;
42+ return path . join ( root , product . applicationName ) ;
43+ }
44+ case 'win32' : {
45+ const product = require ( path . join ( root , 'resources' , 'app' , 'product.json' ) ) ;
46+ return path . join ( root , `${ product . nameShort } .exe` ) ;
47+ }
48+ default :
49+ throw new Error ( 'Unsupported platform.' ) ;
50+ }
51+ }
5152
5253function getDevOutPath ( ) : string {
5354 return path . join ( repoPath , 'out' ) ;
@@ -62,7 +63,7 @@ function getBuildOutPath(root: string): string {
6263 }
6364}
6465
65- async function connect ( child : cp . ChildProcess | undefined , outPath : string , handlePath : string , logger : Logger ) : Promise < Code > {
66+ async function connect ( connectDriver : typeof connectElectronDriver , child : cp . ChildProcess | undefined , outPath : string , handlePath : string , logger : Logger ) : Promise < Code > {
6667 let errCount = 0 ;
6768
6869 while ( true ) {
@@ -95,7 +96,10 @@ export interface SpawnOptions {
9596 verbose ?: boolean ;
9697 extraArgs ?: string [ ] ;
9798 log ?: string ;
99+ /** Run in the test resolver */
98100 remote ?: boolean ;
101+ /** Run in the web */
102+ web ?: boolean ;
99103}
100104
101105async function createDriverHandle ( ) : Promise < string > {
@@ -109,7 +113,7 @@ async function createDriverHandle(): Promise<string> {
109113
110114export async function spawn ( options : SpawnOptions ) : Promise < Code > {
111115 const codePath = options . codePath ;
112- // const electronPath = codePath ? getBuildElectronPath(codePath) : getDevElectronPath();
116+ const electronPath = codePath ? getBuildElectronPath ( codePath ) : getDevElectronPath ( ) ;
113117 const outPath = codePath ? getBuildOutPath ( codePath ) : getDevOutPath ( ) ;
114118 const handle = await createDriverHandle ( ) ;
115119
@@ -162,15 +166,21 @@ export async function spawn(options: SpawnOptions): Promise<Code> {
162166 args . push ( ...options . extraArgs ) ;
163167 }
164168
165- // const spawnOptions: cp.SpawnOptions = { env };
169+ let child : cp . ChildProcess | undefined ;
170+ let connectDriver : typeof connectElectronDriver ;
166171
167- // const child = cp.spawn(electronPath, args, spawnOptions);
168-
169- // instances.add(child);
170- // child.once('exit', () => instances.delete(child));
172+ if ( options . web ) {
173+ launch ( args ) ;
174+ connectDriver = connectPuppeteerDriver ;
175+ } else {
176+ const spawnOptions : cp . SpawnOptions = { env } ;
177+ child = cp . spawn ( electronPath , args , spawnOptions ) ;
178+ instances . add ( child ) ;
179+ child . once ( 'exit' , ( ) => instances . delete ( child ! ) ) ;
180+ connectDriver = connectElectronDriver ;
181+ }
171182
172- const child = undefined ;
173- return connect ( child , outPath , handle , options . logger ) ;
183+ return connect ( connectDriver , child , outPath , handle , options . logger ) ;
174184}
175185
176186async function poll < T > (
@@ -215,7 +225,7 @@ export class Code {
215225 private driver : IDriver ;
216226
217227 constructor (
218- private client : IDisposable ,
228+ private client : IDisposable | undefined ,
219229 driver : IDriver ,
220230 readonly logger : Logger
221231 ) {
@@ -331,7 +341,9 @@ export class Code {
331341 }
332342
333343 dispose ( ) : void {
334- this . client . dispose ( ) ;
344+ if ( this . client ) {
345+ this . client . dispose ( ) ;
346+ }
335347 }
336348}
337349
0 commit comments