Home Manual Reference Source Test

src/init/OptionsValidator.js

  1. import validatePackageName from 'validate-npm-package-name';
  2.  
  3. /**
  4. * A static class containing validators for the options used when running "atscm init".
  5. */
  6. export default class InitOptionsValidator {
  7. /**
  8. * Validates a project name to be a valid npm package name.
  9. * @param {string} value The name to validate.
  10. * @return {boolean|string} Returns true if `value` is a valid npm package name, or an error
  11. * message otherwise.
  12. */
  13. static name(value) {
  14. const result = validatePackageName(value);
  15.  
  16. if (result.errors) {
  17. return result.errors[0];
  18. }
  19.  
  20. // First letter must be a letter
  21. if (value.match(/^@?[a-z]+/i) === null) {
  22. return 'name must start with a letter';
  23. }
  24.  
  25. if (value === 'atscm') {
  26. return "'atscm' is not allowed";
  27. }
  28.  
  29. return result.validForNewPackages ? true : result.warnings[0];
  30. }
  31. }