Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 40 additions & 20 deletions packages/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -88,26 +89,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',
Expand Down Expand Up @@ -160,6 +141,45 @@ 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: '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() {
Expand Down