(args: string[])
| 125 | } |
| 126 | |
| 127 | export const parseCliArgs = (args: string[]): ExportCliOptions => { |
| 128 | let compress = false |
| 129 | let format: CompressionFormat | undefined |
| 130 | let outputFilePath: string | undefined |
| 131 | |
| 132 | if (args.includes('--help') || args.includes('-h')) { |
| 133 | return { |
| 134 | compress, |
| 135 | format, |
| 136 | outputFilePath: DEFAULT_OUTPUT_FILE_PATH, |
| 137 | showHelp: true, |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | for (let index = 0; index < args.length; index++) { |
| 142 | const arg = args[index] |
| 143 | |
| 144 | if (arg === '--compress' || arg === '-z') { |
| 145 | compress = true |
| 146 | continue |
| 147 | } |
| 148 | |
| 149 | if (arg === '--format' || arg.startsWith('--format=')) { |
| 150 | const [rawFormat, nextIndex] = getOptionValue('--format', args, index) |
| 151 | format = parseCompressionFormat(rawFormat) |
| 152 | index = nextIndex |
| 153 | continue |
| 154 | } |
| 155 | |
| 156 | if (arg.startsWith('-')) { |
| 157 | throw new Error(`Unknown option: ${arg}`) |
| 158 | } |
| 159 | |
| 160 | if (outputFilePath) { |
| 161 | throw new Error(`Unexpected extra argument: ${arg}`) |
| 162 | } |
| 163 | |
| 164 | outputFilePath = arg |
| 165 | } |
| 166 | |
| 167 | if (!compress && format) { |
| 168 | throw new Error('--format requires --compress') |
| 169 | } |
| 170 | |
| 171 | outputFilePath = outputFilePath ?? DEFAULT_OUTPUT_FILE_PATH |
| 172 | |
| 173 | if (compress && !format) { |
| 174 | format = getCompressionFormatFromExtension(outputFilePath) ?? CompressionFormat.GZIP |
| 175 | } |
| 176 | |
| 177 | return { |
| 178 | compress, |
| 179 | format, |
| 180 | outputFilePath, |
| 181 | showHelp: false, |
| 182 | } |
| 183 | } |
| 184 |
no test coverage detected