ribbitjs / ribbit

Standalone CLI for easy static file generating and route management.
MIT License
33 stars 1 forks source link

Added input validation to directory and file prompts #33

Closed ramraphael closed 5 years ago

ramraphael commented 5 years ago

What? Added input validation for the appRoot, app, bundleRoot, and bundleFile prompts in ribbit init.

Why? Adds an extra layer of validation to ensure that the user doesn't submit non-existent folders and files. Further checking and/or graceful error & exit messages are still needed in the main program, however.

How (to test): Install ribbit and run ribbit init in an existing React app. Entering non-existent files and folders should throw a meaningful error message.

Request for feedback: Do we need any additional validation in the initial prompt?

mrbut commented 5 years ago

Maybe not for the present, but a refactor would be to break up the validation function so its pieces are reusable:

const exists = (path_string) => fs.existsSync(path_string)
  ? true
  : `Entered ${type} does not exist`;

const fileType = (path_string) => {
  if (fs.lstatSync(path_string).isDirectory()) {
    return 'directory';
  }
  else if (fs.lstatSync(path_string).isFile()) {
    return 'folder';
  }
};

const pathExists = (path_string) => {
  return exists(path_string) ? true : `Entered ${fileType(path_string)} does not exist`
};

And use inquirer's answersHash to grab previous values and concatenate the paths' directories + files. Mentioned in the docs for 'validation' https://github.com/SBoudrias/Inquirer.js.

ramraphael commented 5 years ago

@mrbut : Updated to use Inquirer's native answers hash object! With this change, it's not apparent to me how we can further atomize this function.