From 4a27e5bc3924bc289945c55a10da74dfda02fedb Mon Sep 17 00:00:00 2001
From: Sarah Dayan <5370675+sarahdayan@users.noreply.github.com>
Date: Mon, 25 Sep 2023 11:27:15 +0200
Subject: [PATCH] feat(create-instantsearch-app): support Autocomplete in
InstantSearch.js template (#5857)
Co-authored-by: Dhaya <154633+dhayab@users.noreply.github.com>
---
.../src/cli/__tests__/postProcessAnswers.js | 56 +++++++++++
.../create-instantsearch-app/src/cli/index.js | 85 +++++++++++++++++
.../src/cli/postProcessAnswers.js | 3 +
.../templates/InstantSearch.js/.template.js | 1 +
.../templates/InstantSearch.js/index.html.hbs | 8 ++
.../{package.json => package.json.hbs} | 5 +
.../templates/InstantSearch.js/src/app.js.hbs | 95 +++++++++++++++++++
.../InstantSearch.js/src/global.d.ts.hbs | 16 ++++
8 files changed, 269 insertions(+)
rename packages/create-instantsearch-app/src/templates/InstantSearch.js/{package.json => package.json.hbs} (62%)
diff --git a/packages/create-instantsearch-app/src/cli/__tests__/postProcessAnswers.js b/packages/create-instantsearch-app/src/cli/__tests__/postProcessAnswers.js
index 5cc171d926..d96fd2654e 100644
--- a/packages/create-instantsearch-app/src/cli/__tests__/postProcessAnswers.js
+++ b/packages/create-instantsearch-app/src/cli/__tests__/postProcessAnswers.js
@@ -185,4 +185,60 @@ describe('flags', () => {
).toEqual(expect.objectContaining({ insights: false }));
});
});
+
+ describe('autocomplete', () => {
+ test('with usage of autocomplete in searchInputType', async () => {
+ expect(
+ await postProcessAnswers({
+ configuration: {},
+ templateConfig: {
+ libraryName: 'instantsearch.js',
+ flags: {
+ autocomplete: '>= 4.52',
+ },
+ },
+ optionsFromArguments: {},
+ answers: { searchInputType: 'autocomplete' },
+ })
+ ).toEqual(
+ expect.objectContaining({
+ searchInputType: 'autocomplete',
+ flags: expect.objectContaining({ autocomplete: true }),
+ })
+ );
+ });
+
+ test('without usage of autocomplete in searchInputType', async () => {
+ expect(
+ await postProcessAnswers({
+ configuration: {},
+ templateConfig: {
+ libraryName: 'instantsearch.js',
+ flags: {
+ autocomplete: '>= 4.52',
+ },
+ },
+ optionsFromArguments: {},
+ answers: { searchInputType: 'searchbox' },
+ })
+ ).toEqual(
+ expect.objectContaining({
+ searchInputType: 'searchbox',
+ flags: expect.objectContaining({ autocomplete: false }),
+ })
+ );
+ });
+
+ test('without config', async () => {
+ expect(
+ (
+ await postProcessAnswers({
+ configuration: {},
+ templateConfig: {},
+ optionsFromArguments: {},
+ })
+ ).flags
+ ).toEqual(expect.objectContaining({ autocomplete: false }));
+ });
+ });
});
diff --git a/packages/create-instantsearch-app/src/cli/index.js b/packages/create-instantsearch-app/src/cli/index.js
index 217fc8ecce..6d90fb80bb 100755
--- a/packages/create-instantsearch-app/src/cli/index.js
+++ b/packages/create-instantsearch-app/src/cli/index.js
@@ -201,6 +201,91 @@ const getQuestions = ({ appName }) => ({
when: ({ appId, apiKey, indexName }) =>
attributesForFaceting.length === 0 && appId && apiKey && indexName,
},
+ {
+ type: 'list',
+ name: 'searchInputType',
+ message: 'Type of search input',
+ choices: [
+ {
+ name: 'Autocomplete with suggested and recent searches',
+ value: 'autocomplete',
+ },
+ { name: 'Regular search box', value: 'searchbox' },
+ ],
+ default: 'autocomplete',
+ when: ({ libraryVersion, template }) => {
+ const templatePath = getTemplatePath(template);
+ const templateConfig = getAppTemplateConfig(templatePath);
+
+ const selectedLibraryVersion = libraryVersion;
+ const requiredLibraryVersion =
+ templateConfig.flags && templateConfig.flags.autocomplete;
+ const supportsAutocomplete =
+ selectedLibraryVersion &&
+ requiredLibraryVersion &&
+ semver.satisfies(selectedLibraryVersion, requiredLibraryVersion, {
+ includePrerelease: true,
+ });
+
+ return supportsAutocomplete;
+ },
+ },
+ {
+ type: 'input',
+ name: 'querySuggestionsIndexName',
+ message: 'Index name for suggested searches',
+ suffix: `\n ${chalk.gray('This must be a Query Suggestions index')}`,
+ default: 'instant_search_demo_query_suggestions',
+ when: ({ searchInputType }) => searchInputType === 'autocomplete',
+ },
+ {
+ type: 'list',
+ name: 'autocompleteLibraryVersion',
+ message: () => `Autocomplete version`,
+ choices: async () => {
+ const libraryName = '@algolia/autocomplete-js';
+
+ try {
+ const versions = await fetchLibraryVersions(libraryName);
+ const latestStableVersion = semver.maxSatisfying(versions, '1', {
+ includePrerelease: false,
+ });
+
+ if (!latestStableVersion) {
+ return versions;
+ }
+
+ return [
+ new inquirer.Separator('Latest stable version (recommended)'),
+ latestStableVersion,
+ new inquirer.Separator('All versions'),
+ ...versions,
+ ];
+ } catch (err) {
+ const fallbackLibraryVersion = '1.11.0';
+
+ console.log();
+ console.error(
+ chalk.red(
+ `Cannot fetch versions for library "${chalk.cyan(libraryName)}".`
+ )
+ );
+ console.log();
+ console.log(
+ `Fallback to ${chalk.cyan(
+ fallbackLibraryVersion
+ )}, please upgrade the dependency after generating the app.`
+ );
+ console.log();
+
+ return [
+ new inquirer.Separator('Available versions'),
+ fallbackLibraryVersion,
+ ];
+ }
+ },
+ when: ({ searchInputType }) => searchInputType === 'autocomplete',
+ },
],
widget: [
{
diff --git a/packages/create-instantsearch-app/src/cli/postProcessAnswers.js b/packages/create-instantsearch-app/src/cli/postProcessAnswers.js
index 6538774e92..a647cc89ea 100644
--- a/packages/create-instantsearch-app/src/cli/postProcessAnswers.js
+++ b/packages/create-instantsearch-app/src/cli/postProcessAnswers.js
@@ -77,6 +77,9 @@ async function postProcessAnswers({
insights:
Boolean(templateConfig.flags && templateConfig.flags.insights) &&
semver.satisfies(libraryVersion, templateConfig.flags.insights),
+ autocomplete:
+ Boolean(templateConfig.flags && templateConfig.flags.autocomplete) &&
+ combinedAnswers.searchInputType === 'autocomplete',
},
};
}
diff --git a/packages/create-instantsearch-app/src/templates/InstantSearch.js/.template.js b/packages/create-instantsearch-app/src/templates/InstantSearch.js/.template.js
index fe49c9f0ef..ffe93cd517 100644
--- a/packages/create-instantsearch-app/src/templates/InstantSearch.js/.template.js
+++ b/packages/create-instantsearch-app/src/templates/InstantSearch.js/.template.js
@@ -8,6 +8,7 @@ module.exports = {
flags: {
dynamicWidgets: '>= 4.30',
insights: '>= 4.55',
+ autocomplete: '>= 4.52',
},
templateName: 'instantsearch.js',
appName: 'instantsearch.js-app',
diff --git a/packages/create-instantsearch-app/src/templates/InstantSearch.js/index.html.hbs b/packages/create-instantsearch-app/src/templates/InstantSearch.js/index.html.hbs
index af255d4f8c..1469369586 100644
--- a/packages/create-instantsearch-app/src/templates/InstantSearch.js/index.html.hbs
+++ b/packages/create-instantsearch-app/src/templates/InstantSearch.js/index.html.hbs
@@ -9,6 +9,9 @@
+ {{#if flags.autocomplete}}
+
+ {{/if}}
@@ -50,6 +53,11 @@
+ {{#if flags.autocomplete}}
+
+
+
+ {{/if}}