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