Skip to content

Commit

Permalink
refactor: move some field command fn into their own files
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Dec 30, 2023
1 parent c2f279f commit 4801702
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 92 deletions.
66 changes: 5 additions & 61 deletions src/commands/schema/generate/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ import { objectPrompt } from '../../../shared/prompts/object.js';
import { relationshipFieldPrompts } from '../../../shared/prompts/relationshipField.js';
import { isObjectsFolder, labelValidation } from '../../../shared/flags.js';
import { toSelectOption } from '../../../shared/prompts/functions.js';
import { lengthPrompt } from '../../../shared/prompts/fields/text.js';
import { visibleLinePrompt } from '../../../shared/prompts/fields/text.js';
import { precisionPrompt } from '../../../shared/prompts/fields/number.js';
import { scalePrompt } from '../../../shared/prompts/fields/number.js';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-sobject', 'generate.field');

const MAX_LONG_TEXT_LENGTH = 131072;
const MAX_TEXT_LENGTH = 255;
export const messages = Messages.loadMessages('@salesforce/plugin-sobject', 'generate.field');

const supportedFieldTypesCustomObject = [
'AutoNumber',
Expand Down Expand Up @@ -195,30 +196,6 @@ const getSupportedFieldTypes = (
return supportedFieldTypesCustomObject;
};

/**
* Prompt for the length property based on the field type
*
* @param type the field type
*/
const lengthPrompt = async (type: string): Promise<number | undefined> =>
type === 'Text'
? Number(
await input({
message: `Length (max ${MAX_TEXT_LENGTH})`,
default: MAX_TEXT_LENGTH.toString(),
validate: integerValidation({ min: 1, max: MAX_TEXT_LENGTH }),
})
)
: type === 'Html' || type === 'LongTextArea'
? Number(
await input({
message: `Length (max ${MAX_LONG_TEXT_LENGTH})`,
default: MAX_LONG_TEXT_LENGTH.toString(),
validate: integerValidation({ min: 1, max: MAX_LONG_TEXT_LENGTH }),
})
)
: undefined;

// checkbox type requires a default value
const defaultValuePrompt = async (type: string): Promise<string | undefined> =>
type === 'Checkbox'
Expand Down Expand Up @@ -277,36 +254,3 @@ const inlineHelpPrompt = async (object: string): Promise<string | undefined> =>
: input({
message: messages.getMessage('prompts.inlineHelpText'),
});

const precisionPrompt = async (type: string, scale: number | undefined): Promise<number | undefined> =>
type === 'Number' || type === 'Currency'
? Number(
await input({
message: messages.getMessage('prompts.precision'),
validate: integerValidation({ min: 1, max: 18 - (scale ?? 0) }),
default: (18 - (scale ?? 0)).toString(),
})
)
: undefined;

const scalePrompt = async (type: string): Promise<number | undefined> =>
type === 'Number' || type === 'Currency' || type === 'Location'
? Number(
await input({
message: messages.getMessage('prompts.scale'),
validate: integerValidation({ min: 0, max: 18 }),
default: '0',
})
)
: undefined;

const visibleLinePrompt = async (type: string): Promise<number | undefined> =>
type === 'Html' || type === 'LongTextArea'
? Number(
await input({
message: 'Visible Lines',
validate: integerValidation({ min: 1, max: 1000 }),
default: '5',
})
)
: undefined;
18 changes: 7 additions & 11 deletions src/commands/schema/generate/platformevent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,15 @@ export default class PlatformEventGenerate extends SfCommand<PlatformEventGenera
const { flags } = await this.parse(PlatformEventGenerate);

const directory = await directoryPrompt(this.project.getPackageDirectories());
const pluralLabel = await pluralPrompt(flags.label);
const fullName = await apiNamePrompt(flags.label, 'PlatformEvent');
const description = await descriptionPrompt();
const publishBehavior = await select({
message: messages.getMessage('prompts.publishBehavior'),
choices: ['PublishImmediately', 'PublishAfterCommit'].map(toSelectOption),
});

const objectToWrite: PlatformEventGenerateResult['object'] = {
publishBehavior,
fullName,
pluralLabel,
description,
fullName: await apiNamePrompt(flags.label, 'PlatformEvent'),
pluralLabel: await pluralPrompt(flags.label),
description: await descriptionPrompt(),
publishBehavior: await select({
message: messages.getMessage('prompts.publishBehavior'),
choices: ['PublishImmediately', 'PublishAfterCommit'].map(toSelectOption),
}),
deploymentStatus: 'Deployed',
eventType: 'HighVolume',
label: flags.label,
Expand Down
26 changes: 9 additions & 17 deletions src/commands/schema/generate/sobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,17 @@ export default class ObjectGenerate extends SfCommand<CustomObjectGenerateResult
const { flags } = await this.parse(ObjectGenerate);

const directory = await directoryPrompt(this.project.getPackageDirectories());
const pluralLabel = await pluralPrompt(flags.label);
const fullName = await apiNamePrompt(flags.label, 'CustomObject');
const description = await descriptionPrompt();
const nameField = await nameFieldPrompts(flags.label);

const defaultFeaturesSelected = flags['use-default-features'] ? defaultFeatures : await promptForDefaultFeatures();

const sharingModel = await select({
message: messages.getMessage('prompts.sharingModel'),
choices: [{ value: 'ReadWrite' }, { value: 'Read' }, { value: 'Private' }],
});

const resultsObject = {
pluralLabel,
fullName,
description,
nameField,
sharingModel,
...defaultFeaturesSelected,
pluralLabel: await pluralPrompt(flags.label),
fullName: await apiNamePrompt(flags.label, 'CustomObject'),
description: await descriptionPrompt(),
nameField: await nameFieldPrompts(flags.label),
...(flags['use-default-features'] ? defaultFeatures : await promptForDefaultFeatures()),
sharingModel: await select({
message: messages.getMessage('prompts.sharingModel'),
choices: [{ value: 'ReadWrite' }, { value: 'Read' }, { value: 'Private' }],
}),
label: flags.label,
deploymentStatus: 'Deployed',
} satisfies SaveableCustomObject;
Expand Down
5 changes: 2 additions & 3 deletions src/shared/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import {sep, resolve} from 'node:path';
import { sep, resolve } from 'node:path';

import { Messages, SfError } from '@salesforce/core';


Messages.importMessagesDirectoryFromMetaUrl(import.meta.url)
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-sobject', 'flags');

// eslint-disable-next-line @typescript-eslint/require-await
Expand Down
33 changes: 33 additions & 0 deletions src/shared/prompts/fields/number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import input from '@inquirer/input';
import { Messages } from '@salesforce/core';
import { integerValidation } from '../functions.js';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
export const messages = Messages.loadMessages('@salesforce/plugin-sobject', 'generate.field');

export const precisionPrompt = async (type: string, scale: number | undefined): Promise<number | undefined> =>
type === 'Number' || type === 'Currency'
? Number(
await input({
message: messages.getMessage('prompts.precision'),
validate: integerValidation({ min: 1, max: 18 - (scale ?? 0) }),
default: (18 - (scale ?? 0)).toString(),
})
)
: undefined;
export const scalePrompt = async (type: string): Promise<number | undefined> =>
type === 'Number' || type === 'Currency' || type === 'Location'
? Number(
await input({
message: messages.getMessage('prompts.scale'),
validate: integerValidation({ min: 0, max: 18 }),
default: '0',
})
)
: undefined;
41 changes: 41 additions & 0 deletions src/shared/prompts/fields/text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import input from '@inquirer/input';
import { integerValidation } from '../functions.js';

const MAX_LONG_TEXT_LENGTH = 131072;
const MAX_TEXT_LENGTH = 255;

export const lengthPrompt = async (type: string): Promise<number | undefined> =>
type === 'Text'
? Number(
await input({
message: `Length (max ${MAX_TEXT_LENGTH})`,
default: MAX_TEXT_LENGTH.toString(),
validate: integerValidation({ min: 1, max: MAX_TEXT_LENGTH }),
})
)
: type === 'Html' || type === 'LongTextArea'
? Number(
await input({
message: `Length (max ${MAX_LONG_TEXT_LENGTH})`,
default: MAX_LONG_TEXT_LENGTH.toString(),
validate: integerValidation({ min: 1, max: MAX_LONG_TEXT_LENGTH }),
})
)
: undefined;

export const visibleLinePrompt = async (type: string): Promise<number | undefined> =>
type === 'Html' || type === 'LongTextArea'
? Number(
await input({
message: 'Visible Lines',
validate: integerValidation({ min: 1, max: 1000 }),
default: '5',
})
)
: undefined;

0 comments on commit 4801702

Please sign in to comment.