diff --git a/src/cli_plugin/install/__fixtures__/replies/test_plugin_custom_folder.zip b/src/cli_plugin/install/__fixtures__/replies/test_plugin_custom_folder.zip new file mode 100644 index 000000000000..31feaf74bf0d Binary files /dev/null and b/src/cli_plugin/install/__fixtures__/replies/test_plugin_custom_folder.zip differ diff --git a/src/cli_plugin/install/install.js b/src/cli_plugin/install/install.js index e48c7a1c1349..0b4698d9c2fa 100644 --- a/src/cli_plugin/install/install.js +++ b/src/cli_plugin/install/install.js @@ -40,8 +40,7 @@ import { download } from './download'; import { cleanPrevious, cleanArtifacts } from './cleanup'; import { extract, getPackData } from './pack'; import { renamePlugin } from './rename'; -import { existingInstall, assertVersion } from './opensearch_dashboards'; -import { kebabCase } from 'lodash'; +import { existingInstall, assertVersion, getTargetFolderName } from './opensearch_dashboards'; const mkdir = promisify(Fs.mkdir); @@ -63,7 +62,7 @@ export async function install(settings, logger) { assertVersion(settings); - const targetDir = path.join(settings.pluginDir, kebabCase(settings.plugins[0].id)); + const targetDir = path.join(settings.pluginDir, getTargetFolderName(settings)); await renamePlugin(settings.workingPath, targetDir); logger.log('Plugin installation complete'); diff --git a/src/cli_plugin/install/opensearch_dashboards.js b/src/cli_plugin/install/opensearch_dashboards.js index 1aaf6a5cdf56..27a2853c27db 100644 --- a/src/cli_plugin/install/opensearch_dashboards.js +++ b/src/cli_plugin/install/opensearch_dashboards.js @@ -38,10 +38,11 @@ import { versionSatisfies, cleanVersion } from '../../legacy/utils/version'; export function existingInstall(settings, logger) { try { - statSync(path.join(settings.pluginDir, kebabCase(settings.plugins[0].id))); + const targetFolderName = getTargetFolderName(settings); + statSync(path.join(settings.pluginDir, targetFolderName)); logger.error( - `Plugin ${settings.plugins[0].id} already exists, please remove before installing a new version` + `Plugin ${settings.plugins[0].id} already exists with folder name ${targetFolderName}, please remove before installing a new version` ); process.exit(70); } catch (e) { @@ -64,3 +65,9 @@ export function assertVersion(settings) { ); } } + +export function getTargetFolderName(settings) { + return typeof settings.plugins[0].folderName === 'string' && settings.plugins[0].folderName + ? settings.plugins[0].folderName + : kebabCase(settings.plugins[0].id); +} diff --git a/src/cli_plugin/install/opensearch_dashboards.test.js b/src/cli_plugin/install/opensearch_dashboards.test.js index 4b518ff79c0e..0f4e92335e74 100644 --- a/src/cli_plugin/install/opensearch_dashboards.test.js +++ b/src/cli_plugin/install/opensearch_dashboards.test.js @@ -36,7 +36,7 @@ import fs from 'fs'; import sinon from 'sinon'; import del from 'del'; -import { existingInstall, assertVersion } from './opensearch_dashboards'; +import { existingInstall, assertVersion, getTargetFolderName } from './opensearch_dashboards'; import { Logger } from '../lib/logger'; jest.spyOn(fs, 'statSync'); @@ -159,6 +159,45 @@ describe('opensearchDashboards cli', function () { expect(logger.error.called).toBe(false); }); }); + + describe('getTargetFolderName', function () { + beforeEach(function () { + del.sync(testWorkingPath); + fs.mkdirSync(testWorkingPath, { recursive: true }); + sinon.stub(logger, 'log'); + sinon.stub(logger, 'error'); + }); + + afterEach(function () { + logger.log.restore(); + logger.error.restore(); + del.sync(testWorkingPath); + }); + + it('should return the id for target folder name', function () { + const targetFolderName = settings.plugins[0].id; + + expect(getTargetFolderName(settings)).toEqual(targetFolderName); + }); + + it('should return the custom folder name for target folder name', function () { + const settingsWithCustomFolderName = { + workingPath: testWorkingPath, + tempArchiveFile: tempArchiveFilePath, + plugin: 'test-plugin', + version: '1.0.0', + plugins: [ + { + id: 'foo', + folderName: 'customFolder', + }, + ], + pluginDir, + }; + + expect(getTargetFolderName(settingsWithCustomFolderName)).toEqual('customFolder'); + }); + }); }); }); diff --git a/src/cli_plugin/install/pack.test.js b/src/cli_plugin/install/pack.test.js index 2bf6e4db57c2..5aedcd47aff7 100644 --- a/src/cli_plugin/install/pack.test.js +++ b/src/cli_plugin/install/pack.test.js @@ -120,6 +120,7 @@ describe('opensearchDashboards cli', function () { expect(settings.plugins).toMatchInlineSnapshot(` Array [ Object { + "folderName": undefined, "id": "testPlugin", "opensearchDashboardsVersion": "1.0.0", "stripPrefix": "opensearch-dashboards/test-plugin", @@ -134,6 +135,7 @@ describe('opensearchDashboards cli', function () { expect(settings.plugins).toMatchInlineSnapshot(` Array [ Object { + "folderName": undefined, "id": "testPlugin", "opensearchDashboardsVersion": "5.0.1", "stripPrefix": "opensearch-dashboards/test-plugin", @@ -148,16 +150,19 @@ describe('opensearchDashboards cli', function () { expect(settings.plugins).toMatchInlineSnapshot(` Array [ Object { + "folderName": undefined, "id": "fungerPlugin", "opensearchDashboardsVersion": "1.0.0", "stripPrefix": "opensearch-dashboards/funger-plugin", }, Object { + "folderName": undefined, "id": "pdf", "opensearchDashboardsVersion": "1.0.0", "stripPrefix": "opensearch-dashboards/pdf", }, Object { + "folderName": undefined, "id": "testPlugin", "opensearchDashboardsVersion": "1.0.0", "stripPrefix": "opensearch-dashboards/test-plugin", @@ -166,6 +171,21 @@ describe('opensearchDashboards cli', function () { `); }); + it('populate settings.plugin.folderName', async () => { + await copyReplyFile('test_plugin_custom_folder.zip'); + await getPackData(settings, logger); + expect(settings.plugins).toMatchInlineSnapshot(` + Array [ + Object { + "folderName": "customPluginName", + "id": "testPlugin", + "opensearchDashboardsVersion": "1.0.0", + "stripPrefix": "opensearch-dashboards/test-plugin-custom-folder", + }, + ] + `); + }); + it('throw an error if there is no opensearch-dashboards plugin', async () => { await copyReplyFile('test_plugin_no_opensearch_dashboards.zip'); await expect(getPackData(settings, logger)).rejects.toThrowErrorMatchingInlineSnapshot( diff --git a/src/cli_plugin/install/zip.js b/src/cli_plugin/install/zip.js index 22db10d0a3ce..a3ad488d2e8b 100644 --- a/src/cli_plugin/install/zip.js +++ b/src/cli_plugin/install/zip.js @@ -82,6 +82,7 @@ export function analyzeArchive(archive) { plugins.push({ id: manifest.id, stripPrefix: match[1], + folderName: manifest.folderName, // Plugins must specify their version, and by default that version in the plugin // manifest should match the version of opensearch-dashboards down to the patch level. If these diff --git a/src/cli_plugin/install/zip.test.js b/src/cli_plugin/install/zip.test.js index df5ecd015da5..4bc3f57d41f9 100644 --- a/src/cli_plugin/install/zip.test.js +++ b/src/cli_plugin/install/zip.test.js @@ -63,6 +63,7 @@ describe('opensearchDashboards cli', function () { expect(packages).toMatchInlineSnapshot(` Array [ Object { + "folderName": undefined, "id": "testPlugin", "opensearchDashboardsVersion": "1.0.0", "stripPrefix": "opensearch-dashboards/test-plugin", diff --git a/src/cli_plugin/list/list.js b/src/cli_plugin/list/list.js index 0044be1d5eea..64c61d0e3937 100644 --- a/src/cli_plugin/list/list.js +++ b/src/cli_plugin/list/list.js @@ -30,7 +30,7 @@ * GitHub history for details. */ -import { statSync, readdirSync, readFileSync } from 'fs'; +import { statSync, readdirSync } from 'fs'; import { join } from 'path'; export function list(pluginDir, logger) { @@ -38,13 +38,7 @@ export function list(pluginDir, logger) { const stat = statSync(join(pluginDir, name)); if (stat.isDirectory() && name[0] !== '.') { - try { - const packagePath = join(pluginDir, name, 'opensearch_dashboards.json'); - const pkg = JSON.parse(readFileSync(packagePath, 'utf8')); - logger.log(pkg.id + '@' + pkg.version); - } catch (e) { - throw new Error('Unable to read opensearch_dashboards.json file for plugin ' + name); - } + logger.log(name); } }); diff --git a/src/cli_plugin/list/list.test.js b/src/cli_plugin/list/list.test.js index 62bbca8be9d9..a3fa800e6f01 100644 --- a/src/cli_plugin/list/list.test.js +++ b/src/cli_plugin/list/list.test.js @@ -85,36 +85,12 @@ describe('opensearchDashboards cli', function () { expect(logger.messages).toMatchInlineSnapshot(` Array [ - "log: plugin1@5.0.0-alpha2", - "log: plugin2@3.2.1", - "log: plugin3@1.2.3", + "log: plugin1", + "log: plugin2", + "log: plugin3", "log: ", ] `); }); - - it('list should throw an exception if a plugin does not have a package.json', function () { - createPlugin('plugin1', '1.0.0', pluginDir); - mkdirSync(join(pluginDir, 'empty-plugin'), { recursive: true }); - - expect(function () { - list(pluginDir, logger); - }).toThrowErrorMatchingInlineSnapshot( - `"Unable to read opensearch_dashboards.json file for plugin empty-plugin"` - ); - }); - - it('list should throw an exception if a plugin have an empty package.json', function () { - createPlugin('plugin1', '1.0.0', pluginDir); - const invalidPluginDir = join(pluginDir, 'invalid-plugin'); - mkdirSync(invalidPluginDir, { recursive: true }); - writeFileSync(join(invalidPluginDir, 'package.json'), ''); - - expect(function () { - list(pluginDir, logger); - }).toThrowErrorMatchingInlineSnapshot( - `"Unable to read opensearch_dashboards.json file for plugin invalid-plugin"` - ); - }); }); });