|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import { CommandLineParser } from '@rushstack/ts-command-line/lib/providers/CommandLineParser'; |
| 5 | +import type { |
| 6 | + CommandLineFlagParameter, |
| 7 | + IRequiredCommandLineStringParameter, |
| 8 | + IRequiredCommandLineChoiceParameter, |
| 9 | + IRequiredCommandLineStringListParameter, |
| 10 | + CommandLineChoiceParameter |
| 11 | +} from '@rushstack/ts-command-line/lib/index'; |
| 12 | +import { InternalError } from '@rushstack/node-core-library/lib/InternalError'; |
| 13 | +import { Colorize } from '@rushstack/terminal/lib/Colorize'; |
| 14 | +import type { ConsoleTerminalProvider } from '@rushstack/terminal/lib/ConsoleTerminalProvider'; |
| 15 | +import type { ITerminal } from '@rushstack/terminal/lib/ITerminal'; |
| 16 | + |
| 17 | +import { type IZipMode, zipSync } from './zipSync'; |
| 18 | + |
| 19 | +export class ZipSyncCommandLineParser extends CommandLineParser { |
| 20 | + private readonly _debugParameter: CommandLineFlagParameter; |
| 21 | + private readonly _verboseParameter: CommandLineFlagParameter; |
| 22 | + private readonly _modeParameter: IRequiredCommandLineChoiceParameter<IZipMode>; |
| 23 | + private readonly _archivePathParameter: IRequiredCommandLineStringParameter; |
| 24 | + private readonly _baseDirParameter: IRequiredCommandLineStringParameter; |
| 25 | + private readonly _targetDirectoriesParameter: IRequiredCommandLineStringListParameter; |
| 26 | + private readonly _compressionParameter: CommandLineChoiceParameter<'store' | 'deflate' | 'auto'>; |
| 27 | + private readonly _terminal: ITerminal; |
| 28 | + private readonly _terminalProvider: ConsoleTerminalProvider; |
| 29 | + |
| 30 | + public constructor(terminalProvider: ConsoleTerminalProvider, terminal: ITerminal) { |
| 31 | + super({ |
| 32 | + toolFilename: 'zipsync', |
| 33 | + toolDescription: '' |
| 34 | + }); |
| 35 | + |
| 36 | + this._terminal = terminal; |
| 37 | + this._terminalProvider = terminalProvider; |
| 38 | + |
| 39 | + this._debugParameter = this.defineFlagParameter({ |
| 40 | + parameterLongName: '--debug', |
| 41 | + parameterShortName: '-d', |
| 42 | + description: 'Show the full call stack if an error occurs while executing the tool' |
| 43 | + }); |
| 44 | + |
| 45 | + this._verboseParameter = this.defineFlagParameter({ |
| 46 | + parameterLongName: '--verbose', |
| 47 | + parameterShortName: '-v', |
| 48 | + description: 'Show verbose output' |
| 49 | + }); |
| 50 | + |
| 51 | + this._modeParameter = this.defineChoiceParameter<IZipMode>({ |
| 52 | + parameterLongName: '--mode', |
| 53 | + parameterShortName: '-m', |
| 54 | + description: |
| 55 | + 'The mode of operation: "pack" to create a zip archive, or "unpack" to extract files from a zip archive', |
| 56 | + alternatives: ['pack', 'unpack'], |
| 57 | + required: true |
| 58 | + }); |
| 59 | + |
| 60 | + this._archivePathParameter = this.defineStringParameter({ |
| 61 | + parameterLongName: '--archive-path', |
| 62 | + parameterShortName: '-a', |
| 63 | + description: 'Zip file path', |
| 64 | + argumentName: 'ARCHIVE_PATH', |
| 65 | + required: true |
| 66 | + }); |
| 67 | + |
| 68 | + this._targetDirectoriesParameter = this.defineStringListParameter({ |
| 69 | + parameterLongName: '--target-directory', |
| 70 | + parameterShortName: '-t', |
| 71 | + description: 'Target directories to pack or unpack', |
| 72 | + argumentName: 'TARGET_DIRECTORIES', |
| 73 | + required: true |
| 74 | + }); |
| 75 | + |
| 76 | + this._baseDirParameter = this.defineStringParameter({ |
| 77 | + parameterLongName: '--base-dir', |
| 78 | + parameterShortName: '-b', |
| 79 | + description: 'Base directory for relative paths within the archive', |
| 80 | + argumentName: 'BASE_DIR', |
| 81 | + required: true |
| 82 | + }); |
| 83 | + |
| 84 | + this._compressionParameter = this.defineChoiceParameter<'store' | 'deflate' | 'auto'>({ |
| 85 | + parameterLongName: '--compression', |
| 86 | + parameterShortName: '-z', |
| 87 | + description: |
| 88 | + 'Compression strategy when packing. "deflate" attempts DEFLATE for every file (keeps only if smaller); "auto" first skips likely-compressed types before attempting; "store" disables compression.', |
| 89 | + alternatives: ['store', 'deflate', 'auto'], |
| 90 | + required: true |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + protected override async onExecuteAsync(): Promise<void> { |
| 95 | + if (this._debugParameter.value) { |
| 96 | + InternalError.breakInDebugger = true; |
| 97 | + this._terminalProvider.debugEnabled = true; |
| 98 | + this._terminalProvider.verboseEnabled = true; |
| 99 | + } |
| 100 | + if (this._verboseParameter.value) { |
| 101 | + this._terminalProvider.verboseEnabled = true; |
| 102 | + } |
| 103 | + try { |
| 104 | + zipSync({ |
| 105 | + terminal: this._terminal, |
| 106 | + mode: this._modeParameter.value, |
| 107 | + archivePath: this._archivePathParameter.value, |
| 108 | + targetDirectories: this._targetDirectoriesParameter.values, |
| 109 | + baseDir: this._baseDirParameter.value, |
| 110 | + compression: (this._compressionParameter.value as 'store' | 'deflate' | 'auto' | undefined) ?? 'auto' |
| 111 | + }); |
| 112 | + } catch (error) { |
| 113 | + if (this._debugParameter.value) { |
| 114 | + console.error('\n' + error.stack); |
| 115 | + } else { |
| 116 | + console.error('\n' + Colorize.red('ERROR: ' + error.message.trim())); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments