From 2f51b2f31537c1bcfa63ae757b1f825da6731c5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Chalifour?= Date: Tue, 12 Jun 2018 16:01:13 +0200 Subject: [PATCH 1/2] feat(cli): Reorder questions --- packages/cli/cli.js | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/packages/cli/cli.js b/packages/cli/cli.js index 712f271f3..d73a5ef92 100644 --- a/packages/cli/cli.js +++ b/packages/cli/cli.js @@ -88,26 +88,6 @@ try { const optionsFromArguments = getOptionsFromArguments(options.rawArgs); const questions = [ - { - type: 'input', - name: 'appId', - message: 'Application ID', - }, - { - type: 'input', - name: 'apiKey', - message: 'Search API key', - }, - { - type: 'input', - name: 'indexName', - message: 'Index name', - }, - { - type: 'input', - name: 'mainAttribute', - message: 'Main searchable attribute', - }, { type: 'list', name: 'template', @@ -160,6 +140,21 @@ const questions = [ } }, }, + { + type: 'input', + name: 'appId', + message: 'Application ID', + }, + { + type: 'input', + name: 'apiKey', + message: 'Search API key', + }, + { + type: 'input', + name: 'indexName', + message: 'Index name', + }, ].filter(question => isQuestionAsked({ question, args: optionsFromArguments })); async function getConfig() { From f974a3adf3478d8904b1797a46dc01ff59d7e01d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Chalifour?= Date: Tue, 12 Jun 2018 16:01:36 +0200 Subject: [PATCH 2/2] feat(cli): Recommend attributes to display Closes #32 --- packages/cli/cli.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/cli/cli.js b/packages/cli/cli.js index d73a5ef92..da5356bda 100644 --- a/packages/cli/cli.js +++ b/packages/cli/cli.js @@ -5,6 +5,7 @@ const inquirer = require('inquirer'); const chalk = require('chalk'); const latestSemver = require('latest-semver'); const loadJsonFile = require('load-json-file'); +const algoliasearch = require('algoliasearch'); const createInstantSearchApp = require('../create-instantsearch-app'); const { @@ -155,6 +156,30 @@ const questions = [ name: 'indexName', message: 'Index name', }, + { + type: 'list', + name: 'mainAttribute', + message: 'Attribute to display', + choices: async answers => { + const client = algoliasearch(answers.appId, answers.apiKey); + const index = client.initIndex(answers.indexName); + const defaultAttributes = ['title', 'name', 'description']; + let attributes = []; + + try { + const { hits } = await index.search({ hitsPerPage: 1 }); + const [firstHit] = hits; + attributes = Object.keys(firstHit._highlightResult).sort( + value => !defaultAttributes.includes(value) + ); + } catch (err) { + attributes = defaultAttributes; + } + + return attributes; + }, + when: ({ appId, apiKey, indexName }) => appId && apiKey && indexName, + }, ].filter(question => isQuestionAsked({ question, args: optionsFromArguments })); async function getConfig() {