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

Commit 0d9cb6a

Browse files
feat(cli): Recommend attributes to display in hits (#43)
* feat(cli): Reorder questions * feat(cli): Recommend attributes to display
1 parent 9e99aad commit 0d9cb6a

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

packages/cli/cli.js

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const inquirer = require('inquirer');
55
const chalk = require('chalk');
66
const latestSemver = require('latest-semver');
77
const loadJsonFile = require('load-json-file');
8+
const algoliasearch = require('algoliasearch');
89

910
const createInstantSearchApp = require('../create-instantsearch-app');
1011
const {
@@ -88,26 +89,6 @@ try {
8889
const optionsFromArguments = getOptionsFromArguments(options.rawArgs);
8990

9091
const questions = [
91-
{
92-
type: 'input',
93-
name: 'appId',
94-
message: 'Application ID',
95-
},
96-
{
97-
type: 'input',
98-
name: 'apiKey',
99-
message: 'Search API key',
100-
},
101-
{
102-
type: 'input',
103-
name: 'indexName',
104-
message: 'Index name',
105-
},
106-
{
107-
type: 'input',
108-
name: 'mainAttribute',
109-
message: 'Main searchable attribute',
110-
},
11192
{
11293
type: 'list',
11394
name: 'template',
@@ -160,6 +141,45 @@ const questions = [
160141
}
161142
},
162143
},
144+
{
145+
type: 'input',
146+
name: 'appId',
147+
message: 'Application ID',
148+
},
149+
{
150+
type: 'input',
151+
name: 'apiKey',
152+
message: 'Search API key',
153+
},
154+
{
155+
type: 'input',
156+
name: 'indexName',
157+
message: 'Index name',
158+
},
159+
{
160+
type: 'list',
161+
name: 'mainAttribute',
162+
message: 'Attribute to display',
163+
choices: async answers => {
164+
const client = algoliasearch(answers.appId, answers.apiKey);
165+
const index = client.initIndex(answers.indexName);
166+
const defaultAttributes = ['title', 'name', 'description'];
167+
let attributes = [];
168+
169+
try {
170+
const { hits } = await index.search({ hitsPerPage: 1 });
171+
const [firstHit] = hits;
172+
attributes = Object.keys(firstHit._highlightResult).sort(
173+
value => !defaultAttributes.includes(value)
174+
);
175+
} catch (err) {
176+
attributes = defaultAttributes;
177+
}
178+
179+
return attributes;
180+
},
181+
when: ({ appId, apiKey, indexName }) => appId && apiKey && indexName,
182+
},
163183
].filter(question => isQuestionAsked({ question, args: optionsFromArguments }));
164184

165185
async function getConfig() {

0 commit comments

Comments
 (0)