rdme
Version:
ReadMe's official CLI and GitHub Action.
39 lines (38 loc) • 1.26 kB
JavaScript
import fs from 'node: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) => input.replace(/[^a-z0-9]/gi, '-');
/**
* A validator function used in our prompts for when a user
* is prompted to specify a file path.
*
* @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 validateFilePath(
/** the file name */
value,
/** An optional function for adding a file path or any filename validations */
getFullPath = 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.';
}
/**
* Validates that a project subdomain value is valid.
*
* @returns true if the subdomain value is valid, else an error message
*/
export function validateSubdomain(
/** the raw project subdomain value */
value) {
return (/^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$/.test(value) || 'Project subdomain must contain only letters, numbers and dashes.');
}