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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | 43x 4x 39x 39x 39x 4x 35x 4x 31x 31x 14x 17x 2x 15x 4x 11x 11x 1x 10x 44x 4x 40x 40x 5x 35x 35x 44x 44x 9x 35x 35x 15x 20x 10x 1x 9x 9x 3x 6x 1x 5x 11x 11x 5x 6x | /**
* Validation utilities for CLI inputs
*/
import { existsSync } from 'fs';
import { resolve } from 'path';
/**
* Validate project name according to npm and filesystem rules.
*
* The name is used both as the directory created under the current
* directory and as the package.json name, so it must be a valid,
* unscoped npm package name and a single path segment: no separators
* (`foo/../../x` would write outside the working directory), lowercase,
* starting with a letter or digit.
*/
export function validateProjectName(name) {
if (!name || typeof name !== 'string' || name.trim().length === 0) {
return 'Project name is required';
}
const trimmed = name.trim();
// Check length
Iif (trimmed.length > 214) {
return 'Project name must be less than 214 characters';
}
// A single directory name: no path separators
if (/[\\/]/.test(trimmed)) {
return 'Project name cannot contain path separators (/ or \\); run the command from the parent directory instead';
}
// 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`;
}
// npm package names cannot contain capital letters
if (trimmed !== trimmed.toLowerCase()) {
return 'Project name must be lowercase (npm package names cannot contain capital letters)';
}
// URL-safe npm name characters
if (!/^[a-z0-9][a-z0-9._-]*$/.test(trimmed)) {
return 'Project name must start with a letter or number and contain only lowercase letters, numbers, hyphens, underscores, and dots';
}
// Check if directory already exists
const projectPath = resolve(trimmed);
if (existsSync(projectPath)) {
return `Directory "${trimmed}" already exists`;
}
return true;
}
/**
* Validate an API name. Any case is accepted (`users`, `user-profile`,
* `UserProfile`): the api generator derives the kebab-case route and file
* name and the camelCase identifiers from it.
*/
export function validateApiName(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
Iif (trimmed.length > 100) {
return 'Name must be less than 100 characters';
}
return true;
}
/**
* Validate component/page name
*/
export function validateComponentName(name) {
const base = validateApiName(name);
if (base !== true) {
return base;
}
const trimmed = name.trim();
// 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;
} |