@@ -5,6 +5,7 @@ const inquirer = require('inquirer');
55const chalk = require ( 'chalk' ) ;
66const latestSemver = require ( 'latest-semver' ) ;
77const loadJsonFile = require ( 'load-json-file' ) ;
8+ const algoliasearch = require ( 'algoliasearch' ) ;
89
910const createInstantSearchApp = require ( '../create-instantsearch-app' ) ;
1011const {
8889const optionsFromArguments = getOptionsFromArguments ( options . rawArgs ) ;
8990
9091const 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
165185async function getConfig ( ) {
0 commit comments