(args: string[] = process.argv.slice(2), options: ExportOptions = {})
| 256 | } |
| 257 | |
| 258 | export async function runExportEvents(args: string[] = process.argv.slice(2), options: ExportOptions = {}): Promise<number> { |
| 259 | const useStructuredFormat = Boolean(options.format) |
| 260 | const structuredFormat = resolveExportFormat(options.format) |
| 261 | const cliOptions = useStructuredFormat ? undefined : parseCliArgs(args) |
| 262 | |
| 263 | if (!useStructuredFormat && cliOptions?.showHelp) { |
| 264 | printUsage() |
| 265 | return 0 |
| 266 | } |
| 267 | |
| 268 | const outputPath = useStructuredFormat |
| 269 | ? resolveOutputPath(args[0], structuredFormat) |
| 270 | : path.resolve(cliOptions?.outputFilePath ?? DEFAULT_OUTPUT_FILE_PATH) |
| 271 | |
| 272 | const db = getMasterDbClient() |
| 273 | const abortController = new AbortController() |
| 274 | let interruptedBySignal: NodeJS.Signals | undefined |
| 275 | |
| 276 | const onSignal = (signal: NodeJS.Signals) => { |
| 277 | if (abortController.signal.aborted) { |
| 278 | return |
| 279 | } |
| 280 | |
| 281 | interruptedBySignal = signal |
| 282 | process.exitCode = 130 |
| 283 | console.log(`${signal} received. Stopping export...`) |
| 284 | abortController.abort() |
| 285 | } |
| 286 | |
| 287 | process.on('SIGINT', onSignal).on('SIGTERM', onSignal) |
| 288 | |
| 289 | try { |
| 290 | const firstEvent = await db('events').select('event_id').whereNull('deleted_at').first() |
| 291 | |
| 292 | if (abortController.signal.aborted) { |
| 293 | return 130 |
| 294 | } |
| 295 | |
| 296 | if (!firstEvent) { |
| 297 | if (options.json) { |
| 298 | console.log(JSON.stringify({ exported: 0, outputPath, empty: true }, null, 2)) |
| 299 | } else { |
| 300 | console.log('No events to export.') |
| 301 | } |
| 302 | return 0 |
| 303 | } |
| 304 | |
| 305 | if (useStructuredFormat) { |
| 306 | console.log(`Exporting events to ${outputPath}`) |
| 307 | } else if (cliOptions?.format) { |
| 308 | console.log(`Exporting events to ${outputPath} using ${getCompressionLabel(cliOptions.format)} compression`) |
| 309 | } else { |
| 310 | console.log(`Exporting events to ${outputPath}`) |
| 311 | } |
| 312 | |
| 313 | const startedAt = Date.now() |
| 314 | const output = fs.createWriteStream(outputPath) |
| 315 |
no test coverage detected