Skip to content

Commit

Permalink
feat(core): change user flow
Browse files Browse the repository at this point in the history
- ask for schemas before asking for ports
- add ports and selected schemas to config
- improve start with config file (+1 squashed commit)
Squashed commits:
[7cf24a3] feat(core): change flow order

- Add a new step to ask for ports
- Ask for ports after asking for schemas
- Write schemas and ports array on rc file
  • Loading branch information
MHCYR committed Oct 20, 2023
1 parent 4e1f97f commit 8ef890e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 34 deletions.
18 changes: 3 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { checkbox } from '@inquirer/prompts';
import * as fs from 'node:fs';
import { initWithConfigFile, initNoConfigFile, getSchemas, startMockServer } from './services/user-flow-steps.js';
import { initWithConfigFile, startMockServer, startFlow } from './services/user-flow-steps.js';
import { RC_FILE_NAME } from './services/utils.js';

/**
Expand All @@ -11,18 +10,7 @@ import { RC_FILE_NAME } from './services/utils.js';
*/
const main = async () => {
const configFileExists = fs.existsSync(`${process.cwd()}/${RC_FILE_NAME}`);

const config = configFileExists ? await initWithConfigFile() : await initNoConfigFile();

const schemas = await getSchemas(config.schemasOrigin);

const selectedSchemas = await checkbox({
message: 'Select a schema',
choices: schemas.map((schema) => {
return { name: schema.fileName, value: schema.filePath };
}),
});
await startMockServer(config.initialPort, selectedSchemas);
const config = configFileExists ? await initWithConfigFile() : await startFlow();
await startMockServer(config.ports, config.selectedSchemas);
};

main();
71 changes: 52 additions & 19 deletions src/services/user-flow-steps.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { input, confirm } from '@inquirer/prompts';
import { input, confirm, checkbox } from '@inquirer/prompts';
import * as fs from 'node:fs';
import OpenApiMocker from 'open-api-mocker';
import cloneGitRepository from '../services/clone-git-repository.js';
import findOasFromDir from '../services/find-oas-from-dir.js';
import { addToGitignore, verifyRemoteOrigin, TEMP_FOLDER_NAME, RC_FILE_NAME } from './utils.js';
import { addToGitignore, verifyRemoteOrigin, TEMP_FOLDER_NAME, RC_FILE_NAME, overwriteFile } from './utils.js';
/**
* @typedef {Object} Config
* @property {string} schemasOrigin - The origin of the schemas (local or remote)
Expand All @@ -17,13 +17,18 @@ import { addToGitignore, verifyRemoteOrigin, TEMP_FOLDER_NAME, RC_FILE_NAME } fr
* @returns {Promise<Config>} A object with the initial values from the user
*/
async function initWithConfigFile() {
let config;
const existingConfig = JSON.parse(fs.readFileSync(`${process.cwd()}/${RC_FILE_NAME}`));
console.table(existingConfig);
console.log(existingConfig);
const useExistingConfig = await confirm({
message: 'Do you want to use the existing config?',
});

return useExistingConfig ? existingConfig : initNoConfigFile();
if (useExistingConfig) {
config = existingConfig;
} else {
config = await startFlow();
}
return config;
}

/**
Expand All @@ -34,7 +39,6 @@ async function initWithConfigFile() {
*/
async function initNoConfigFile() {
const config = await getInitialValues();

const addRcFileToGitignore = await confirm({
message: `Add ${RC_FILE_NAME} to .gitignore?`,
});
Expand Down Expand Up @@ -69,23 +73,21 @@ async function getSchemas(origin) {
* Start the mock server
* @async
* @function startMockServer
* @param {number} port - The port to start the mock server
* @param {number[]} ports - ports for each schema
* @param {string[]} schemas - An array of schemas
* @returns {Promise<void>}
*/
async function startMockServer(port, schemas) {
let currentPort = port;
for (const schema of schemas) {
async function startMockServer(ports, schemas) {
for (let i = 0; i < schemas.length; i++) {
const openApiMocker = new OpenApiMocker({
port: currentPort,
schema: schema,
port: ports[i],
schema: schemas[i],
watch: true,
});

await openApiMocker.validate();

await openApiMocker.mock();
currentPort++;
}
}
/**
Expand All @@ -98,16 +100,47 @@ async function getInitialValues() {
const schemasOrigin = await input({
message: 'Enter the repo url or relative path',
});
const initialPort = await input({
message: 'Enter the initial port',
default: 1234,
});

const config = {
schemasOrigin,
initialPort,
};
return config;
}

export { initWithConfigFile, initNoConfigFile, getSchemas, startMockServer };
async function startFlow() {
let config;
config = await initNoConfigFile();

const schemas = await getSchemas(config.schemasOrigin);

const selectedSchemas = await checkbox({
message: 'Select a schema',
choices: schemas.map((schema) => {
return { name: schema.fileName, value: schema.filePath };
}),
});

config.selectedSchemas = selectedSchemas;
const ports = await askForPorts(selectedSchemas);
config.ports = ports;

overwriteFile(`${process.cwd()}/${RC_FILE_NAME}`, JSON.stringify(config));
return config;
}

async function askForPorts(selectedSchemas) {
let ports = [];
let suggestedPort = 1234;
for (const schema of selectedSchemas) {
const port = await input({
message: `Select a port for ${schema}`,
default: suggestedPort,
});
ports.push(port);
suggestedPort++;
}

return ports;
}

export { initWithConfigFile, initNoConfigFile, getSchemas, startMockServer, startFlow };
17 changes: 17 additions & 0 deletions src/services/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,20 @@ export function verifyRemoteOrigin(origin) {
const isOriginRemote = isOriginRemoteRegex.test(origin);
return isOriginRemote;
}
/**
* Overwrites the document on a file
* @async
* @function overwriteFile
* @param {string} filePath - The path of the file to overwrite
* @param {string} content - The content to overwrite
* @returns {Promise<void>}
*/
export function overwriteFile(filePath, content) {
fs.writeFileSync(filePath, content, (err) => {
if (err) {
console.error(err);
} else {
console.log('Config saved');
}
});
}

0 comments on commit 8ef890e

Please # to comment.