Skip to content

Commit

Permalink
fix: improve user messages
Browse files Browse the repository at this point in the history
  • Loading branch information
javier-sierra-sngular authored and jorgecasar committed Jan 12, 2024
1 parent b4b5e79 commit 6d4b0b9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/services/gitignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from 'node:fs';
import path from 'node:path';

import { checkStringInFile } from './check-string-in-file.js';
import { messages } from '../utils/messages.js';

export const GITIGNORE_PATH = path.join(process.cwd(), '.gitignore');

Expand All @@ -20,7 +21,7 @@ export default async function addToGitignore(fileName) {
const existsGitignoreFile = fs.existsSync(GITIGNORE_PATH);
if (
(!existsGitignoreFile || !(await isInGitignore(fileName))) &&
(await confirm({ message: `Add ${fileName} to .gitignore?` }))
(await confirm({ message: messages.CONFIRM_ADD_TO_GITIGNORE(fileName) }))
) {
const leadingCharacter = existsGitignoreFile ? getLeadingCharacter() : '';
fs.appendFileSync(GITIGNORE_PATH, `${leadingCharacter}${fileName}\n`);
Expand Down
7 changes: 4 additions & 3 deletions src/services/inquirer-validators.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import fs from 'fs';

import { verifyRemoteOrigin } from './utils.js';
import { messages } from '../utils/messages.js';

/**
* @typedef {import('../types/types.d.js').Schema} Schema
*/

export const errorMessages = Object.freeze({
origin: {
INVALID: 'Enter a valid remote origin (https:// or git@) or local path',
INVALID: messages.VALIDATION_INVALID_ORIGIN,
},
port: {
IN_USE: 'Port already in use',
INVALID: 'Enter a valid port number between 0 and 65535',
IN_USE: messages.VALIDATION_PORT_IN_USE,
INVALID: messages.VALIDATION_INVALID_PORT,
},
});

Expand Down
10 changes: 5 additions & 5 deletions src/services/user-flow-steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async function initWithConfigFile() {
const existingConfig = /** @type {Config} */ (JSON.parse(fileContent)) || {};
Logger.info(messages.CURRENT_CONFIG, existingConfig);
const useExistingConfig = await confirm({
message: 'Do you want to use the existing config?',
message: messages.CONFIRM_EXISTING_CONFIG,
});
return useExistingConfig ? existingConfig : userFlowSteps.init();
}
Expand Down Expand Up @@ -62,7 +62,7 @@ async function getSchemas(origin) {
*/
async function getOrigin() {
const schemasOrigin = await input({
message: 'Enter a remote origin (https:// or git@) or local path',
message: messages.INPUT_ORIGIN,
validate: originValidator,
});

Expand Down Expand Up @@ -91,7 +91,7 @@ async function init({ origin, schemaPaths, ports } = { schemaPaths: [], ports: [
const schemasToMock = schemaPathsAreAvailable
? schemaPaths
: await checkbox({
message: 'Select a schema',
message: messages.CHOOSE_FILES,
choices: schemas.map((schema) => {
return { value: schema.filePath };
}),
Expand All @@ -103,7 +103,7 @@ async function init({ origin, schemaPaths, ports } = { schemaPaths: [], ports: [
const config = { schemasOrigin, selectedSchemas };

fs.writeFileSync(path.join(process.cwd(), RC_FILE_NAME), JSON.stringify(config, null, '\t'));
Logger.info(messages.SAVED_CONFIG, config);
Logger.info(messages.SAVED_CONFIG(RC_FILE_NAME), config);
await addToGitignore(RC_FILE_NAME);

return config;
Expand Down Expand Up @@ -139,7 +139,7 @@ async function askForPorts(schemaPaths) {
let suggestedPort = 1234;
for (const schemaPath of schemaPaths) {
const port = await input({
message: `Select a port for ${schemaPath}`,
message: messages.INPUT_PORT(schemaPath),
default: suggestedPort.toString(),
validate: (input) => portValidator(input, selectedSchemas),
});
Expand Down
19 changes: 14 additions & 5 deletions src/utils/messages.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
export const messages = Object.freeze({
CONFIG_FILE_NOT_FOUND: 'Could not find the configuration file',
CHOOSE_FILES: 'Choose the files you want to use:',
CONFIG_FILE_NOT_FOUND: 'Could not find the configuration file named:',
CONFIRM_ADD_TO_GITIGNORE: (/** @type {string} */ fileName) => `Add ${fileName} to .gitignore?:`,
CONFIRM_EXISTING_CONFIG: 'Do you want to use the existing config?:',
CURRENT_CONFIG: 'Current configuration:',
DIRECTORY_NOT_FOUND: 'Could not find the directory',
OPENAPI_SCHEMA_NOT_FOUND_ERROR_MSG: 'No OpenAPI schema found in the given directory',
SAVED_CONFIG: 'Saved configuration:',
SOME_SCHEMA_DOES_NOT_EXIST: 'Some schema does not exist',
DIRECTORY_NOT_FOUND: 'Could not find the directory named:',
INPUT_ORIGIN:
'Enter a remote origin (https:// or git@) or a local path where the OpenAPI Specification files are located:',
INPUT_PORT: (/** @type {string} */ schemaPath) => `Enter a port number for ${schemaPath}:`,
OPENAPI_SCHEMA_NOT_FOUND_ERROR_MSG: 'No OpenAPI schema found in the given directory.',
SAVED_CONFIG: (/** @type {string} */ configFile) => `Configuration saved in ${configFile}:`,
SOME_SCHEMA_DOES_NOT_EXIST: 'Some schema does not exist.',
USING_PROVIDED_CONFIG: 'Using provided configuration:',
VALIDATION_INVALID_ORIGIN: 'Enter a valid remote origin (https:// or git@) or local path.',
VALIDATION_INVALID_PORT: 'Enter a valid port number between 0 and 65535.',
VALIDATION_PORT_IN_USE: 'Port already in use.',
});
14 changes: 11 additions & 3 deletions test/unit/services/gitignore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { restore, stub, match } from 'sinon';
import sinonChai from 'sinon-chai';

import { checkStringInFile } from '../../../src/services/check-string-in-file.js';
import { messages } from '../../../src/utils/messages.js';

use(sinonChai);

let confirmStub = stub();
Expand Down Expand Up @@ -66,7 +68,9 @@ describe('unit: addToGitignore', () => {
readFileSyncStub.returns(gitignoreContentNoNewline);
await addToGitignore(fileNameTest);
expect(existsSyncStub).to.have.been.calledOnceWith(GITIGNORE_PATH);
expect(confirmStub).to.have.been.calledOnceWith(match({ message: `Add ${fileNameTest} to .gitignore?` }));
expect(confirmStub).to.have.been.calledOnceWith(
match({ message: messages.CONFIRM_ADD_TO_GITIGNORE(fileNameTest) })
);
expect(readFileSyncStub).to.have.been.calledOnceWith(GITIGNORE_PATH, 'utf8');
expect(appendFileSyncStub).to.have.been.calledOnceWith(GITIGNORE_PATH, `\n${lineToAdd}`);
});
Expand All @@ -77,7 +81,9 @@ describe('unit: addToGitignore', () => {
readFileSyncStub.returns(`${gitignoreContentNoNewline}\n`);
await addToGitignore(fileNameTest);
expect(existsSyncStub).to.have.been.calledOnceWith(GITIGNORE_PATH);
expect(confirmStub).to.have.been.calledOnceWith(match({ message: `Add ${fileNameTest} to .gitignore?` }));
expect(confirmStub).to.have.been.calledOnceWith(
match({ message: messages.CONFIRM_ADD_TO_GITIGNORE(fileNameTest) })
);
expect(readFileSyncStub).to.have.been.calledOnceWith(GITIGNORE_PATH, 'utf8');
expect(appendFileSyncStub).to.have.been.calledOnceWith(GITIGNORE_PATH, `${lineToAdd}`);
});
Expand All @@ -87,7 +93,9 @@ describe('unit: addToGitignore', () => {
confirmStub.returns(true);
await addToGitignore(fileNameTest);
expect(existsSyncStub).to.have.been.calledOnceWith(GITIGNORE_PATH);
expect(confirmStub).to.have.been.calledOnceWith(match({ message: `Add ${fileNameTest} to .gitignore?` }));
expect(confirmStub).to.have.been.calledOnceWith(
match({ message: messages.CONFIRM_ADD_TO_GITIGNORE(fileNameTest) })
);
expect(readFileSyncStub).to.not.have.been.called;
expect(appendFileSyncStub).to.have.been.calledOnceWith(GITIGNORE_PATH, `${lineToAdd}`);
});
Expand Down

0 comments on commit 6d4b0b9

Please sign in to comment.