-
-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
12 changed files
with
781 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.