validate type and id
This commit is contained in:
@@ -3,7 +3,7 @@ import { getPluginJson, validatePluginJson } from './pluginValidation';
|
||||
describe('pluginValdation', () => {
|
||||
describe('plugin.json', () => {
|
||||
test('missing plugin.json file', () => {
|
||||
expect(() => getPluginJson(`${__dirname}/mocks/missing-plugin-json`)).toThrow('plugin.json file is missing!');
|
||||
expect(() => getPluginJson(`${__dirname}/mocks/missing-plugin.json`)).toThrowError();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -22,15 +22,24 @@ export const validatePluginJson = (pluginJson: any) => {
|
||||
if (!pluginJson.info.version) {
|
||||
throw new Error('Plugin info.version is missing in plugin.json');
|
||||
}
|
||||
|
||||
const types = ['panel', 'datasource', 'app'];
|
||||
const type = pluginJson.type;
|
||||
if (!types.includes(type)) {
|
||||
throw new Error('Invalid plugin type in plugin.json: ' + type);
|
||||
}
|
||||
|
||||
if (!pluginJson.id.endsWith('-' + type)) {
|
||||
throw new Error('[plugin.json] id should end with: -' + type);
|
||||
}
|
||||
};
|
||||
|
||||
export const getPluginJson = (root: string = process.cwd()): PluginJSONSchema => {
|
||||
export const getPluginJson = (path: string): PluginJSONSchema => {
|
||||
let pluginJson;
|
||||
|
||||
try {
|
||||
pluginJson = require(path.resolve(root, 'src/plugin.json'));
|
||||
pluginJson = require(path);
|
||||
} catch (e) {
|
||||
throw new Error('plugin.json file is missing!');
|
||||
throw new Error('Unable to find: ' + path);
|
||||
}
|
||||
|
||||
validatePluginJson(pluginJson);
|
||||
|
||||
Reference in New Issue
Block a user