@@ -5,7 +5,6 @@ import * as fs from 'fs-extra';
55import * as path from 'path' ;
66
77import { JSZipCLI } from './JSZipCLI' ;
8- import { defaultOptions } from './defaultOptions' ;
98
109const defaultPackageJsonPath = path . join ( __dirname , 'package.json' ) ;
1110const packageJsonPath = fs . existsSync ( defaultPackageJsonPath )
@@ -31,7 +30,7 @@ commander
3130 . name ( name . replace ( / ^ @ [ ^ / ] + \/ / , '' ) )
3231 . description ( description )
3332 . option ( '--noconfig' , "don't look for a configuration file" )
34- . option ( '-c, --config <path>' , 'use a configuration file' )
33+ . option ( '-c, --config <path>' , 'use a configuration file (default: .jsziprc.json) ' )
3534 . option ( '-d, --dereference' , 'dereference (follow) links' , false )
3635 . option ( '-f, --force' , 'force overwriting files and directories when extracting' , false )
3736 . option ( '-i, --ignore <entry>' , 'ignore a file or directory' )
@@ -59,30 +58,25 @@ commander
5958 . option ( '-q, --quiet' , "don't log anything excluding errors" , false )
6059 . option ( '-V, --verbose' , 'enable verbose logging' , false )
6160 . arguments ( '[entries...]' )
62- . action ( ( entries : string [ ] ) => {
61+ . action ( async ( entries : string [ ] ) => {
6362 const options = commander . opts ( ) as CLIOptions ;
6463 try {
65- new JSZipCLI ( {
66- compressionLevel : Number ( options . level ) ?? defaultOptions . compressionLevel ,
67- configFile : options . config ?? ( options . noconfig && false ) ?? defaultOptions . configFile ,
68- dereferenceLinks : options . dereference ?? defaultOptions . dereferenceLinks ,
69- force : options . force ?? defaultOptions . force ,
70- ignoreEntries : options . ignore ? [ options . ignore ] : defaultOptions . ignoreEntries ,
71- outputEntry : options . output ?? defaultOptions . outputEntry ,
72- quiet : options . quiet ?? defaultOptions . quiet ,
73- verbose : options . verbose ?? defaultOptions . verbose ,
74- } )
75- . add ( entries )
76- . save ( )
77- . then ( ( { outputFile, compressedFilesCount} ) => {
78- if ( options . output && ! options . quiet ) {
79- console . info ( `Done compressing ${ compressedFilesCount } files to "${ outputFile } ".` ) ;
80- }
81- } )
82- . catch ( error => {
83- console . error ( 'Error:' , ( error as Error ) . message ) ;
84- process . exit ( 1 ) ;
85- } ) ;
64+ const jszip = new JSZipCLI ( {
65+ ...( options . level && { compressionLevel : Number ( options . level ) } ) ,
66+ ...( ( options . config && { configFile : options . config } ) || ( options . noconfig && { configFile : false } ) ) ,
67+ ...( options . dereference && { dereferenceLinks : options . dereference } ) ,
68+ ...( options . force && { force : options . force } ) ,
69+ ...( options . ignore && { ignoreEntries : [ options . ignore ] } ) ,
70+ ...( options . output && { outputEntry : options . output } ) ,
71+ ...( options . quiet && { quiet : options . quiet } ) ,
72+ ...( options . verbose && { verbose : options . verbose } ) ,
73+ } ) ;
74+ jszip . add ( entries ) ;
75+ const { outputFile, compressedFilesCount} = await jszip . save ( ) ;
76+
77+ if ( options . output && ! options . quiet ) {
78+ console . info ( `Done compressing ${ compressedFilesCount } files to "${ outputFile } ".` ) ;
79+ }
8680 } catch ( error ) {
8781 console . error ( 'Error:' , ( error as Error ) . message ) ;
8882 process . exit ( 1 ) ;
@@ -94,65 +88,67 @@ commander
9488 . alias ( 'e' )
9589 . description ( 'extract files and directories from ZIP archive(s)' )
9690 . option ( '--noconfig' , "don't look for a configuration file" , false )
97- . option ( '-c, --config <path>' , 'use a configuration file' )
91+ . option ( '-c, --config <path>' , 'use a configuration file (default: .jsziprc.json) ' )
9892 . option ( '-o, --output <dir>' , 'set the output file or directory (default: stdout)' )
9993 . option ( '-i, --ignore <entry>' , 'ignore a file or directory' )
10094 . option ( '-f, --force' , 'force overwriting files and directories' , false )
10195 . option ( '-V, --verbose' , 'enable verbose logging' , false )
10296 . option ( '-q, --quiet' , "don't log anything excluding errors" , false )
10397 . arguments ( '<archives...>' )
104- . action ( ( archives : string [ ] ) => {
98+ . action ( async ( archives : string [ ] ) => {
10599 const options = commander . opts ( ) as CLIOptions ;
106100 try {
107- new JSZipCLI ( {
108- configFile : options . config ?? ( options . noconfig && false ) ?? defaultOptions . configFile ,
109- force : options . force ?? defaultOptions . force ,
110- ignoreEntries : options . ignore ? [ options . ignore ] : defaultOptions . ignoreEntries ,
111- outputEntry : options . output ?? defaultOptions . outputEntry ,
112- quiet : options . quiet ?? defaultOptions . quiet ,
113- verbose : options . verbose ?? defaultOptions . verbose ,
114- } )
115- . extract ( archives )
116- . then ( ( { outputDir, extractedFilesCount} ) => {
117- if ( options . output && ! options . quiet ) {
118- console . info ( `Done extracting ${ extractedFilesCount } files to "${ outputDir } ".` ) ;
119- }
120- } )
121- . catch ( error => {
122- console . error ( 'Error:' , ( error as Error ) . message ) ;
123- process . exit ( 1 ) ;
124- } ) ;
101+ const { outputDir, extractedFilesCount} = await new JSZipCLI ( {
102+ ...( ( options . config && { configFile : options . config } ) || ( options . noconfig && { configFile : false } ) ) ,
103+ ...( options . force && { force : options . force } ) ,
104+ ...( options . ignore && { ignoreEntries : [ options . ignore ] } ) ,
105+ ...( options . output && { outputEntry : options . output } ) ,
106+ ...( options . quiet && { quiet : options . quiet } ) ,
107+ ...( options . verbose && { verbose : options . verbose } ) ,
108+ } ) . extract ( archives ) ;
109+
110+ if ( options . output && ! options . quiet ) {
111+ console . info ( `Done extracting ${ extractedFilesCount } files to "${ outputDir } ".` ) ;
112+ }
125113 } catch ( error ) {
126114 console . error ( 'Error:' , ( error as Error ) . message ) ;
127115 process . exit ( 1 ) ;
128116 }
129117 } ) ;
130118
131- commander . parse ( process . argv ) ;
119+ commander
120+ . command ( 'fileMode' , { hidden : true , isDefault : true } )
121+ . option ( '--noconfig' , "don't look for a configuration file" , false )
122+ . option ( '-c, --config <path>' , 'use a configuration file (default: .jsziprc.json)' )
123+ . option ( '-o, --output <dir>' , 'set the output file or directory (default: stdout)' )
124+ . option ( '-i, --ignore <entry>' , 'ignore a file or directory' )
125+ . option ( '-f, --force' , 'force overwriting files and directories' , false )
126+ . option ( '-V, --verbose' , 'enable verbose logging' , false )
127+ . option ( '-q, --quiet' , "don't log anything excluding errors" , false )
128+ . action ( async ( ) => {
129+ const options = commander . opts ( ) as CLIOptions ;
132130
133- const commanderOptions = commander . opts ( ) ;
131+ try {
132+ if ( options . noconfig ) {
133+ commander . outputHelp ( ) ;
134+ }
134135
135- if ( ! commander . args . length ) {
136- if ( commanderOptions . noconfig ) {
137- commander . outputHelp ( ) ;
138- process . exit ( 1 ) ;
139- }
140- try {
141- new JSZipCLI ( {
142- configFile : commanderOptions . config || true ,
143- ...( commanderOptions . force && { force : commanderOptions . force } ) ,
144- ...( commanderOptions . ignore && { ignoreEntries : [ commanderOptions . ignore ] } ) ,
145- ...( commanderOptions . output && { outputEntry : commanderOptions . output } ) ,
146- ...( commanderOptions . quiet && { quiet : commanderOptions . quiet } ) ,
147- ...( commanderOptions . verbose && { verbose : commanderOptions . verbose } ) ,
148- } )
149- . fileMode ( )
150- . catch ( error => {
136+ await new JSZipCLI ( {
137+ configFile : options . config ,
138+ ...( options . force && { force : options . force } ) ,
139+ ...( options . ignore && { ignoreEntries : [ options . ignore ] } ) ,
140+ ...( options . output && { outputEntry : options . output } ) ,
141+ ...( options . quiet && { quiet : options . quiet } ) ,
142+ ...( options . verbose && { verbose : options . verbose } ) ,
143+ } ) . fileMode ( ) ;
144+ } catch ( error ) {
145+ if ( ( error as Error ) . message . includes ( 'ENOENT' ) ) {
146+ console . error ( 'Error:' , `Configuration file "${ options . config } " not found and no mode specified.` ) ;
147+ } else {
151148 console . error ( 'Error:' , ( error as Error ) . message ) ;
152- process . exit ( 1 ) ;
153- } ) ;
154- } catch ( error ) {
155- console . error ( 'Error:' , ( error as Error ) . message ) ;
156- process . exit ( 1 ) ;
157- }
158- }
149+ }
150+ process . exit ( 1 ) ;
151+ }
152+ } ) ;
153+
154+ commander . parse ( process . argv ) ;
0 commit comments