Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

utils.ts

38 lines1.2 KB

1import { basename } from 'node:path'
2import validatePackageName from 'validate-npm-package-name'
3import type { TemplateOptions } from './types.js'
4​
5export function convertTemplateToMode(template: TemplateOptions): string {
6 if (template === 'typescript' || template === 'javascript') {
7 return 'code-router'
8 }
9 return 'file-router'
11​
12export function sanitizePackageName(name: string): string {
13 return name
14 .toLowerCase()
15 .replace(/\s+/g, '-') // Replace spaces with hyphens
16 .replace(/_/g, '-') // Replace underscores with hyphens
17 .replace(/[^a-z0-9-]/g, '') // Remove invalid characters
18 .replace(/^[^a-z]+/, '') // Ensure it starts with a letter
19 .replace(/-+/g, '-') // Collapse multiple hyphens
20 .replace(/-$/, '') // Remove trailing hyphen
22​
23export function getCurrentDirectoryName(): string {
24 return basename(process.cwd())
26​
27export function validateProjectName(name: string) {
28 const { validForNewPackages, validForOldPackages, errors, warnings } =
29 validatePackageName(name)
30 const error = errors?.[0] || warnings?.[0]
31​
32 return {
33 valid: validForNewPackages && validForOldPackages,
34 error:
35 error?.replace(/name/g, 'Project name') ||
36 'Project name does not meet npm package naming requirements',
37 }