1
+ import * as childProcess from 'child_process' ;
1
2
import * as fs from 'fs' ;
2
3
import * as os from 'os' ;
3
4
import * as path from 'path' ;
4
5
import * as semver from 'semver' ;
6
+
5
7
import { requestBody , JsonObject , requestBinary } from './utils/http_utils' ;
6
- import { getBinaryPathFromConfig , removeFiles , unzipFile } from './utils/file_utils' ;
8
+ import { changeFilePermissions , generateConfigFile , getBinaryPathFromConfig , removeFiles , unzipFile } from './utils/file_utils' ;
7
9
import { isExpired } from './utils/file_utils' ;
8
10
import { OUT_DIR , ProviderClass , ProviderConfig , ProviderInterface } from './provider' ;
11
+ import rimraf = require( 'rimraf' ) ;
9
12
10
13
export class Chromium extends ProviderClass implements ProviderInterface {
11
14
cacheFileName = 'chromium-all.json' ;
@@ -18,6 +21,7 @@ export class Chromium extends ProviderClass implements ProviderInterface {
18
21
osArch = os . arch ( ) ;
19
22
outDir = OUT_DIR ;
20
23
proxy : string = null ;
24
+ maxVersion : string = null ;
21
25
22
26
constructor ( config ?: ProviderConfig ) {
23
27
super ( ) ;
@@ -28,6 +32,7 @@ export class Chromium extends ProviderClass implements ProviderInterface {
28
32
this . osType = this . setVar ( 'osType' , this . osType , config ) ;
29
33
this . outDir = this . setVar ( 'outDir' , this . outDir , config ) ;
30
34
this . proxy = this . setVar ( 'proxy' , this . proxy , config ) ;
35
+ this . maxVersion = this . setVar ( 'maxVersion' , this . maxVersion , config ) ;
31
36
}
32
37
33
38
private makeDirectory ( fileName : string ) {
@@ -223,16 +228,69 @@ export class Chromium extends ProviderClass implements ProviderInterface {
223
228
}
224
229
}
225
230
}
226
- unzipFile ( fileName , this . outDir ) ;
231
+
232
+ if ( this . osType === 'Linux' ) {
233
+ unzipFile ( fileName , this . outDir ) ;
234
+ changeFilePermissions (
235
+ path . resolve ( this . outDir , 'chrome-linux/chrome' ) , '0755' , this . osType ) ;
236
+ } else if ( this . osType === 'Darwin' ) {
237
+ spawnProcess ( 'unzip' , [ fileName , '-d' , this . outDir ] , 'ignore' ) ;
238
+ } else if ( this . osType === 'Windows_NT' ) {
239
+ unzipFile ( fileName , this . outDir ) ;
240
+ }
227
241
}
228
242
229
- async updateBinary ( majorVersion ?: string ) : Promise < void > {
243
+ async updateBinary ( _ : string , majorVersion ?: string ) : Promise < void > {
244
+ try {
245
+ this . cleanFiles ( ) ;
246
+ } catch ( _ ) {
247
+ // no-op: best attempt to clean files, there can be only one version.
248
+ }
230
249
const allJson = await this . downloadAllJson ( ) ;
231
250
const downloadVersionJson = await this . downloadVersionJson (
232
251
allJson , majorVersion ) ;
233
252
const storageObject = await this . downloadStorageObject (
234
253
downloadVersionJson , majorVersion ) ;
235
254
await this . downloadUrl ( storageObject , majorVersion ) ;
255
+
256
+ let binaryFolder = ( ) : string => {
257
+ if ( this . osType === 'Linux' ) {
258
+ return path . resolve ( this . outDir , 'chrome-linux' ) ;
259
+ } else if ( this . osType === 'Darwin' ) {
260
+ return path . resolve ( this . outDir ,
261
+ 'chrome-mac/Chromium.app/Contents/MacOS' ) ;
262
+ } else if ( this . osType === 'Windows_NT' ) {
263
+ return 'fix me' ;
264
+ }
265
+ throw new Error ( 'os does not exist' ) ;
266
+ }
267
+
268
+ let binaryRegex = ( ) : RegExp => {
269
+ if ( this . osType === 'Linux' ) {
270
+ return / c h r o m e $ / g;
271
+ } else if ( this . osType === 'Darwin' ) {
272
+ return / C h r o m i u m / g;
273
+ } else if ( this . osType === 'Windows_NT' ) {
274
+ return / f i x - m e / g;
275
+ }
276
+ throw new Error ( 'os does not exist' ) ;
277
+ } ;
278
+
279
+ let binaryFile = ( ) : string => {
280
+ if ( this . osType === 'Linux' ) {
281
+ return path . resolve ( this . outDir , 'chrome-linux/chrome' ) ;
282
+ } else if ( this . osType === 'Darwin' ) {
283
+ return path . resolve ( this . outDir ,
284
+ 'chrome-mac/Chromium.app/Contents/MacOS/Chromium' ) ;
285
+ } else if ( this . osType === 'Windows_NT' ) {
286
+ return 'fix me' ;
287
+ }
288
+ throw new Error ( 'os does not exist' ) ;
289
+ }
290
+
291
+ generateConfigFile ( binaryFolder ( ) ,
292
+ path . resolve ( this . outDir , this . configFileName ) ,
293
+ binaryRegex ( ) , binaryFile ( ) ) ;
236
294
}
237
295
238
296
getBinaryPath ( version ?: string ) : string | null {
@@ -245,11 +303,47 @@ export class Chromium extends ProviderClass implements ProviderInterface {
245
303
}
246
304
247
305
getStatus ( ) : string | null {
248
- return '' ;
306
+ try {
307
+ const existFiles = fs . readdirSync ( this . outDir ) ;
308
+ for ( const existFile of existFiles ) {
309
+ if ( existFile . match ( / c h r o m i u m \- v e r s i o n \. * / g) ) {
310
+ const regex = / c h r o m i u m \- v e r s i o n \- ( \d + ) \. j s o n / g;
311
+ const exec = regex . exec ( existFile ) ;
312
+ if ( exec ) {
313
+ return exec [ 1 ] ;
314
+ }
315
+ }
316
+ }
317
+ return null ;
318
+ } catch ( _ ) {
319
+ return null ;
320
+ }
249
321
}
250
322
251
323
cleanFiles ( ) : string {
252
- return removeFiles ( this . outDir , [ / c h r o m i u m .* / g] ) ;
324
+ let chromiumPath = '' ;
325
+ if ( this . osType === 'Darwin' ) {
326
+ chromiumPath = 'chrome-mac/' ;
327
+ } else if ( this . osType === 'Linux' ) {
328
+ chromiumPath = 'chrome-linux/' ;
329
+ } else if ( this . osType === 'Windows_NT' ) {
330
+ chromiumPath = 'chrome-win/' ;
331
+ }
332
+
333
+ rimraf . sync ( path . resolve ( this . outDir , chromiumPath ) ) ;
334
+ const files = removeFiles ( this . outDir , [ / c h r o m i u m .* / g] ) ;
335
+ try {
336
+ const fileList = files . split ( '\n' ) ;
337
+ if ( files . length === 0 ) {
338
+ // No files listed to clean.
339
+ return '' ;
340
+ }
341
+ fileList . push ( chromiumPath ) ;
342
+ return ( fileList . sort ( ) ) . join ( '\n' ) ;
343
+ } catch ( _ ) {
344
+ // If files returns null, catch split error.
345
+ return '' ;
346
+ }
253
347
}
254
348
}
255
349
@@ -275,4 +369,21 @@ export function osHelper(ostype: string, osarch: string): string {
275
369
}
276
370
}
277
371
return null ;
372
+ }
373
+
374
+ /**
375
+ * A command line to run. Example 'npm start', the task='npm' and the
376
+ * opt_arg=['start']
377
+ * @param task The task string.
378
+ * @param optArg Optional task args.
379
+ * @param optIo Optional io arg. By default, it should log to console.
380
+ * @returns The child process.
381
+ */
382
+ export function spawnProcess ( task : string , optArg ?: string [ ] , optIo ?: string ) {
383
+ optArg = typeof optArg !== 'undefined' ? optArg : [ ] ;
384
+ let stdio : childProcess . StdioOptions = 'inherit' ;
385
+ if ( optIo === 'ignore' ) {
386
+ stdio = 'ignore' ;
387
+ }
388
+ return childProcess . spawnSync ( task , optArg , { stdio} ) ;
278
389
}
0 commit comments