From 8ef890eeaaba682bf24ff57e38ea81513a5fd744 Mon Sep 17 00:00:00 2001 From: Mohacyr Rojas Date: Thu, 19 Oct 2023 22:06:46 -0600 Subject: [PATCH] feat(core): change user flow - 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 --- src/index.js | 18 ++------- src/services/user-flow-steps.js | 71 ++++++++++++++++++++++++--------- src/services/utils.js | 17 ++++++++ 3 files changed, 72 insertions(+), 34 deletions(-) diff --git a/src/index.js b/src/index.js index 8617f15..8448151 100644 --- a/src/index.js +++ b/src/index.js @@ -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'; /** @@ -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(); diff --git a/src/services/user-flow-steps.js b/src/services/user-flow-steps.js index 781811f..7ef22b4 100644 --- a/src/services/user-flow-steps.js +++ b/src/services/user-flow-steps.js @@ -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) @@ -17,13 +17,18 @@ import { addToGitignore, verifyRemoteOrigin, TEMP_FOLDER_NAME, RC_FILE_NAME } fr * @returns {Promise} 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; } /** @@ -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?`, }); @@ -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} */ -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++; } } /** @@ -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 }; diff --git a/src/services/utils.js b/src/services/utils.js index bdba198..cce4a41 100644 --- a/src/services/utils.js +++ b/src/services/utils.js @@ -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} + */ +export function overwriteFile(filePath, content) { + fs.writeFileSync(filePath, content, (err) => { + if (err) { + console.error(err); + } else { + console.log('Config saved'); + } + }); +}