diff --git a/README.md b/README.md
index b3afc23ed..c8a317073 100644
--- a/README.md
+++ b/README.md
@@ -86,10 +86,13 @@ console.log(output);
|
npm install --global httpsnippet
| yarn global add httpsnippet
|
```text
-httpsnippet [harFilePath]
+httpsnippet
the default command
+Positionals:
+ harFilePaths HAR input files
+
Options:
--help Show help [boolean]
--version Show version number [boolean]
@@ -102,6 +105,12 @@ Examples:
httpsnippet my_har.json --target rust --client actix --output my_src_directory
```
+To convert multiple files, pass their paths or let your shell expand a glob:
+
+```shell
+httpsnippet ./requests/*.json --target shell --client curl --output ./snippets
+```
+
### Example
The input to HTTPSnippet is any valid [HAR Request Object](http://www.softwareishard.com/blog/har-12-spec/#request), or full [HAR](http://www.softwareishard.com/blog/har-12-spec/#log) log format.
diff --git a/src/cli.test.ts b/src/cli.test.ts
new file mode 100644
index 000000000..494cafe60
--- /dev/null
+++ b/src/cli.test.ts
@@ -0,0 +1,58 @@
+import { mkdtempSync, readFileSync, rmSync } from 'fs';
+import { tmpdir } from 'os';
+import path from 'path';
+
+import { go } from './cli';
+
+const fixtureDir = path.join(__dirname, 'fixtures', 'requests');
+const shortHar = path.join(fixtureDir, 'short.json');
+const httpsHar = path.join(fixtureDir, 'https.json');
+
+const runCli = (...args: string[]) => {
+ const originalArgv = process.argv;
+ process.argv = [process.execPath, 'httpsnippet', ...args];
+ try {
+ return go();
+ } finally {
+ process.argv = originalArgv;
+ }
+};
+
+describe('command line', () => {
+ it('writes a snippet for each input file to a new output directory', () => {
+ const tempDir = mkdtempSync(path.join(tmpdir(), 'httpsnippet-'));
+ const output = path.join(tempDir, 'snippets');
+ jest.spyOn(console, 'log').mockImplementation(() => undefined);
+ jest.spyOn(console, 'error').mockImplementation(() => undefined);
+ jest.spyOn(process, 'exit').mockImplementation(code => {
+ throw new Error(`CLI exited with status ${code}`);
+ });
+
+ try {
+ runCli(shortHar, httpsHar, '--target', 'shell', '--client', 'curl', '--output', output);
+
+ expect(readFileSync(path.join(output, 'short.sh'), 'utf8')).toContain(
+ 'http://mockbin.com/har',
+ );
+ expect(readFileSync(path.join(output, 'https.sh'), 'utf8')).toContain(
+ 'https://mockbin.com/har',
+ );
+ } finally {
+ jest.restoreAllMocks();
+ rmSync(tempDir, { recursive: true, force: true });
+ }
+ });
+
+ it('prints a single input snippet to stdout when no output directory is given', () => {
+ const log = jest.spyOn(console, 'log').mockImplementation(() => undefined);
+
+ try {
+ runCli(shortHar, '--target', 'shell');
+
+ expect(log).toHaveBeenCalledTimes(1);
+ expect(log).toHaveBeenCalledWith(expect.stringContaining('http://mockbin.com/har'));
+ } finally {
+ log.mockRestore();
+ }
+ });
+});
diff --git a/src/cli.ts b/src/cli.ts
index ffc98cd80..60f82cec8 100644
--- a/src/cli.ts
+++ b/src/cli.ts
@@ -1,5 +1,5 @@
import chalk from 'chalk';
-import { readFileSync, writeFileSync } from 'fs';
+import { mkdirSync, readFileSync, writeFileSync } from 'fs';
import path from 'path';
import { hideBin } from 'yargs/helpers';
import yargs from 'yargs/yargs';
@@ -19,7 +19,7 @@ interface CliOptions {
target: TargetId;
client?: ClientId;
output?: string;
- harFilePath: string;
+ harFilePaths: string[];
options?: any;
}
@@ -27,10 +27,14 @@ export const go = () =>
yargs(hideBin(process.argv))
.version(packageJson.version)
.command(
- '$0 [harFilePath]',
+ '$0 ',
'the default command',
builder => {
builder
+ .positional('harFilePaths', {
+ type: 'string',
+ describe: 'HAR input files',
+ })
.option('target', {
alias: 't',
type: 'string',
@@ -59,42 +63,46 @@ export const go = () =>
.showHelpOnFail(true)
.help();
},
- ({ target: targetId, client, output, options, harFilePath }) => {
- const har = JSON.parse(readFileSync(harFilePath).toString()) as HarRequest;
- const httpsnippet = new HTTPSnippet(har);
-
+ ({ target: targetId, client, output, options, harFilePaths }) => {
try {
if (options) {
options = JSON.parse(options);
}
} catch (error) {
if (error instanceof Error) {
- bad(`${cyan.bold(harFilePath)} failed to read JSON: ${red(error.message)}`);
+ bad(`failed to parse --options as JSON: ${red(error.message)}`);
}
return;
}
- const result = httpsnippet.convert(targetId, client, options);
-
- if (!result) {
- throw new Error('something went wrong');
+ if (output) {
+ mkdirSync(output, { recursive: true });
}
- if (!output) {
- console.log(result);
- return;
- }
+ for (const harFilePath of harFilePaths) {
+ const har = JSON.parse(readFileSync(harFilePath, 'utf8')) as HarRequest;
+ const result = new HTTPSnippet(har).convert(targetId, client, options);
- const file = path.parse(harFilePath).name;
- const writeFilePath = path.join(output, `${file}${extname(targetId)}`);
- writeFileSync(writeFilePath, String(result));
- const target = targets[targetId];
- const clientId = target.clientsById[client || target.info.default].info.key;
- good(
- `converted ${cyan.bold(harFilePath)} with ${yellow(targetId)}[${yellow(
- clientId,
- )}] at ${cyan(writeFilePath)}\n\n${result}`,
- );
+ if (!result) {
+ throw new Error('something went wrong');
+ }
+
+ if (!output) {
+ console.log(result);
+ continue;
+ }
+
+ const file = path.parse(harFilePath).name;
+ const writeFilePath = path.join(output, `${file}${extname(targetId)}`);
+ writeFileSync(writeFilePath, String(result));
+ const target = targets[targetId];
+ const clientId = target.clientsById[client || target.info.default].info.key;
+ good(
+ `converted ${cyan.bold(harFilePath)} with ${yellow(targetId)}[${yellow(
+ clientId,
+ )}] at ${cyan(writeFilePath)}\n\n${result}`,
+ );
+ }
},
)
.example('$0 my_har.json', '--target rust --client actix --output my_src_directory').argv;