Skip to content

Commit

Permalink
Made flag inputs case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaKulkarni committed Oct 27, 2023
1 parent a43f6f6 commit 3a28b80
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 21 deletions.
11 changes: 9 additions & 2 deletions core/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createApp } from './create-app';
import { printHelp } from './help-text';
import { resolveToRoot } from './utils/path-helpers';
import { CreateMagicAppError, CreateMagicAppErrorCode } from './utils/errors-warnings';
import { parseFlags } from './flags';
import { makeInputsLowercase, parseFlags } from './flags';
import { globalOptions } from './global-options';
import { shutdown, useGracefulShutdown } from './utils/shutdown';
import { SharedAnalytics } from './analytics';
Expand Down Expand Up @@ -87,7 +87,14 @@ async function sayHello() {

useGracefulShutdown();

const { version, help, projectName, template, branch, network, shareUsageData } = await parseFlags(globalOptions);
var { version, help, projectName, template, branch, network, shareUsageData } = await parseFlags(globalOptions);

template = makeInputsLowercase(template);
network = makeInputsLowercase(network);
branch = makeInputsLowercase(branch);

console.log('network', network);

const collectUsageData = await initializeUsageConfigIfneeded();
const config = loadConfig();

Expand Down
4 changes: 3 additions & 1 deletion core/create-app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export async function createApp(config: CreateMagicAppConfig) {
};
});

const isChosenTemplateValid = availableScaffolds.map((i) => i.name).includes(config?.template!);
const isChosenTemplateValid = availableScaffolds.map((i) => i.name).includes(config?.template!.toLowerCase());

if (config?.template && !isChosenTemplateValid) {
printWarning(chalk`'{bold ${config.template}}' does not match any templates.`);
Expand Down Expand Up @@ -113,6 +113,8 @@ export async function createApp(config: CreateMagicAppConfig) {
...config.data,
};

console.log('templateData', templateData);

const { gray, cyan } = chalk;
const timer = createTimer();

Expand Down
20 changes: 6 additions & 14 deletions core/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,8 @@ export async function parseFlags<T extends Flags>(flags: T, data?: {}): Promise<
boolean: booleans,
});

const caseInsensitiveResults = Object.fromEntries(
Object.entries(results).map(([key, value]) => [convertArgToCamelCase(key), value]),
);

const defaultResults = getFlagDefaults(flags);
const validatedResults = await validateFlagInputs(flags, caseInsensitiveResults);
const validatedResults = await validateFlagInputs(flags, results);
const finalResults = { ...defaultResults, ...validatedResults };

// If `data` is provided (in other words, if the flow is programmatic rather
Expand Down Expand Up @@ -188,13 +184,9 @@ async function validateFlagInputs<T extends Flags>(flags: T, inputs: {} = {}) {
);
}

function convertArgToCamelCase(arg: string) {
const tokens = arg.split('-');
return tokens.reduce((camelCase, token, index) => {
if (index === 0) {
return token.toLowerCase();
}

return camelCase + token.charAt(0).toUpperCase() + token.slice(1).toLowerCase();
}, '');
export function makeInputsLowercase(arg: string | undefined): string | undefined {
if (arg === undefined) {
return undefined;
}
return arg.toLowerCase();
}
2 changes: 2 additions & 0 deletions core/utils/templateMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import SolanaDedicatedScaffold, {
} from '../../scaffolds/nextjs-solana-dedicated-wallet/scaffold';
import UniversalScaffold, { flags as universalFlags } from '../../scaffolds/nextjs-universal-wallet/scaffold';
import { Timer } from './timer';
import { makeInputsLowercase } from 'core/flags';

export type Chain = 'evm' | 'solana' | 'flow';
export type Template =
Expand Down Expand Up @@ -75,6 +76,7 @@ export async function mapTemplateToScaffold(
timer: Timer,
): Promise<BaseScaffold> {
const data = appData;
data.network = makeInputsLowercase(data.network);
pauseTimerAndSpinner(timer, spinner);
if (!data.publishableApiKey) {
data.publishableApiKey = await PublishableApiKeyPrompt.publishableApiKeyPrompt();
Expand Down
9 changes: 5 additions & 4 deletions scaffolds/nextjs-dedicated-wallet/scaffold.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ export default class DedicatedScaffold extends BaseScaffold {
this.data = data;

if (typeof this.source !== 'string') {
data.loginMethods = data.loginMethods.map((authType) => AuthTypePrompt.mapInputToLoginMethods(authType));
data.loginMethods.forEach((authType) => {
(this.source as string[]).push(`./src/components/magic/auth/${authType.replaceAll(' ', '')}.tsx`);
(this.source as string[]).push(`./src/components/magic/auth/${authType}.tsx`);
if (
authType === 'Discord' ||
authType === 'Facebook' ||
Expand All @@ -67,12 +68,12 @@ export default class DedicatedScaffold extends BaseScaffold {
authType === 'Twitch' ||
authType === 'Twitter'
) {
(this.source as string[]).push(`./public/social/${authType.replaceAll(' ', '')}.svg`);
(this.source as string[]).push(`./public/social/${authType}.svg`);
}
if (authType.replaceAll(' ', '') === 'EmailOTP') {
if (authType === 'EmailOTP') {
(this.source as string[]).push('./src/components/magic/wallet-methods/UpdateEmail.tsx');
}
if (authType.replaceAll(' ', '') === 'SMSOTP') {
if (authType === 'SMSOTP') {
(this.source as string[]).push('./src/components/magic/wallet-methods/UpdatePhone.tsx');
}
});
Expand Down
1 change: 1 addition & 0 deletions scaffolds/nextjs-flow-dedicated-wallet/scaffold.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default class FlowDedicatedScaffold extends BaseScaffold {
this.data = data;

if (typeof this.source !== 'string') {
data.loginMethods = data.loginMethods.map((authType) => AuthTypePrompt.mapInputToLoginMethods(authType));
data.loginMethods.forEach((authType) => {
(this.source as string[]).push(`./src/components/magic/auth/${authType.replaceAll(' ', '')}.tsx`);
if (
Expand Down
1 change: 1 addition & 0 deletions scaffolds/nextjs-solana-dedicated-wallet/scaffold.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default class SolanaDedicatedScaffold extends BaseScaffold {
this.data = data;

if (typeof this.source !== 'string') {
data.loginMethods = data.loginMethods.map((authType) => AuthTypePrompt.mapInputToLoginMethods(authType));
data.loginMethods.forEach((authType) => {
(this.source as string[]).push(`./src/components/magic/auth/${authType.replaceAll(' ', '')}.tsx`);
if (
Expand Down
32 changes: 32 additions & 0 deletions scaffolds/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,36 @@ export namespace AuthTypePrompt {
},
},
};

export const mapInputToLoginMethods = (input: string) => {
if (input.replaceAll(' ', '').toLocaleLowerCase().includes('emailotp')) {
return 'EmailOTP';
}

if (input.replaceAll(' ', '').toLocaleLowerCase().includes('smsotp')) {
return 'SMSOTP';
}

if (input.replaceAll(' ', '').toLocaleLowerCase().includes('google')) {
return 'Google';
}

if (input.replaceAll(' ', '').toLocaleLowerCase().includes('github')) {
return 'Github';
}

if (input.replaceAll(' ', '').toLocaleLowerCase().includes('discord')) {
return 'Discord';
}

if (input.replaceAll(' ', '').toLocaleLowerCase().includes('twitter')) {
return 'Twitter';
}

if (input.replaceAll(' ', '').toLocaleLowerCase().includes('twitch')) {
return 'Twitch';
}

return input;
};
}

0 comments on commit 3a28b80

Please sign in to comment.