UNPKG

create-typescript-project-scaffolding

Version:
38 lines (37 loc) 1.47 kB
import * as path from 'path'; import * as fs from 'fs'; class Name { constructor(givenProjectString) { if (givenProjectString === '.' || givenProjectString === '') { Name.PROJECT_PATH = process.cwd(); Name.PROJECT_NAME = path.basename(process.cwd()); } else { Name.PROJECT_PATH = path.join(process.cwd(), givenProjectString); Name.PROJECT_NAME = givenProjectString; } } static validate(input) { let isValidPathInput = Name.validatePackageName(input); if (!isValidPathInput && input === '.') isValidPathInput = true; if (fs.existsSync(path.join(process.cwd(), input)) && fs.readdirSync(path.join(process.cwd(), input)).length > 0) { this.VALIDATE_RETRY++; if (this.VALIDATE_RETRY > 3) { this.VALIDATE_RETRY = 0; return true; } return `The given path is not empty (Press Enter ${3 - this.VALIDATE_RETRY} times again to force-use this folder)`; } else this.VALIDATE_RETRY = 0; return isValidPathInput ? true : "Please enter '.' for the current directory or a folder name like 'new-project'"; } static validatePackageName(input) { return /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(input); } } Name.PROJECT_PATH = ''; Name.PROJECT_NAME = ''; Name.VALIDATE_RETRY = 0; export default Name;