Skip to content

Commit

Permalink
Import and export using CLI (#772)
Browse files Browse the repository at this point in the history
* Import and export using CLI

Closes #678

* Adapt to new release changes

* PR suggestions

* Create unit tests

* Remove legacy tests

* Update expected output for import test

* Include commands in the README
  • Loading branch information
Mavbraz committed Nov 20, 2023
1 parent ffc9568 commit e2187fb
Show file tree
Hide file tree
Showing 12 changed files with 781 additions and 3 deletions.
47 changes: 47 additions & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ Mockoon's CLI has been tested on Node.js versions 18 and 20.

- [Start command](#start-command)
- [Dockerize command](#dockerize-command)
- [Import command](#import-command)
- [Export command](#export-command)
- [Help command](#help-command)

### Start command
Expand Down Expand Up @@ -163,6 +165,51 @@ EXAMPLES
$ mockoon-cli dockerize --data https://file-server/data.json --output ./Dockerfile
```

### Import command

Import a Swagger v2/OpenAPI v3 specification file (YAML or JSON).

The output file will not be prettified by default. You can prettify it using the `--prettify` flag described below.

Note: This command is similar to the app's import feature, but it will not import directly to your desktop app. If you need to import and open in your desktop app, use the app's import feature instead.

```
USAGE
$ mockoon-cli import
OPTIONS
-i, --input [required] Path or URL to your Swagger v2/OpenAPI v3 file
-o, --output [required] Generated Mockoon path and name (e.g. `./environment.json`)
-p, --prettify Prettify output
-h, --help Show CLI help
EXAMPLES
$ mockoon-cli import --input ~/input.json --output ./output.json
$ mockoon-cli import --input ~/input.yaml --output ./output.json
$ mockoon-cli import --input ~/input.json --output ./output.json --prettify
```

### Export command

Export a mock API to an OpenAPI v3 specification file (JSON).

The output file will not be prettified by default. You can prettify it using the `--prettify` flag described below.

```
USAGE
$ mockoon-cli export
OPTIONS
-i, --input [required] Path or URL to your Mockoon data file
-o, --output [required] Generated OpenApi v3 path and name (e.g. `./output.json`)
-p, --prettify Prettify output
-h, --help Show CLI help
EXAMPLES
$ mockoon-cli export --input ~/input.json --output ./output.json
$ mockoon-cli export --input ~/input.json --output ./output.json --prettify
```

### Help command

Returns information about a command.
Expand Down
34 changes: 34 additions & 0 deletions packages/cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"@typescript-eslint/eslint-plugin": "6.7.3",
"@typescript-eslint/parser": "6.7.3",
"chai": "4.3.10",
"chai-exclude": "^2.1.0",
"eslint": "8.50.0",
"eslint-config-oclif": "5.0.0",
"eslint-config-oclif-typescript": "2.0.1",
Expand Down
60 changes: 60 additions & 0 deletions packages/cli/src/commands/export.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Environment } from '@mockoon/commons';
import { OpenAPIConverter } from '@mockoon/commons-server';
import { Command, Flags } from '@oclif/core';
import { promises as fs } from 'fs';
import { CLIMessages } from '../constants/cli-messages.constants';
import { parseDataFiles, prepareEnvironment } from '../libs/data';

export default class Export extends Command {
public static description =
'Export a mock API to an OpenAPI v3 specification file (JSON)';

public static examples = [
'$ mockoon-cli export --input ~/data.json --output ./output.json',
'$ mockoon-cli export --input ~/data.json --output ./output.json --prettify'
];

public static flags = {
input: Flags.string({
char: 'i',
description: 'Path or URL to your Mockoon data file',
required: true
}),
output: Flags.string({
char: 'o',
description: 'Generated OpenApi v3 path and name (e.g. `./output.json`)',
required: true
}),
prettify: Flags.boolean({
char: 'p',
description: 'Prettify output',
default: false
})
};

public async run(): Promise<void> {
const { flags: userFlags } = await this.parse(Export);

try {
const parsedEnvironments = await parseDataFiles([userFlags.input]);

if (parsedEnvironments.length !== 1) {
this.error(CLIMessages.ONLY_ONE_ENVIRONMENT_ALLOWED);
}

const environment: Environment = await prepareEnvironment({
environment: parsedEnvironments[0].environment,
userOptions: {}
});
const openApiConverter = new OpenAPIConverter();
const data: string = await openApiConverter.convertToOpenAPIV3(
environment,
userFlags.prettify
);

await fs.writeFile(userFlags.output, data, 'utf-8');
} catch (error: any) {
this.error(error.message);
}
}
}
57 changes: 57 additions & 0 deletions packages/cli/src/commands/import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Environment } from '@mockoon/commons';
import { Command, Flags } from '@oclif/core';
import { promises as fs } from 'fs';
import { CLIMessages } from '../constants/cli-messages.constants';
import { parseDataFiles } from '../libs/data';

export default class Import extends Command {
public static description =
'Import a Swagger v2/OpenAPI v3 specification file (YAML or JSON)';

public static examples = [
'$ mockoon-cli import --input ~/data.json --output ./output.json',
'$ mockoon-cli import --input ~/data.json --output ./output.json --prettify'
];

public static flags = {
input: Flags.string({
char: 'i',
description: 'Path or URL to your Swagger v2/OpenAPI v3 file',
required: true
}),
output: Flags.string({
char: 'o',
description:
'Generated Mockoon path and name (e.g. `./environment.json`)',
required: true
}),
prettify: Flags.boolean({
char: 'p',
description: 'Prettify output',
default: false
})
};

public async run(): Promise<void> {
const { flags: userFlags } = await this.parse(Import);

try {
const parsedEnvironments = await parseDataFiles([userFlags.input]);

if (parsedEnvironments.length !== 1) {
this.error(CLIMessages.ONLY_ONE_ENVIRONMENT_ALLOWED);
}

const environment: Environment = parsedEnvironments[0].environment;
const data: string = JSON.stringify(
environment,
null,
userFlags.prettify ? 2 : 0
);

await fs.writeFile(userFlags.output, data, 'utf-8');
} catch (error: any) {
this.error(error.message);
}
}
}
3 changes: 2 additions & 1 deletion packages/cli/src/constants/cli-messages.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export const CLIMessages = {
"These environment's data are too old or not a valid Mockoon environment.\nPlease verify or migrate them using a more recent version of the application",
DATA_TOO_RECENT_ERROR:
"These environment's data are too recent and cannot be run with the CLI\nPlease update the CLI with the following command 'npm install -g @mockoon/cli'",
ENVIRONMENT_NOT_AVAILABLE_ERROR: 'No environments exist in specified file'
ENVIRONMENT_NOT_AVAILABLE_ERROR: 'No environments exist in specified file',
ONLY_ONE_ENVIRONMENT_ALLOWED: 'Only one environment is allowed'
};
Loading

0 comments on commit e2187fb

Please sign in to comment.