Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(prompts): consolidate file path checks #600

Merged
merged 5 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('cli', () => {
conf.set('apiKey', '123456');

await expect(cli(['docs'])).rejects.toThrow('No folder provided. Usage `rdme docs <folder> [options]`.');
conf.delete('apiKey');
conf.clear();
});

it('should error with `rdme oas` arguments passed in', async () => {
Expand Down
33 changes: 33 additions & 0 deletions __tests__/lib/checkFile.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import fs from 'fs';

import { checkFilePath } from '../../src/lib/checkFile';

describe('#checkFilePath', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('should return error for empty path value', () => {
return expect(checkFilePath('')).toBe('An output path must be supplied.');
});

it('should return error if path already exists', () => {
expect.assertions(2);
const testPath = 'path-that-already-exists';

fs.existsSync = jest.fn(() => true);

expect(checkFilePath(testPath)).toBe('Specified output path already exists.');
expect(fs.existsSync).toHaveBeenCalledWith(testPath);
});

it("should return true if the path doesn't exist", () => {
expect.assertions(2);
const testPath = 'path-that-does-not-exist';

fs.existsSync = jest.fn(() => false);

expect(checkFilePath(testPath)).toBe(true);
expect(fs.existsSync).toHaveBeenCalledWith(testPath);
});
});
13 changes: 2 additions & 11 deletions src/cmds/openapi/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import oasReducer from 'oas/dist/lib/reducer';
import ora from 'ora';

import Command, { CommandCategories } from '../../lib/baseCommand';
import { checkFilePath } from '../../lib/checkFile';
import { oraOptions } from '../../lib/logger';
import prepareOas from '../../lib/prepareOas';
import promptTerminal from '../../lib/promptWrapper';
Expand Down Expand Up @@ -131,17 +132,7 @@ export default class OpenAPIReduceCommand extends Command {
const extension = path.extname(specPath);
return `${path.basename(specPath).split(extension)[0]}-reduced${extension}`;
},
validate: value => {
if (value.length) {
if (!fs.existsSync(value)) {
return true;
}

return 'Specified output path already exists.';
}

return 'An output path must be supplied.';
},
validate: value => checkFilePath(value),
},
]);

Expand Down
30 changes: 30 additions & 0 deletions src/lib/checkFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import fs from 'fs';

/**
* Removes any non-alphanumeric characters and replaces them with hyphens.
*
* This is used for file names and for YAML keys.
*/
export const cleanFileName = (input: string) => input.replace(/[^a-z0-9]/gi, '-');

/**
* A validator function used in our prompts for when a user
* is prompted to specify a file path.
*
* @param value the file name
* @param getFullPath An optional function for adding a file path or any filename validations
* @returns true if path is valid (i.e. is non-empty and doesn't already exist),
* otherwise a string containing the error message
*/
export function checkFilePath(value: string, getFullPath: (file: string) => string = file => file) {
if (value.length) {
const fullPath = getFullPath(value);
if (!fs.existsSync(fullPath)) {
return true;
}

return 'Specified output path already exists.';
}

return 'An output path must be supplied.';
}
21 changes: 2 additions & 19 deletions src/lib/createGHA/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import simpleGit from 'simple-git';

import { transcludeString } from 'hercule/promises';

import { checkFilePath, cleanFileName } from '../checkFile';
import configstore from '../configstore';
import { getPkgVersion } from '../getPkgVersion';
import isCI from '../isCI';
Expand Down Expand Up @@ -44,13 +45,6 @@ export const getMajorRdmeVersion = async () => semverMajor(await getPkgVersion()

export const git = simpleGit();

/**
* Removes any non-alphanumeric characters and replaces them with hyphens.
*
* This is used for file names and for YAML keys.
*/
const cleanFileName = (input: string) => input.replace(/[^a-z0-9]/gi, '-');

/**
* Removes any non-file-friendly characters and adds
* the full path + file extension for GitHub Workflow files.
Expand Down Expand Up @@ -255,18 +249,7 @@ export default async function createGHA(
type: 'text',
initial: cleanFileName(`rdme-${command}`),
format: prev => getGHAFileName(prev),
validate: value => {
if (value.length) {
const fullPath = getGHAFileName(value);
if (!fs.existsSync(fullPath)) {
return true;
}

return 'Specified output path already exists.';
}

return 'An output path must be supplied.';
},
validate: value => checkFilePath(value, getGHAFileName),
},
],
{
Expand Down