|
| 1 | +const semver = require('semver') |
| 2 | +const path = require('path') |
1 | 3 | const { updateTableValues, isTableExists } = require('../../../util/database') |
| 4 | +const getJsonfileData = require('../../../util/get-jsonfile-data') |
| 5 | +const runComposerCommand = require('../../../util/run-composer') |
| 6 | + |
| 7 | +const magentoModuleElasticSearch8 = 'magento/module-elasticsearch-8' |
| 8 | + |
| 9 | +/** |
| 10 | + * @param {import('../../../../typings/context').ListrContext} ctx |
| 11 | + */ |
| 12 | +const isNeedToInstallElasticSearch8Module = async (ctx) => { |
| 13 | + /** |
| 14 | + * @type {{ packages: { name: string, version: string }[] } | null} |
| 15 | + */ |
| 16 | + const composerLockData = await getJsonfileData( |
| 17 | + path.join(ctx.config.baseConfig.magentoDir, 'composer.lock') |
| 18 | + ) |
| 19 | + |
| 20 | + if (!composerLockData) { |
| 21 | + return true |
| 22 | + } |
| 23 | + |
| 24 | + return !composerLockData.packages.some( |
| 25 | + ({ name }) => name === magentoModuleElasticSearch8 |
| 26 | + ) |
| 27 | +} |
2 | 28 |
|
3 | 29 | /** |
4 | 30 | * @returns {import('listr2').ListrTask<import('../../../../typings/context').ListrContext>} |
5 | 31 | */ |
6 | | -module.exports = () => ({ |
| 32 | +const configureElasticSearchInDatabase = () => ({ |
7 | 33 | title: 'Configuring Elasticsearch', |
8 | | - skip: async (ctx) => |
9 | | - !(await isTableExists('magento', 'core_config_data', ctx)), |
| 34 | + skip: async (ctx) => { |
| 35 | + const isCoreConfigDataExists = await isTableExists( |
| 36 | + 'magento', |
| 37 | + 'core_config_data', |
| 38 | + ctx |
| 39 | + ) |
| 40 | + if (isCoreConfigDataExists) { |
| 41 | + const elasticsearchConfig = await ctx.databaseConnection.query( |
| 42 | + `SELECT path,value FROM core_config_data WHERE path='catalog/search/engine';` |
| 43 | + ) |
| 44 | + |
| 45 | + if (elasticsearchConfig.length > 0) { |
| 46 | + const { major: parsedESMajorVersion } = semver.parse( |
| 47 | + ctx.elasticSearchVersion |
| 48 | + ) || { major: 7 } |
| 49 | + const elasticsearchSearchEngine = `elasticsearch${parsedESMajorVersion}` |
| 50 | + |
| 51 | + return ( |
| 52 | + elasticsearchConfig[0].value === elasticsearchSearchEngine |
| 53 | + ) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return false |
| 58 | + }, |
10 | 59 | task: async (ctx, task) => { |
11 | 60 | const { ports, databaseConnection, isDockerDesktop } = ctx |
12 | 61 | const hostMachine = !isDockerDesktop |
13 | 62 | ? '127.0.0.1' |
14 | 63 | : 'host.docker.internal' |
| 64 | + |
| 65 | + const { major: parsedESMajorVersion } = semver.parse( |
| 66 | + ctx.elasticSearchVersion |
| 67 | + ) || { major: 7 } |
| 68 | + |
| 69 | + const elasticsearchConfig = { |
| 70 | + 'catalog/search/engine': `elasticsearch${parsedESMajorVersion}`, |
| 71 | + [`catalog/search/elasticsearch${parsedESMajorVersion}_server_hostname`]: |
| 72 | + hostMachine, |
| 73 | + [`catalog/search/elasticsearch${parsedESMajorVersion}_server_port`]: `${ports.elasticsearch}` |
| 74 | + } |
| 75 | + |
15 | 76 | await updateTableValues( |
16 | 77 | 'core_config_data', |
17 | | - [ |
18 | | - { path: 'catalog/search/engine', value: 'elasticsearch7' }, |
19 | | - { |
20 | | - path: 'catalog/search/elasticsearch7_server_hostname', |
21 | | - value: hostMachine |
22 | | - }, |
23 | | - { |
24 | | - path: 'catalog/search/elasticsearch7_server_port', |
25 | | - value: `${ports.elasticsearch}` |
26 | | - } |
27 | | - ], |
| 78 | + Object.entries(elasticsearchConfig).map(([path, value]) => ({ |
| 79 | + path, |
| 80 | + value |
| 81 | + })), |
28 | 82 | { databaseConnection, task } |
29 | 83 | ) |
30 | 84 | } |
31 | 85 | }) |
| 86 | + |
| 87 | +/** |
| 88 | + * @returns {import('listr2').ListrTask<import('../../../../typings/context').ListrContext>} |
| 89 | + */ |
| 90 | +const installElasticSearch8Module = () => ({ |
| 91 | + title: 'Installing Magento ElasticSearch8 Module', |
| 92 | + task: async (ctx, task) => { |
| 93 | + await runComposerCommand( |
| 94 | + ctx, |
| 95 | + `require ${magentoModuleElasticSearch8} --with-all-dependencies`, |
| 96 | + { |
| 97 | + callback: !ctx.verbose |
| 98 | + ? undefined |
| 99 | + : (t) => { |
| 100 | + task.output = t |
| 101 | + } |
| 102 | + } |
| 103 | + ) |
| 104 | + }, |
| 105 | + options: { |
| 106 | + bottomBar: 10 |
| 107 | + } |
| 108 | +}) |
| 109 | + |
| 110 | +/** |
| 111 | + * @returns {import('listr2').ListrTask<import('../../../../typings/context').ListrContext>} |
| 112 | + */ |
| 113 | +module.exports = () => ({ |
| 114 | + task: async (ctx, task) => { |
| 115 | + const { major: parsedESMajorVersion } = semver.parse( |
| 116 | + ctx.elasticSearchVersion |
| 117 | + ) || { major: 7 } |
| 118 | + |
| 119 | + if ( |
| 120 | + parsedESMajorVersion === 8 && |
| 121 | + (await isNeedToInstallElasticSearch8Module(ctx)) |
| 122 | + ) { |
| 123 | + return task.newListr([ |
| 124 | + installElasticSearch8Module(), |
| 125 | + configureElasticSearchInDatabase() |
| 126 | + ]) |
| 127 | + } |
| 128 | + |
| 129 | + return task.newListr(configureElasticSearchInDatabase()) |
| 130 | + } |
| 131 | +}) |
0 commit comments