11/* eslint-disable require-atomic-updates */
22
3- import { mkdirp , readFile , remove , stat , readFileSync } from 'fs-extra' ;
3+ import { existsSync , mkdirp , readFile , remove , stat , readFileSync } from 'fs-extra' ;
44import { need , system } from 'pkg-fetch' ;
55import assert from 'assert' ;
66import minimist from 'minimist' ;
@@ -16,6 +16,7 @@ import refine from './refiner';
1616import { shutdown } from './fabricator' ;
1717import walk , { Marker , WalkerParams } from './walker' ;
1818import { Target , NodeTarget , SymLinks } from './types' ;
19+ import { CompressType } from './compress_type' ;
1920
2021const { version } = JSON . parse (
2122 readFileSync ( path . join ( __dirname , '../package.json' ) , 'utf-8' )
@@ -25,15 +26,6 @@ function isConfiguration(file: string) {
2526 return isPackageJson ( file ) || file . endsWith ( '.config.json' ) ;
2627}
2728
28- async function exists ( file : string ) {
29- try {
30- await stat ( file ) ;
31- return true ;
32- } catch ( error ) {
33- return false ;
34- }
35- }
36-
3729// http://www.openwall.com/lists/musl/2012/12/08/4
3830
3931const {
@@ -245,6 +237,8 @@ export async function exec(argv2: string[]) {
245237 't' ,
246238 'target' ,
247239 'targets' ,
240+ 'C' ,
241+ 'compress' ,
248242 ] ,
249243 default : { bytecode : true } ,
250244 } ) ;
@@ -272,7 +266,31 @@ export async function exec(argv2: string[]) {
272266
273267 const forceBuild = argv . b || argv . build ;
274268
275- // _
269+ // doCompress
270+ const algo = argv . C || argv . compress || 'None' ;
271+
272+
273+ let doCompress : CompressType = CompressType . None ;
274+ switch ( algo . toLowerCase ( ) ) {
275+ case 'brotli' :
276+ case 'br' :
277+ doCompress = CompressType . Brotli ;
278+ break ;
279+ case 'gzip' :
280+ case 'gz' :
281+ doCompress = CompressType . GZip ;
282+ break ;
283+ case 'none' :
284+ break ;
285+ default :
286+ // eslint-disable-next-line no-console
287+ throw wasReported ( `Invalid compression algorithm ${ algo } ( should be None, Brotli or Gzip)` ) ;
288+
289+ }
290+ if ( doCompress !== CompressType . None ) {
291+ // eslint-disable-next-line no-console
292+ console . log ( 'compression: ' , CompressType [ doCompress ] ) ;
293+ }
276294
277295 if ( ! argv . _ . length ) {
278296 throw wasReported ( 'Entry file/directory is expected' , [
@@ -288,14 +306,13 @@ export async function exec(argv2: string[]) {
288306
289307 let input = path . resolve ( argv . _ [ 0 ] ) ;
290308
291- if ( ! ( await exists ( input ) ) ) {
309+ if ( ! existsSync ( input ) ) {
292310 throw wasReported ( 'Input file does not exist' , [ input ] ) ;
293311 }
294312
295313 if ( ( await stat ( input ) ) . isDirectory ( ) ) {
296314 input = path . join ( input , 'package.json' ) ;
297-
298- if ( ! ( await exists ( input ) ) ) {
315+ if ( ! existsSync ( input ) ) {
299316 throw wasReported ( 'Input file does not exist' , [ input ] ) ;
300317 }
301318 }
@@ -330,10 +347,10 @@ export async function exec(argv2: string[]) {
330347 }
331348 }
332349 inputBin = path . resolve ( path . dirname ( input ) , bin ) ;
333- if ( ! ( await exists ( inputBin ) ) ) {
350+ if ( ! existsSync ( inputBin ) ) {
334351 throw wasReported (
335352 'Bin file does not exist (taken from package.json ' +
336- "'bin' property)" ,
353+ "'bin' property)" ,
337354 [ inputBin ]
338355 ) ;
339356 }
@@ -362,8 +379,7 @@ export async function exec(argv2: string[]) {
362379
363380 if ( config ) {
364381 config = path . resolve ( config ) ;
365-
366- if ( ! ( await exists ( config ) ) ) {
382+ if ( ! existsSync ( config ) ) {
367383 throw wasReported ( 'Config file does not exist' , [ config ] ) ;
368384 }
369385
@@ -603,7 +619,7 @@ export async function exec(argv2: string[]) {
603619 log . debug ( 'Targets:' , JSON . stringify ( targets , null , 2 ) ) ;
604620
605621 for ( const target of targets ) {
606- if ( target . output && ( await exists ( target . output ) ) ) {
622+ if ( target . output && existsSync ( target . output ) ) {
607623 if ( ( await stat ( target . output ) ) . isFile ( ) ) {
608624 await remove ( target . output ) ;
609625 } else {
@@ -615,14 +631,8 @@ export async function exec(argv2: string[]) {
615631 await mkdirp ( path . dirname ( target . output ) ) ;
616632 }
617633
618- await producer ( {
619- backpack,
620- bakes,
621- slash : target . platform === 'win' ? '\\' : '/' ,
622- target : target as Target ,
623- symLinks,
624- } ) ;
625-
634+ const slash = target . platform === 'win' ? '\\' : '/' ;
635+ await producer ( { backpack, bakes, slash, target : target as Target , symLinks, doCompress } ) ;
626636 if ( target . platform !== 'win' && target . output ) {
627637 await plusx ( target . output ) ;
628638 }
0 commit comments