All files / packages/cli/src/utils validation.js

95.91% Statements 94/98
94.73% Branches 36/38
100% Functions 4/4
95.91% Lines 94/98

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 1241x 1x 1x         1x 1x 1x 1x 37x 4x 4x   33x   33x 37x       33x 37x 3x 3x   30x 37x 4x 4x   26x 26x 26x 26x 26x 26x 26x 26x   37x 14x 14x   12x 12x 37x 1x 1x   11x 11x   1x 1x 1x 1x 44x 4x 4x   40x   40x 44x 5x 5x   35x 44x       35x 44x 15x 15x   20x 20x   1x 1x 1x 1x 10x 1x 1x   9x   9x 10x 3x 3x   6x 10x 1x 1x   5x 5x   1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x   11x 5x 5x   6x 6x
/**
 * Validation utilities for CLI inputs
 */
 
import { existsSync } from 'fs';
import { resolve } from 'path';
 
/**
 * Validate project name according to npm and filesystem rules
 */
export function validateProjectName(name) {
  if (!name || typeof name !== 'string' || name.trim().length === 0) {
    return 'Project name is required';
  }
 
  const trimmed = name.trim();
 
  // Check length
  if (trimmed.length > 214) {
    return 'Project name must be less than 214 characters';
  }
 
  // Check for invalid characters (allowing @ for scoped packages)
  if (!/^[a-z0-9-_@./]+$/i.test(trimmed)) {
    return 'Project name can only contain letters, numbers, hyphens, underscores, dots, and slashes';
  }
 
  // Cannot start with . or _
  if (trimmed.startsWith('.') || trimmed.startsWith('_')) {
    return 'Project name cannot start with . or _';
  }
 
  // Cannot be reserved words
  const reserved = [
    'node_modules', 'favicon.ico', '.git', '.gitignore', '.env',
    'package.json', 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml',
    'con', 'prn', 'aux', 'nul', 'com1', 'com2', 'com3', 'com4', 'com5', 
    'com6', 'com7', 'com8', 'com9', 'lpt1', 'lpt2', 'lpt3', 'lpt4', 
    'lpt5', 'lpt6', 'lpt7', 'lpt8', 'lpt9'
  ];
 
  if (reserved.includes(trimmed.toLowerCase())) {
    return `Project name "${trimmed}" is reserved`;
  }
 
  // Check if directory already exists
  const projectPath = resolve(trimmed);
  if (existsSync(projectPath)) {
    return `Directory "${trimmed}" already exists`;
  }
 
  return true;
}
 
/**
 * Validate component/page/API name
 */
export function validateComponentName(name) {
  if (!name || typeof name !== 'string' || name.trim().length === 0) {
    return 'Name is required';
  }
 
  const trimmed = name.trim();
 
  // Check for valid identifier
  if (!/^[a-zA-Z][a-zA-Z0-9-_]*$/.test(trimmed)) {
    return 'Name must start with a letter and contain only letters, numbers, hyphens, and underscores';
  }
 
  // Check length
  if (trimmed.length > 100) {
    return 'Name must be less than 100 characters';
  }
 
  // Should be PascalCase for components
  if (!/^[A-Z]/.test(trimmed)) {
    return 'Name should start with a capital letter (PascalCase)';
  }
 
  return true;
}
 
/**
 * Validate file path
 */
export function validatePath(path) {
  if (!path || path.trim().length === 0) {
    return true; // Optional
  }
 
  const trimmed = path.trim();
 
  // Check for invalid characters
  if (!/^[a-zA-Z0-9-_/.]+$/.test(trimmed)) {
    return 'Path can only contain letters, numbers, hyphens, underscores, dots, and slashes';
  }
 
  // Cannot start with /
  if (trimmed.startsWith('/')) {
    return 'Path should be relative (don\'t start with /)';
  }
 
  return true;
}
 
/**
 * Validate template name
 */
export function validateTemplate(template) {
  const validTemplates = [
    'basic',
    'fullstack', 
    'express',
    'fastify',
    'components',
    'nextjs'
  ];
 
  if (!validTemplates.includes(template)) {
    return `Invalid template. Available: ${validTemplates.join(', ')}`;
  }
 
  return true;
}