tpmjs/packages/tools/create-basic-tools/src/validation/package-name.ts
Ajax Davis 38c85bd904 feat: add @tpmjs/create-basic-tools CLI generator
- Interactive CLI generator for scaffolding TPMJS tool packages
- Generates packages with minimum 2 tools (ideally 2-3)
- Zod 4 schemas - uses Zod directly (not jsonSchema wrapper)
- One file per tool in src/tools/<toolName>.ts
- TPMJS validated against official schemas from @tpmjs/types
- Complete package generation ready to publish to npm
- Works both standalone and in monorepo packages/ folders

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-04 23:34:37 +10:00

89 lines
1.8 KiB
TypeScript

import validatePackageName from 'validate-npm-package-name';
export interface PackageNameValidation {
valid: boolean;
errors?: string[];
warnings?: string[];
}
/**
* Validates an npm package name
*/
export function validateNpmPackageName(name: string): PackageNameValidation {
const result = validatePackageName(name);
if (result.validForNewPackages) {
return {
valid: true,
warnings: result.warnings,
};
}
return {
valid: false,
errors: result.errors,
warnings: result.warnings,
};
}
/**
* Validates that export name is a valid JavaScript identifier
*/
export function validateExportName(name: string): { valid: boolean; error?: string } {
if (!name || name.length === 0) {
return { valid: false, error: 'Export name cannot be empty' };
}
// JavaScript identifier regex: must start with letter, $, or _, followed by letters, digits, $, or _
const identifierRegex = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
if (!identifierRegex.test(name)) {
return {
valid: false,
error: 'Export name must be a valid JavaScript identifier (e.g., myTool, summarizeText)',
};
}
// Check for reserved words
const reservedWords = [
'break',
'case',
'catch',
'class',
'const',
'continue',
'debugger',
'default',
'delete',
'do',
'else',
'export',
'extends',
'finally',
'for',
'function',
'if',
'import',
'in',
'instanceof',
'new',
'return',
'super',
'switch',
'this',
'throw',
'try',
'typeof',
'var',
'void',
'while',
'with',
'yield',
];
if (reservedWords.includes(name)) {
return { valid: false, error: `"${name}" is a reserved JavaScript keyword` };
}
return { valid: true };
}