From d8b77ea951b80abbab72e19c4857f251f64f5d86 Mon Sep 17 00:00:00 2001 From: Patrick Coffey Date: Fri, 10 Dec 2021 12:41:50 -0600 Subject: [PATCH] fix: special snowflake drupal needs underscores :D --- src/handlers/init.ts | 12 +++++++++--- src/util/strToMachineName.test.ts | 10 ++++++++++ src/util/strToMachineName.ts | 13 +++++++++++-- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/handlers/init.ts b/src/handlers/init.ts index e2a835d..cc93f2d 100644 --- a/src/handlers/init.ts +++ b/src/handlers/init.ts @@ -4,7 +4,10 @@ import { existsSync, promises as fs } from 'fs'; import simpleGit from 'simple-git'; import ProgressBar from 'progress'; -import type { EmulsifyProjectConfiguration } from '@emulsify-cli/config'; +import type { + EmulsifyProjectConfiguration, + Platform, +} from '@emulsify-cli/config'; import type { InitHandlerOptions } from '@emulsify-cli/handlers'; import { EMULSIFY_PROJECT_CONFIG_FILE, @@ -43,7 +46,9 @@ export default function init(progress: InstanceType) { (await getPlatformInfo()) || {}; // If no platform name is given, and none can be detected, exit and error. - const platformName = options?.platform || autoPlatformName; + const platformName = (options?.platform || autoPlatformName) as + | Platform + | undefined; if (!platformName) { return log( 'error', @@ -57,7 +62,8 @@ export default function init(progress: InstanceType) { }); // Choose a folder name. If no machineName is given, create one using the project name. - const machineName = options?.machineName || strToMachineName(name); + const machineName = + options?.machineName || strToMachineName(name, platformName); // Collection information about the starter kit, such as the target directory, // starter repository, and checkout version. diff --git a/src/util/strToMachineName.test.ts b/src/util/strToMachineName.test.ts index 49da781..e76728b 100644 --- a/src/util/strToMachineName.test.ts +++ b/src/util/strToMachineName.test.ts @@ -9,4 +9,14 @@ describe('strToMachineName', () => { ) ).toBe('live-meaningfully-and-do-not-persue-expediency'); }); + + it('uses underscores if the given platform is drupal', () => { + expect.assertions(1); + expect( + strToMachineName( + '^Live &meaningfully%%%% and DO nOt!# persue $expediency**(#$', + 'drupal' + ) + ).toBe('live_meaningfully_and_do_not_persue_expediency'); + }); }); diff --git a/src/util/strToMachineName.ts b/src/util/strToMachineName.ts index fbd0bf6..52d916d 100644 --- a/src/util/strToMachineName.ts +++ b/src/util/strToMachineName.ts @@ -1,3 +1,9 @@ +import { Platform } from '@emulsify-cli/config'; + +export const getMachineNameSpaceReplacement = ( + platform?: Platform +): '-' | '_' => (platform === 'drupal' ? '_' : '-'); + /** * Takes a string and converts it to a machine-friendly string by eliminating * non-alphanumeric characters, and replacing spaces with dashes. @@ -6,7 +12,10 @@ * * @returns machine-friendly version of the given string. */ -export default function strToMachineName(str: string): string { +export default function strToMachineName( + str: string, + platform?: Platform +): string { return ( str // Filter out all non-alphanumeric/space characters. @@ -14,7 +23,7 @@ export default function strToMachineName(str: string): string { // Exclude double-spaces to prevent dual dashes. .replace(/\s{2,}/g, ' ') // Turn all spaces into dashes. - .replace(/\s/g, '-') + .replace(/\s/g, getMachineNameSpaceReplacement(platform)) .toLowerCase() ); }