diff --git a/src/services/start-mock-server.js b/src/services/start-mock-server.js index 242ae85..c2f8b7f 100644 --- a/src/services/start-mock-server.js +++ b/src/services/start-mock-server.js @@ -1,4 +1,6 @@ +import fs from 'fs'; import OpenApiMocker from '@os3/open-api-mocker'; +import { init } from './user-flow-steps.js'; /** * @typedef {import('../types/schema.js').Schema} Schema @@ -12,7 +14,8 @@ import OpenApiMocker from '@os3/open-api-mocker'; * @returns {Promise} */ async function startMockServer(schemas) { - for (const schema of schemas) { + const validatedSchemas = await validateSchemas(schemas); + for (const schema of validatedSchemas) { const openApiMocker = new OpenApiMocker({ port: schema.port, schema: schema.path, @@ -26,4 +29,24 @@ async function startMockServer(schemas) { } } +/** + * Validate schemas + * @async + * @function validateSchemas + * @param {Schema[]} schemas - An array of schemas + * @returns {Promise} + */ +async function validateSchemas(schemas) { + const allSchemasExists = schemas.reduce( + (acc, schema) => (fs.existsSync(`${process.cwd()}/${schema.path}`) ? acc : false), + true + ); + if (!allSchemasExists) { + console.log('Any schema does not exists'); + const config = await init(); + return config.selectedSchemas; + } + return schemas; +} + export default startMockServer; diff --git a/src/services/user-flow-steps.js b/src/services/user-flow-steps.js index bbd4ca5..1b2d517 100644 --- a/src/services/user-flow-steps.js +++ b/src/services/user-flow-steps.js @@ -24,7 +24,7 @@ async function initWithConfigFile() { const useExistingConfig = await confirm({ message: 'Do you want to use the existing config?', }); - return useExistingConfig ? existingConfig : await init(); + return useExistingConfig ? existingConfig : init(); } /**