Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import and export using CLI #772

Merged
merged 7 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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