From d8e3b3fec7d2feb058379647cf3eef09e51cd744 Mon Sep 17 00:00:00 2001 From: Bennett Date: Mon, 1 Nov 2021 00:51:01 +0100 Subject: Use PascalCasing for generated backend API class name (#759) --- scripts/create.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'scripts/create.js') diff --git a/scripts/create.js b/scripts/create.js index 184dc70..f171290 100644 --- a/scripts/create.js +++ b/scripts/create.js @@ -19,7 +19,6 @@ pnpm run create WhatsApp FerdiDev const recipeName = process.argv[2]; const recipe = recipeName.toLowerCase().replace(/\s/g, '-'); -const cleanRecipeId = recipe.replace(/[^a-z]/g, ''); // Clean recipe ID only containing a-z, for usage as the JavaScript class name const folderName = process.argv[3] || 'Ferdi'; const filesThatNeedTextReplace = [ 'package.json', @@ -28,6 +27,21 @@ const filesThatNeedTextReplace = [ 'README.md', ]; +const toPascalCase = (str) => { + const words = str + .replace(/[^a-z]/g, '') + .split(/\W/) + .map((word) => { + if (word.length === 0) { + return word; + } + // Capitalize the first letter, lowercase the rest + return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(); + }); + return words.join(''); +} +const pascalCasedName = toPascalCase(recipe); // PascalCased recipe ID only containing a-z, for usage as the JavaScript class name + (async () => { // Folder paths const userData = @@ -66,7 +80,7 @@ const filesThatNeedTextReplace = [ let contents = await fs.readFile(filePath, 'utf-8'); contents = contents.replace(/SERVICE/g, recipe); contents = contents.replace(/SNAME/g, recipeName); - contents = contents.replace(/SCLEAN/g, cleanRecipeId); + contents = contents.replace(/SPASCAL/g, pascalCasedName); await fs.writeFile(filePath, contents); } console.log('[Info] Prepared new recipe'); -- cgit v1.2.3-54-g00ecf