Something went wrong. Try again.
Dedicated SSR (Server-side-rendered) Astro native Headless CMS, build from the ground up for the Astro community and by Astro community memb github.com/withstudiocms/studiocms
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475import fs from 'node:fs';
// Some existing files and directories can be safely ignored when checking if a directory is a valid project directory.// https://github.com/facebook/create-react-app/blob/d960b9e38c062584ff6cfb1a70e1512509a966e7/packages/create-react-app/createReactApp.js#L907-L934const VALID_PROJECT_DIRECTORY_SAFE_LIST = [ '.DS_Store', '.git', '.gitkeep', '.gitattributes', '.gitignore', '.gitlab-ci.yml', '.hg', '.hgcheck', '.hgignore', '.idea', '.npmignore', '.travis.yml', '.yarn', '.yarnrc.yml', 'docs', 'LICENSE', 'mkdocs.yml', 'Thumbs.db', /\.iml$/, /^npm-debug\.log/, /^yarn-debug\.log/, /^yarn-error\.log/,];
/** * Check if a directory is empty, ignoring certain safe files and directories. * @param dirPath The path to the directory to check. * @returns True if the directory is empty or does not exist, false otherwise. */export function isEmpty(dirPath: string) { if (!fs.existsSync(dirPath)) { return true; }
const conflicts = fs.readdirSync(dirPath).filter((content) => { return !VALID_PROJECT_DIRECTORY_SAFE_LIST.some((safeContent) => { return typeof safeContent === 'string' ? content === safeContent : safeContent.test(content); }); });
return conflicts.length === 0;}
/** * Check if a project name is valid according to npm package naming conventions. * @param projectName The project name to validate. * @returns True if the project name is valid, false otherwise. */export function isValidName(projectName: string) { return /^(?:@[a-z\d\-*~][a-z\d\-*._~]*\/)?[a-z\d\-~][a-z\d\-._~]*$/.test(projectName);}
/** * Convert a project name to a valid npm package name. * @param projectName The project name to convert. * @returns A valid npm package name. */export function toValidName(projectName: string) { if (isValidName(projectName)) return projectName;
return projectName .trim() .toLowerCase() .replace(/\s+/g, '-') .replace(/^[._]/, '') .replace(/[^a-z\d\-~]+/g, '-') .replace(/^-+/, '') .replace(/-+$/, '');}