| 24 | } |
| 25 | |
| 26 | export const go = () => |
| 27 | yargs(hideBin(process.argv)) |
| 28 | .version(packageJson.version) |
| 29 | .command<CliOptions>( |
| 30 | '$0 [harFilePath]', |
| 31 | 'the default command', |
| 32 | builder => { |
| 33 | builder |
| 34 | .option('target', { |
| 35 | alias: 't', |
| 36 | type: 'string', |
| 37 | description: 'target output', |
| 38 | requiresArg: true, |
| 39 | }) |
| 40 | .option('client', { |
| 41 | alias: 'c', |
| 42 | type: 'string', |
| 43 | description: 'language client', |
| 44 | requiresArg: true, |
| 45 | }) |
| 46 | .option('output', { |
| 47 | alias: 'o', |
| 48 | type: 'string', |
| 49 | description: 'write output to directory', |
| 50 | }) |
| 51 | .option('options', { |
| 52 | alias: 'x', |
| 53 | type: 'string', |
| 54 | description: 'provide extra options for the target/client', |
| 55 | requiresArg: true, |
| 56 | }) |
| 57 | .demandOption(['target'], 'please provide a target') |
| 58 | .strict() |
| 59 | .showHelpOnFail(true) |
| 60 | .help(); |
| 61 | }, |
| 62 | ({ target: targetId, client, output, options, harFilePath }) => { |
| 63 | const har = JSON.parse(readFileSync(harFilePath).toString()) as HarRequest; |
| 64 | const httpsnippet = new HTTPSnippet(har); |
| 65 | |
| 66 | try { |
| 67 | if (options) { |
| 68 | options = JSON.parse(options); |
| 69 | } |
| 70 | } catch (error) { |
| 71 | if (error instanceof Error) { |
| 72 | bad(`${cyan.bold(harFilePath)} failed to read JSON: ${red(error.message)}`); |
| 73 | } |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | const result = httpsnippet.convert(targetId, client, options); |
| 78 | |
| 79 | if (!result) { |
| 80 | throw new Error('something went wrong'); |
| 81 | } |
| 82 | |
| 83 | if (!output) { |