Skip to content

Commit

Permalink
Merge pull request #57 from emulsify-ds/develop
Browse files Browse the repository at this point in the history
Deploy
  • Loading branch information
patrickocoffeyo authored Dec 10, 2021
2 parents a7bf491 + abe5e75 commit 4f8cc59
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/handlers/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -43,7 +46,9 @@ export default function init(progress: InstanceType<typeof ProgressBar>) {
(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',
Expand All @@ -57,7 +62,8 @@ export default function init(progress: InstanceType<typeof ProgressBar>) {
});

// 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.
Expand Down
10 changes: 10 additions & 0 deletions src/util/strToMachineName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
13 changes: 11 additions & 2 deletions src/util/strToMachineName.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -6,15 +12,18 @@
*
* @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.
.replace(/[^A-Za-z0-9 ]/g, '')
// 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()
);
}

0 comments on commit 4f8cc59

Please sign in to comment.