Skip to content

Commit

Permalink
Allowing custom folder name for plugin installation
Browse files Browse the repository at this point in the history
Signed-off-by: Vacha Shah <[email protected]>
  • Loading branch information
VachaShah committed Jun 15, 2021
1 parent 433165a commit 7b02a01
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 41 deletions.
Binary file not shown.
5 changes: 2 additions & 3 deletions src/cli_plugin/install/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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');
Expand Down
11 changes: 9 additions & 2 deletions src/cli_plugin/install/opensearch_dashboards.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
41 changes: 40 additions & 1 deletion src/cli_plugin/install/opensearch_dashboards.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
});
});
});
});

Expand Down
20 changes: 20 additions & 0 deletions src/cli_plugin/install/pack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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(
Expand Down
1 change: 1 addition & 0 deletions src/cli_plugin/install/zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/cli_plugin/install/zip.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 2 additions & 8 deletions src/cli_plugin/list/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,15 @@
* GitHub history for details.
*/

import { statSync, readdirSync, readFileSync } from 'fs';
import { statSync, readdirSync } from 'fs';
import { join } from 'path';

export function list(pluginDir, logger) {
readdirSync(pluginDir).forEach((name) => {
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);
}
});

Expand Down
30 changes: 3 additions & 27 deletions src/cli_plugin/list/list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
);
});
});
});

0 comments on commit 7b02a01

Please sign in to comment.