Skip to content

Commit

Permalink
feat: check if origin folder exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Adán Toscano López authored and jorgecasar committed Jan 12, 2024
1 parent 528a1c8 commit 79e85a2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
25 changes: 24 additions & 1 deletion src/services/start-mock-server.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,7 +14,8 @@ import OpenApiMocker from '@os3/open-api-mocker';
* @returns {Promise<void>}
*/
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,
Expand All @@ -26,4 +29,24 @@ async function startMockServer(schemas) {
}
}

/**
* Validate schemas
* @async
* @function validateSchemas
* @param {Schema[]} schemas - An array of schemas
* @returns {Promise<Schema[]>}
*/
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;
2 changes: 1 addition & 1 deletion src/services/user-flow-steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down

0 comments on commit 79e85a2

Please sign in to comment.