diff --git a/tools/challenge-helper-scripts/create-project.ts b/tools/challenge-helper-scripts/create-project.ts index 9892a7b972f..f0fb390126f 100644 --- a/tools/challenge-helper-scripts/create-project.ts +++ b/tools/challenge-helper-scripts/create-project.ts @@ -6,7 +6,7 @@ import { format } from 'prettier'; import ObjectID from 'bson-objectid'; import { SuperBlocks } from '../../shared/config/superblocks'; -import { createStepFile } from './utils'; +import { createStepFile, validateBlockName } from './utils'; import { getSuperBlockSubPath } from './fs-utils'; import { Meta } from './helpers/project-metadata'; @@ -198,17 +198,9 @@ void prompt([ { name: 'block', message: 'What is the dashed name (in kebab-case) for this project?', - validate: (block: string) => { - if (!block.length) { - return 'please enter a dashed name'; - } - if (/[^a-z0-9-]/.test(block)) { - return 'please use alphanumerical characters and kebab case'; - } - return true; - }, + validate: validateBlockName, filter: (block: string) => { - return block.toLowerCase(); + return block.toLowerCase().trim(); } }, { diff --git a/tools/challenge-helper-scripts/utils.test.ts b/tools/challenge-helper-scripts/utils.test.ts index ae05fa5344f..5663bb325b1 100644 --- a/tools/challenge-helper-scripts/utils.test.ts +++ b/tools/challenge-helper-scripts/utils.test.ts @@ -21,7 +21,8 @@ import { createChallengeFile, createStepFile, insertStepIntoMeta, - updateStepTitles + updateStepTitles, + validateBlockName } from './utils'; const basePath = join( @@ -78,6 +79,30 @@ describe('Challenge utils helper scripts', () => { }); }); + describe('createProject util', () => { + it('should allow alphanumerical names with trailing whitespace', () => { + expect( + validateBlockName('learn-callbacks-by-creating-a-bookshelf ') + ).toBe(true); + }); + it('should allow alphanumerical names with no trailing whitespace', () => { + expect(validateBlockName('learn-callbacks-by-creating-a-bookshelf')).toBe( + true + ); + }); + it('should not allow non-kebab case names', () => { + expect(validateBlockName('learnCallbacksBetter')).toBe( + 'please use alphanumerical characters and kebab case' + ); + }); + it('should not allow white space names', () => { + expect(validateBlockName(' ')).toBe('please enter a dashed name'); + }); + it('should not allow empty names', () => { + expect(validateBlockName('')).toBe('please enter a dashed name'); + }); + }); + describe('createChallengeFile util', () => { it('should create the challenge', () => { fs.writeFileSync( diff --git a/tools/challenge-helper-scripts/utils.ts b/tools/challenge-helper-scripts/utils.ts index 3a9b20be8f5..1090d3a88d3 100644 --- a/tools/challenge-helper-scripts/utils.ts +++ b/tools/challenge-helper-scripts/utils.ts @@ -201,6 +201,16 @@ const getChallengeSeeds = ( return parseMDSync(challengeFilePath).challengeFiles; }; +const validateBlockName = (block: string): boolean | string => { + if (!block.trim().length) { + return 'please enter a dashed name'; + } + if (/[^a-z0-9-]/.test(block.trim())) { + return 'please use alphanumerical characters and kebab case'; + } + return true; +}; + export { createStepFile, createChallengeFile, @@ -211,5 +221,6 @@ export { insertChallengeIntoMeta, insertStepIntoMeta, deleteChallengeFromMeta, - deleteStepFromMeta + deleteStepFromMeta, + validateBlockName };