@@ -3,6 +3,9 @@ import { getDefaultPrefs } from "@commands/default-prefs";
3
3
import { defaultPrefsUserJS } from "@commands/default-prefs-userjs" ;
4
4
import { diff } from "@commands/diff" ;
5
5
import { unusedPrefs } from "@commands/unused-prefs" ;
6
+ import { getArgumentValue , parseKeepArgument } from "@lib/cli" ;
7
+ import { getInstalledFirefoxPath } from "@lib/firefox" ;
8
+ import { startsWithNumberDotNumber } from "@lib/helpers" ;
6
9
7
10
const CONSOLE_COLORS = {
8
11
GREEN : "\u001B[32m" ,
@@ -15,7 +18,7 @@ interface SourceCleanupOptions {
15
18
sources : boolean ;
16
19
}
17
20
18
- export interface OutputOptions {
21
+ interface OutputOptions {
19
22
doNotPrintConsole : boolean ;
20
23
saveOutput : boolean ;
21
24
}
@@ -31,7 +34,7 @@ export const HELP_ARGS = {
31
34
longOption : "--help" ,
32
35
doc : "Print help" ,
33
36
} ;
34
- export const HELP_ARGS_VALUES = Object . values ( VERSION_ARGS ) ;
37
+ export const HELP_ARGS_VALUES = Object . values ( HELP_ARGS ) ;
35
38
36
39
export const CLI_ARGS = {
37
40
CLEAN_ARCHIVES : "--clean-archives" ,
@@ -81,6 +84,16 @@ const pathToFirefox: CliOption = {
81
84
longOption : `${ CLI_ARGS . FIREFOX_PATH } ${ CLI_VALUES . PATH_USAGE } ` ,
82
85
doc : "Path to the firefox binary" ,
83
86
} ;
87
+
88
+ export const hasAnyArg = ( args : readonly string [ ] ) : boolean => {
89
+ return process . argv . some ( ( arg ) => args . includes ( arg ) ) ;
90
+ } ;
91
+
92
+ const createPrintOptions = ( ) : OutputOptions => ( {
93
+ doNotPrintConsole : process . argv . includes ( CLI_ARGS . DO_NOT_PRINT_IN_CONSOLE ) ,
94
+ saveOutput : process . argv . includes ( CLI_ARGS . SAVE_OUTPUT ) ,
95
+ } ) ;
96
+
84
97
const createCleanOptions = ( ) : SourceCleanupOptions => ( {
85
98
archives : process . argv . includes ( CLI_ARGS . CLEAN_ARCHIVES ) ,
86
99
sources : process . argv . includes ( CLI_ARGS . CLEAN_SOURCES ) ,
@@ -91,15 +104,6 @@ const createKeepOptions = (): SourceCleanupOptions => ({
91
104
sources : process . argv . includes ( CLI_ARGS . KEEP_SOURCES ) ,
92
105
} ) ;
93
106
94
- export const hasAnyArg = ( args : readonly string [ ] ) : boolean => {
95
- return process . argv . some ( ( arg ) => args . includes ( arg ) ) ;
96
- } ;
97
-
98
- export const createPrintOptions = ( ) : OutputOptions => ( {
99
- doNotPrintConsole : process . argv . includes ( CLI_ARGS . DO_NOT_PRINT_IN_CONSOLE ) ,
100
- saveOutput : process . argv . includes ( CLI_ARGS . SAVE_OUTPUT ) ,
101
- } ) ;
102
-
103
107
export const cleanOptions : SourceCleanupOptions = createCleanOptions ( ) ;
104
108
export const keepOptions : SourceCleanupOptions = createKeepOptions ( ) ;
105
109
export const printOptions : OutputOptions = createPrintOptions ( ) ;
@@ -118,7 +122,7 @@ interface CliOption extends CliDoc {
118
122
readonly shortOption ?: string ;
119
123
}
120
124
121
- export abstract class BaseCli {
125
+ abstract class BaseCli {
122
126
public readonly command ?: string ;
123
127
public readonly description : string ;
124
128
public readonly commands : readonly CliCommand [ ] ;
@@ -211,163 +215,192 @@ export abstract class BaseCli {
211
215
}
212
216
}
213
217
214
- export class DiffCommand extends BaseCli {
215
- public static readonly COMMAND = "diff" ;
216
- public static readonly SMALL_DESCRIPTION =
217
- "Compare the default preferences of two firefox versions" ;
218
- public static readonly DESCRIPTION =
219
- DiffCommand . SMALL_DESCRIPTION + " and highlight differences" ;
220
-
221
- public static readonly COMMANDS : readonly CliCommand [ ] = [
222
- {
223
- values : [ `<${ CLI_VALUES . OLD_VERSION } > <${ CLI_VALUES . NEW_VERSION } >` ] ,
224
- doc : [
225
- "First arg is the old version, second arg is the new version" ,
226
- ` Example: diff ${ EXAMPLE_VERSION . OLD_VERSION } ${ EXAMPLE_VERSION . NEW_VERSION } ` ,
227
- ] . join ( "\n" ) ,
228
- } ,
229
- ] ;
230
-
218
+ class CleanCommand extends BaseCli {
219
+ public static readonly COMMAND = "clean" ;
220
+ public static readonly SMALL_DESCRIPTION = undefined ;
221
+ public static readonly DESCRIPTION = `Remove files generated by ${ APP_NAME } ` ;
222
+ public static readonly COMMANDS : readonly CliCommand [ ] = [ ] ;
231
223
public static readonly OPTIONS : readonly CliOption [ ] = [
232
224
{
233
- longOption : CLI_ARGS . CLEAN_ARCHIVES ,
225
+ longOption : ` ${ CLI_ARGS . KEEP } ${ CLI_VALUES . VERSION1 } , ${ CLI_VALUES . VERSION2 } ` ,
234
226
235
- doc : "Remove archives after retrieving preferences" ,
227
+ doc : [
228
+ "Specify one or more versions whose archives and binaries should be preserved during cleanup" ,
229
+ "Provide a comma-separated list of versions to keep" ,
230
+ ` Example: ${ CLI_ARGS . KEEP } ${ EXAMPLE_VERSION . OLD_VERSION } ,${ EXAMPLE_VERSION . NEW_VERSION } ` ,
231
+ ] . join ( "\n" ) ,
232
+ smallDoc : "Version to keep" ,
236
233
} ,
237
234
{
238
- longOption : CLI_ARGS . CLEAN_SOURCES ,
239
-
240
- doc : "Remove binaries after retrieving preferences" ,
235
+ longOption : CLI_ARGS . KEEP_ARCHIVES ,
236
+ doc : "Keep all archives" ,
241
237
} ,
242
- ...printAndSave ,
243
238
{
244
- longOption : ` ${ CLI_ARGS . COMPARE_USERJS } ${ CLI_VALUES . PATH_USAGE } ` ,
245
- doc : "Check for removed or changed keys in the specified user.js file " ,
239
+ longOption : CLI_ARGS . KEEP_SOURCES ,
240
+ doc : "Keep all binaries " ,
246
241
} ,
247
242
] ;
248
243
249
244
constructor ( fail : boolean = true ) {
250
245
super (
251
246
fail ,
252
- DiffCommand . DESCRIPTION ,
253
- DiffCommand . COMMANDS ,
254
- DiffCommand . OPTIONS ,
247
+ CleanCommand . DESCRIPTION ,
248
+ CleanCommand . COMMANDS ,
249
+ CleanCommand . OPTIONS ,
255
250
) ;
256
251
}
257
252
258
253
public async entrypoint ( ) : Promise < void > {
259
- await diff ( ) ;
254
+ const keptVersions = parseKeepArgument ( ) ;
255
+ await clean ( keptVersions ) ;
260
256
}
261
257
}
262
258
263
- export class UnusedPrefCommand extends BaseCli {
264
- public static readonly COMMAND = "unused -prefs-userjs" ;
259
+ class DefaultPrefsUserJSCommand extends BaseCli {
260
+ public static readonly COMMAND = "default -prefs-userjs" ;
265
261
public static readonly SMALL_DESCRIPTION = undefined ;
266
262
public static readonly DESCRIPTION =
267
- "Identify unused preferences from your user.js file" ;
263
+ "Identify default preferences from your user.js file" ;
268
264
public static readonly COMMANDS : readonly CliCommand [ ] = [ pathUsageUserJS ] ;
269
- public static readonly OPTIONS : readonly CliOption [ ] = [ pathToFirefox ] ;
265
+ public static readonly OPTIONS : readonly CliOption [ ] = [
266
+ pathToFirefox ,
267
+ ...printAndSave ,
268
+ ] ;
270
269
271
270
constructor ( fail : boolean = true ) {
272
271
super (
273
272
fail ,
274
- UnusedPrefCommand . DESCRIPTION ,
275
- UnusedPrefCommand . COMMANDS ,
276
- UnusedPrefCommand . OPTIONS ,
273
+ DefaultPrefsUserJSCommand . DESCRIPTION ,
274
+ DefaultPrefsUserJSCommand . COMMANDS ,
275
+ DefaultPrefsUserJSCommand . OPTIONS ,
277
276
) ;
278
277
}
279
278
280
279
public async entrypoint ( ) : Promise < void > {
281
- await unusedPrefs ( ) ;
280
+ const [ , , , compareUserjs ] = process . argv ;
281
+ if ( compareUserjs === undefined ) {
282
+ this . usage ( ) ;
283
+ }
284
+ await defaultPrefsUserJS ( compareUserjs ) ;
282
285
}
283
286
}
284
287
285
- export class CleanCommand extends BaseCli {
286
- public static readonly COMMAND = "clean " ;
288
+ class DefaultPrefsCommand extends BaseCli {
289
+ public static readonly COMMAND = "default-prefs " ;
287
290
public static readonly SMALL_DESCRIPTION = undefined ;
288
- public static readonly DESCRIPTION = `Remove files generated by ${ APP_NAME } ` ;
291
+ public static readonly DESCRIPTION = `Get a list of all default prefs ` ;
289
292
public static readonly COMMANDS : readonly CliCommand [ ] = [ ] ;
290
293
public static readonly OPTIONS : readonly CliOption [ ] = [
291
- {
292
- longOption : `${ CLI_ARGS . KEEP } ${ CLI_VALUES . VERSION1 } ,${ CLI_VALUES . VERSION2 } ` ,
293
-
294
- doc : [
295
- "Specify one or more versions whose archives and binaries should be preserved during cleanup" ,
296
- "Provide a comma-separated list of versions to keep" ,
297
- ` Example: ${ CLI_ARGS . KEEP } ${ EXAMPLE_VERSION . OLD_VERSION } ,${ EXAMPLE_VERSION . NEW_VERSION } ` ,
298
- ] . join ( "\n" ) ,
299
- smallDoc : "Version to keep" ,
300
- } ,
301
- {
302
- longOption : CLI_ARGS . KEEP_ARCHIVES ,
303
- doc : "Keep all archives" ,
304
- } ,
305
- {
306
- longOption : CLI_ARGS . KEEP_SOURCES ,
307
- doc : "Keep all binaries" ,
308
- } ,
294
+ ...printAndSave ,
295
+ pathToFirefox ,
309
296
] ;
310
297
311
298
constructor ( fail : boolean = true ) {
312
299
super (
313
300
fail ,
314
- CleanCommand . DESCRIPTION ,
315
- CleanCommand . COMMANDS ,
316
- CleanCommand . OPTIONS ,
301
+ DefaultPrefsCommand . DESCRIPTION ,
302
+ DefaultPrefsCommand . COMMANDS ,
303
+ DefaultPrefsCommand . OPTIONS ,
317
304
) ;
318
305
}
319
306
320
307
public async entrypoint ( ) : Promise < void > {
321
- await clean ( ) ;
308
+ const { path } = getInstalledFirefoxPath ( ) ;
309
+ await getDefaultPrefs ( path ) ;
322
310
}
323
311
}
324
312
325
- export class DefaultPrefsCommand extends BaseCli {
326
- public static readonly COMMAND = "default-prefs" ;
327
- public static readonly SMALL_DESCRIPTION = undefined ;
328
- public static readonly DESCRIPTION = `Get a list of all default prefs` ;
329
- public static readonly COMMANDS : readonly CliCommand [ ] = [ ] ;
313
+ export interface Diff {
314
+ compareUserJS ?: string ;
315
+ oldVersion : string ;
316
+ newVersion : string ;
317
+ }
318
+
319
+ class DiffCommand extends BaseCli {
320
+ public static readonly COMMAND = "diff" ;
321
+ public static readonly SMALL_DESCRIPTION =
322
+ "Compare the default preferences of two firefox versions" ;
323
+ public static readonly DESCRIPTION =
324
+ DiffCommand . SMALL_DESCRIPTION + " and highlight differences" ;
325
+
326
+ public static readonly COMMANDS : readonly CliCommand [ ] = [
327
+ {
328
+ values : [ `<${ CLI_VALUES . OLD_VERSION } > <${ CLI_VALUES . NEW_VERSION } >` ] ,
329
+ doc : [
330
+ "First arg is the old version, second arg is the new version" ,
331
+ ` Example: diff ${ EXAMPLE_VERSION . OLD_VERSION } ${ EXAMPLE_VERSION . NEW_VERSION } ` ,
332
+ ] . join ( "\n" ) ,
333
+ } ,
334
+ ] ;
335
+
330
336
public static readonly OPTIONS : readonly CliOption [ ] = [
337
+ {
338
+ longOption : CLI_ARGS . CLEAN_ARCHIVES ,
339
+
340
+ doc : "Remove archives after retrieving preferences" ,
341
+ } ,
342
+ {
343
+ longOption : CLI_ARGS . CLEAN_SOURCES ,
344
+
345
+ doc : "Remove binaries after retrieving preferences" ,
346
+ } ,
331
347
...printAndSave ,
332
- pathToFirefox ,
348
+ {
349
+ longOption : `${ CLI_ARGS . COMPARE_USERJS } ${ CLI_VALUES . PATH_USAGE } ` ,
350
+ doc : "Check for removed or changed keys in the specified user.js file" ,
351
+ } ,
333
352
] ;
334
353
335
354
constructor ( fail : boolean = true ) {
336
355
super (
337
356
fail ,
338
- DefaultPrefsCommand . DESCRIPTION ,
339
- DefaultPrefsCommand . COMMANDS ,
340
- DefaultPrefsCommand . OPTIONS ,
357
+ DiffCommand . DESCRIPTION ,
358
+ DiffCommand . COMMANDS ,
359
+ DiffCommand . OPTIONS ,
341
360
) ;
342
361
}
343
362
344
363
public async entrypoint ( ) : Promise < void > {
345
- await getDefaultPrefs ( ) ;
364
+ const [ , , , oldVersion , newVersion ] = process . argv ;
365
+
366
+ if (
367
+ ! oldVersion ||
368
+ ! newVersion ||
369
+ ! startsWithNumberDotNumber ( oldVersion ) ||
370
+ ! startsWithNumberDotNumber ( newVersion )
371
+ ) {
372
+ this . usage ( ) ;
373
+ }
374
+
375
+ const compareUserJS = getArgumentValue ( CLI_ARGS . COMPARE_USERJS ) ;
376
+
377
+ await diff ( { compareUserJS, oldVersion, newVersion } ) ;
346
378
}
347
379
}
348
380
349
- export class DefaultPrefsUserJSCommand extends BaseCli {
350
- public static readonly COMMAND = "default -prefs-userjs" ;
381
+ class UnusedPrefCommand extends BaseCli {
382
+ public static readonly COMMAND = "unused -prefs-userjs" ;
351
383
public static readonly SMALL_DESCRIPTION = undefined ;
352
384
public static readonly DESCRIPTION =
353
- "Identify default preferences from your user.js file" ;
385
+ "Identify unused preferences from your user.js file" ;
354
386
public static readonly COMMANDS : readonly CliCommand [ ] = [ pathUsageUserJS ] ;
355
- public static readonly OPTIONS : readonly CliOption [ ] = [
356
- pathToFirefox ,
357
- ...printAndSave ,
358
- ] ;
387
+ public static readonly OPTIONS : readonly CliOption [ ] = [ pathToFirefox ] ;
359
388
360
389
constructor ( fail : boolean = true ) {
361
390
super (
362
391
fail ,
363
- DefaultPrefsUserJSCommand . DESCRIPTION ,
364
- DefaultPrefsUserJSCommand . COMMANDS ,
365
- DefaultPrefsUserJSCommand . OPTIONS ,
392
+ UnusedPrefCommand . DESCRIPTION ,
393
+ UnusedPrefCommand . COMMANDS ,
394
+ UnusedPrefCommand . OPTIONS ,
366
395
) ;
367
396
}
368
397
369
398
public async entrypoint ( ) : Promise < void > {
370
- await defaultPrefsUserJS ( ) ;
399
+ const [ , , , compareUserjs ] = process . argv ;
400
+ if ( compareUserjs === undefined ) {
401
+ this . usage ( ) ;
402
+ }
403
+ await unusedPrefs ( compareUserjs ) ;
371
404
}
372
405
}
373
406
@@ -378,6 +411,7 @@ export const ALL_COMMANDS = [
378
411
UnusedPrefCommand ,
379
412
DefaultPrefsUserJSCommand ,
380
413
] ;
414
+
381
415
export class Cli extends BaseCli {
382
416
public static readonly COMMANDS : readonly CliCommand [ ] = ALL_COMMANDS . map (
383
417
( cmd ) => ( {
0 commit comments