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

Commit a04e9c6

Browse files
feat(cli): Add template categories (#76)
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)
1 parent 8ea5a77 commit a04e9c6

File tree

10 files changed

+82
-1
lines changed

10 files changed

+82
-1
lines changed

src/cli/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
getAppTemplateConfig,
1414
fetchLibraryVersions,
1515
getAllTemplates,
16+
getTemplatesByCategory,
1617
getTemplatePath,
1718
} = require('../utils');
1819
const getOptionsFromArguments = require('./getOptionsFromArguments');
@@ -89,9 +90,21 @@ try {
8990
const questions = [
9091
{
9192
type: 'list',
93+
pageSize: 10,
9294
name: 'template',
9395
message: 'InstantSearch template',
94-
choices: getAllTemplates(),
96+
choices: () => {
97+
const templatesByCategory = getTemplatesByCategory();
98+
99+
return Object.entries(templatesByCategory).reduce(
100+
(templates, [category, values]) => [
101+
...templates,
102+
new inquirer.Separator(category),
103+
...values,
104+
],
105+
[]
106+
);
107+
},
95108
validate(input) {
96109
return Boolean(input);
97110
},

src/templates/Angular InstantSearch/.template.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const install = require('../../tasks/node/install');
22
const teardown = require('../../tasks/node/teardown');
33

44
module.exports = {
5+
category: 'Web',
56
libraryName: 'angular-instantsearch',
67
templateName: 'angular-instantsearch',
78
appName: 'angular-instantsearch-app',

src/templates/InstantSearch Android/.template.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const install = require('../../tasks/ios/install');
33
const teardown = require('../../tasks/android/teardown');
44

55
module.exports = {
6+
category: 'Mobile',
67
templateName: 'instantsearch-android',
78
appName: 'instantsearch-android-app',
89
tasks: {

src/templates/InstantSearch iOS/.template.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const install = require('../../tasks/ios/install');
33
const teardown = require('../../tasks/ios/teardown');
44

55
module.exports = {
6+
category: 'Mobile',
67
templateName: 'instantsearch-ios',
78
appName: 'instantsearch-ios-app',
89
tasks: {

src/templates/InstantSearch.js/.template.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const install = require('../../tasks/node/install');
22
const teardown = require('../../tasks/node/teardown');
33

44
module.exports = {
5+
category: 'Web',
56
libraryName: 'instantsearch.js',
67
templateName: 'instantsearch.js',
78
appName: 'instantsearch.js-app',

src/templates/React InstantSearch Native/.template.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const install = require('../../tasks/node/install');
33
const teardown = require('../../tasks/node/teardown');
44

55
module.exports = {
6+
category: 'Mobile',
67
libraryName: 'react-instantsearch-native',
78
templateName: 'react-instantsearch-native',
89
appName: 'react-instantsearch-native-app',

src/templates/React InstantSearch/.template.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const install = require('../../tasks/node/install');
22
const teardown = require('../../tasks/node/teardown');
33

44
module.exports = {
5+
category: 'Web',
56
libraryName: 'react-instantsearch-dom',
67
templateName: 'react-instantsearch',
78
appName: 'react-instantsearch-app',

src/templates/Vue InstantSearch/.template.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const install = require('../../tasks/node/install');
22
const teardown = require('../../tasks/node/teardown');
33

44
module.exports = {
5+
category: 'Web',
56
libraryName: 'vue-instantsearch',
67
templateName: 'vue-instantsearch',
78
appName: 'vue-instantsearch-app',

src/utils/__tests__/index.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,39 @@ describe('checkTemplateConfigFile', () => {
8585
}).not.toThrow();
8686
});
8787
});
88+
89+
describe('getTemplatesByCategory', () => {
90+
beforeAll(() => {
91+
mockReaddirSync.mockImplementation(() => [
92+
'InstantSearch.js',
93+
'Angular InstantSearch',
94+
'React InstantSearch',
95+
'Vue InstantSearch',
96+
'React InstantSearch Native',
97+
'InstantSearch iOS',
98+
'InstantSearch Android',
99+
]);
100+
mockLstatSync.mockImplementation(() => ({ isDirectory: () => true }));
101+
});
102+
103+
test('return the correct templates and categories', () => {
104+
expect(utils.getTemplatesByCategory()).toEqual({
105+
Web: expect.arrayContaining([
106+
'InstantSearch.js',
107+
'Angular InstantSearch',
108+
'React InstantSearch',
109+
'Vue InstantSearch',
110+
]),
111+
Mobile: expect.arrayContaining([
112+
'React InstantSearch Native',
113+
'InstantSearch iOS',
114+
'InstantSearch Android',
115+
]),
116+
});
117+
});
118+
119+
afterAll(() => {
120+
mockReaddirSync.mockReset();
121+
mockLstatSync.mockReset();
122+
});
123+
});

src/utils/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,30 @@ function getAllTemplates() {
8888
return templates;
8989
}
9090

91+
function getTemplatesByCategory() {
92+
const templatePaths = fs
93+
.readdirSync(TEMPLATES_FOLDER)
94+
.map(name => path.join(TEMPLATES_FOLDER, name))
95+
.filter(source => fs.lstatSync(source).isDirectory());
96+
97+
const templates = templatePaths.reduce((allTemplates, source) => {
98+
const name = path.basename(source);
99+
100+
const { category } = require(`${source}/.template.js`);
101+
102+
if (!category) {
103+
return allTemplates;
104+
}
105+
106+
return {
107+
...allTemplates,
108+
[category]: [...(allTemplates[category] || []), name],
109+
};
110+
}, {});
111+
112+
return templates;
113+
}
114+
91115
function getTemplatePath(templateName) {
92116
const supportedTemplates = getAllTemplates();
93117

@@ -114,4 +138,5 @@ module.exports = {
114138
fetchLibraryVersions,
115139
getAllTemplates,
116140
getTemplatePath,
141+
getTemplatesByCategory,
117142
};

0 commit comments

Comments
 (0)