Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

feat(cli): Add template categories #76

Merged
merged 6 commits into from Jul 5, 2018

Conversation

francoischalifour
Copy link
Member

@francoischalifour francoischalifour commented Jul 5, 2018

This PR adds categories to templates.

Why

1. Order the templates

As we're getting more templates, I think it's better to distinguish web templates from mobile templates.

See if Web and Native make more sense than Web and Mobile.

2. Hide templates from the CLI

Templates having no category in their .template.js file don't show up in the CLI. This is a way to hide them but to still compile them to the templates branch for CodeSandbox (for the client, helper and AutoComplete.js templates).

Preview

image

Copy link
Contributor

@samouss samouss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - Just few questions / comments


test('return the correct templates and categories', () => {
expect(utils.getTemplatesByCategory()).toEqual({
Web: expect.arrayContaining([
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we use arrayContaining on this assertion? We check for an exact match not a partial one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use arrayContaining because we don't care about the order. Does that make sense?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, just for curiosity the order is not stable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it depends on your OS because it's determined by fs.readdirSync() (or perhaps Node standardizes that). It should be the same everywhere but I suppose there's no guarantee. We could add some weights if it's problematic.

return allTemplates;
}

const newAllTemplates = allTemplates;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The temporary variable is useless I think, we modify the reference of the object on the next line. We can inline the return statement with a spread operator.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've got an ESLint rule no-param-reassign that disallows to modify the reference object. I think it's a lot more work to inline the return statement with a spread operator in this case. I might be wrong?

Copy link
Contributor

@samouss samouss Jul 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like you want, I personally prefer to avoid mutations in general but it's only my point of view.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So do I. However I had the feeling it might be easier to understand here. I'll compare both versions!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine actually:

{
  ...allTemplates,
  [category]: [...(allTemplates[category] || []), name],
}

src/cli/index.js Outdated
const templatesByCategory = getTemplatesByCategory();
const choices = [];

for (const [category, values] of Object.entries(templatesByCategory)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also use a reduce rather than the for loop.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I first wanted to use reduce but it seemed like it was too much compared to this loop. I will compare them again.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be the equivalent:

Object.entries(templatesByCategory).reduce(
  (templates, [category, values]) => [
    ...templates,
    new inquirer.Separator(category),
    ...values,
  ],
  []
);

Not sure it more readable though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind it's not a big deal.

@samouss samouss self-requested a review July 5, 2018 13:02
@francoischalifour francoischalifour merged commit a04e9c6 into master Jul 5, 2018
@francoischalifour francoischalifour deleted the feat/template-category branch July 5, 2018 13:26
aymeric-giraudet pushed a commit to algolia/instantsearch that referenced this pull request Dec 14, 2022
This PR adds categories to templates.

## Why

1. Order the templates: as we're getting more templates, it's better to distinguish web templates from mobile templates.

2. Hide templates from the CLI: templates having no `category` in their `.template.js` file don't show up in the CLI. This is a way to hide them but to still compile them to the `templates` branch for CodeSandbox (for the client, helper and AutoComplete.js templates).

## Preview

![image](https://user-images.githubusercontent.com/6137112/42315441-bb34a234-8047-11e8-97c1-ebc805763827.png)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants