Skip to content

Commit

Permalink
Clarify ActionTypes vs TemplateTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
tkcranny committed Oct 14, 2024
1 parent b03df39 commit 1a757f1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
8 changes: 4 additions & 4 deletions packages/cli/src/oclif/commands/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ScaffoldCommand extends BaseCommand {

await writeTemplateFile({
destinationPath: context.actionFileResolved,
templateType: context.templateType,
templateType: context.actionType,
language: context.language,
preventOverwrite: context.preventOverwrite,
templateContext: context.templateContext,
Expand All @@ -78,14 +78,14 @@ class ScaffoldCommand extends BaseCommand {
context.indexFileResolved,
context.templateContext.VARIABLE,
context.actionFileResolvedStem,
context.templateType,
context.actionType,
context.templateContext.KEY
);

if (isValidAppInstall().valid) {
const success = isValidEntryFileUpdate(
context.indexFileResolved,
context.templateType,
context.actionType,
context.templateContext.KEY
);

Expand Down Expand Up @@ -115,7 +115,7 @@ class ScaffoldCommand extends BaseCommand {
this.stopSpinner();

if (!this.flags.invokedFromAnotherCommand) {
this.log(`\nAll done! Your new ${context.templateType} is ready to use.`);
this.log(`\nAll done! Your new ${context.actionType} is ready to use.`);
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions packages/cli/src/tests/utils/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('scaffold', () => {
});

const commonContext = createTemplateContext({
templateType: 'trigger',
actionType: 'trigger',
noun: 'thing',
includeIntroComments: true,
});
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('scaffold', () => {
language: 'js',
preventOverwrite: true,
templateContext: createTemplateContext({
templateType: 'trigger',
actionType: 'trigger',
noun: 'thing',
includeIntroComments: true,
}),
Expand All @@ -129,7 +129,7 @@ describe('scaffold', () => {
language: 'js',
preventOverwrite: false,
templateContext: createTemplateContext({
templateType: 'trigger',
actionType: 'trigger',
noun: 'thing',
includeIntroComments: true,
}),
Expand All @@ -149,7 +149,7 @@ describe('scaffold', () => {
});

const commonContext = createTemplateContext({
templateType: 'trigger',
actionType: 'trigger',
noun: 'thing',
includeIntroComments: true,
});
Expand Down Expand Up @@ -210,7 +210,7 @@ describe('scaffold', () => {
language: 'ts',
preventOverwrite: true,
templateContext: createTemplateContext({
templateType: 'trigger',
actionType: 'trigger',
noun: 'thing',
includeIntroComments: true,
}),
Expand All @@ -222,7 +222,7 @@ describe('scaffold', () => {
language: 'ts',
preventOverwrite: false,
templateContext: createTemplateContext({
templateType: 'trigger',
actionType: 'trigger',
noun: 'thing',
includeIntroComments: true,
}),
Expand Down
31 changes: 17 additions & 14 deletions packages/cli/src/utils/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,37 @@ const nounToKey = (noun) => _.snakeCase(noun).replace(/V_(\d+)$/gi, 'v$1');
/**
* Create a context object to pass to the template
* @param {Object} options
* @param {'trigger'| 'search'| 'create'| 'resource'} options.templateType - the action type
* @param {ActionType} options.actionType - the action type
* @param {string} options.noun - the noun for the action
* @param {boolean} [options.includeIntroComments] - whether to include comments in the template
* @returns {TemplateContext}
*/
const createTemplateContext = ({
templateType,
actionType,
noun,
includeIntroComments = false,
}) => {
// if noun is "Cool Contact"
return {
ACTION: templateType, // trigger
ACTION_PLURAL: plural(templateType), // triggers
ACTION: actionType, // trigger
ACTION_PLURAL: plural(actionType), // triggers

VARIABLE: _.camelCase(getVariableName(templateType, noun)), // getContact, the variable that's imported
VARIABLE: _.camelCase(getVariableName(actionType, noun)), // getContact, the variable that's imported
KEY: nounToKey(noun), // "cool_contact", the action key
NOUN: noun
.split(' ')
.map((s) => _.capitalize(s))
.join(' '), // "Cool Contact", the noun
LOWER_NOUN: noun.toLowerCase(), // "cool contact", for use in comments
// resources need an extra line for tests to "just run"
MAYBE_RESOURCE: templateType === 'resource' ? 'list.' : '',
MAYBE_RESOURCE: actionType === 'resource' ? 'list.' : '',
INCLUDE_INTRO_COMMENTS: includeIntroComments,
};
};

/**
* @param {Object} options
* @param {TemplateType} options.templateType - the action type
* @param {TemplateType} options.templateType - the template to write
* @param {'js' | 'ts'} options.language - the language of the project
* @param {string} options.destinationPath - where to write the file
* @param {boolean} options.preventOverwrite - whether to prevent overwriting
Expand Down Expand Up @@ -158,7 +158,7 @@ const isValidEntryFileUpdate = (entryFilePath, actionType, newActionKey) => {
* operation.
*
* @param {Object} options
* @param {'trigger'| 'search'| 'create'| 'resource'} options.actionType - the action type
* @param {ActionType} options.actionType - the action type
* @param {string} options.noun - the noun for the action
* @param {'js' | 'ts'} options.language - the language of the project
* @param {string} options.indexFileLocal - the App's entry point (index.js/ts)
Expand Down Expand Up @@ -203,13 +203,13 @@ const createScaffoldingContext = ({
);

return {
templateType: actionType,
actionType,
actionTypePlural: plural(actionType),
noun,
preventOverwrite,
language,
templateContext: createTemplateContext({
templateType: actionType,
actionType,
noun,
includeIntroComments,
}),
Expand Down Expand Up @@ -241,9 +241,12 @@ module.exports = {
};

/**
* The varieties of templates that can be generated. The set of Action
* Types and "test" too.
* @typedef {'create' | 'resource' | 'search' | 'test' | 'trigger'} TemplateType
* The varieties of actions that can be generated.
* @typedef {'create' | 'resource' | 'search' | 'trigger'} ActionType
*/
/**
* The types of templates that can be made, including "test" files.
* @typedef { ActionType | 'test' } TemplateType
*/

/**
Expand All @@ -262,7 +265,7 @@ module.exports = {
* Everything needed to define a scaffolding operation.
*
* @typedef {Object} ScaffoldContext
* @property {TemplateType} templateType - the type of template (actions or "test") to use
* @property {ActionType} actionType - the type of action being created
* @property {string} actionTypePlural - plural of the template type, e.g. "triggers".
* @property {string} noun - the noun for the action
* @property {'js' | 'ts'} language - the language of the project
Expand Down

0 comments on commit 1a757f1

Please sign in to comment.