11#!/usr/bin/env node
22const path = require ( 'path' ) ;
33const process = require ( 'process' ) ;
4+ const os = require ( 'os' ) ;
45const program = require ( 'commander' ) ;
56const inquirer = require ( 'inquirer' ) ;
67const chalk = require ( 'chalk' ) ;
78const latestSemver = require ( 'latest-semver' ) ;
8- const os = require ( 'os ' ) ;
9+ const semver = require ( 'semver ' ) ;
910
1011const createInstantSearchApp = require ( '../api' ) ;
1112const {
@@ -18,6 +19,7 @@ const {
1819} = require ( '../utils' ) ;
1920const getOptionsFromArguments = require ( './getOptionsFromArguments' ) ;
2021const getAttributesFromIndex = require ( './getAttributesFromIndex' ) ;
22+ const getFacetsFromIndex = require ( './getFacetsFromIndex' ) ;
2123const getAnswersDefaultValues = require ( './getAnswersDefaultValues' ) ;
2224const isQuestionAsked = require ( './isQuestionAsked' ) ;
2325const {
@@ -61,6 +63,10 @@ const attributesToDisplay = (optionsFromArguments.attributesToDisplay || '')
6163 . split ( ',' )
6264 . filter ( Boolean )
6365 . map ( x => x . trim ( ) ) ;
66+ const attributesForFaceting = ( optionsFromArguments . attributesForFaceting || '' )
67+ . split ( ',' )
68+ . filter ( Boolean )
69+ . map ( x => x . trim ( ) ) ;
6470
6571const getQuestions = ( { appName } ) => ( {
6672 application : [
@@ -156,6 +162,48 @@ const getQuestions = ({ appName }) => ({
156162 when : ( { appId, apiKey, indexName } ) =>
157163 ! attributesToDisplay . length > 0 && appId && apiKey && indexName ,
158164 } ,
165+ {
166+ type : 'checkbox' ,
167+ name : 'attributesForFaceting' ,
168+ message : 'Attributes to display' ,
169+ suffix : `\n ${ chalk . gray ( 'Used to filter the search interface' ) } ` ,
170+ pageSize : 10 ,
171+ choices : async answers => {
172+ const templatePath = getTemplatePath ( answers . template ) ;
173+ const templateConfig = getAppTemplateConfig ( templatePath ) ;
174+
175+ const selectedLibraryVersion = answers . libraryVersion ;
176+ const requiredLibraryVersion =
177+ templateConfig . flags && templateConfig . flags . dynamicWidgets ;
178+ const supportsDynamicWidgets =
179+ selectedLibraryVersion &&
180+ requiredLibraryVersion &&
181+ semver . satisfies ( selectedLibraryVersion , requiredLibraryVersion , {
182+ includePrerelease : true ,
183+ } ) ;
184+
185+ const dynamicWidgets = supportsDynamicWidgets
186+ ? [
187+ {
188+ name : 'Dynamic widgets' ,
189+ value : 'ais.dynamicWidgets' ,
190+ checked : true ,
191+ } ,
192+ new inquirer . Separator ( ) ,
193+ ]
194+ : [ ] ;
195+
196+ return [
197+ ...dynamicWidgets ,
198+ new inquirer . Separator ( 'From your index' ) ,
199+ ...( await getFacetsFromIndex ( answers ) ) ,
200+ new inquirer . Separator ( ) ,
201+ ] ;
202+ } ,
203+ filter : attributes => attributes . filter ( Boolean ) ,
204+ when : ( { appId, apiKey, indexName } ) =>
205+ ! attributesForFaceting . length > 0 && appId && apiKey && indexName ,
206+ } ,
159207 ] ,
160208 widget : [
161209 {
@@ -301,6 +349,11 @@ async function run() {
301349 ...configuration ,
302350 ...answers ,
303351 ...alternativeNames ,
352+ flags : {
353+ dynamicWidgets :
354+ Array . isArray ( answers . attributesForFaceting ) &&
355+ answers . attributesForFaceting . includes ( 'ais.dynamicWidgets' ) ,
356+ } ,
304357 libraryVersion,
305358 template : templatePath ,
306359 installation : program . installation ,
0 commit comments