Skip to content

Commit

Permalink
add method to check if a plugin is installed
Browse files Browse the repository at this point in the history
  • Loading branch information
vutran committed Jan 10, 2017
1 parent 61b1f53 commit 4b33688
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
19 changes: 19 additions & 0 deletions __mocks__/fs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const fs = jest.genMockFromModule('fs');

let _files = [];

/**
* Set files
*
* @param {String[]}
*/
fs.__setFiles = (files) => {
_files = files;
};

// Mocks fs.link
fs.link = (src, dest, callback) => {
callback.call(null);
Expand All @@ -10,4 +21,12 @@ fs.unlink = (dest, callback) => {
callback.call(null);
};

/**
* Returns true if the file path exists in the _files list
*
* @param {String} filePath
* @return {Boolean}
*/
fs.existsSync = filePath => _files.indexOf(filePath) > -1;

module.exports = fs;
16 changes: 16 additions & 0 deletions __tests__/api/plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import path from 'path';
import plugins from '../../src/api/plugins';
import { PLUGIN_PATH } from '../../src/utils/paths';

jest.mock('fs');

describe('plugins', () => {
it('should check if a plugin is installed', () => {
// eslint-disable-next-line global-require
require('fs').__setFiles([
path.resolve(PLUGIN_PATH, 'foobar'),
]);
expect(plugins.isInstalled('foobar')).toBeTruthy();
expect(plugins.isInstalled('bazqux')).toBeFalsy();
});
});
12 changes: 12 additions & 0 deletions src/api/plugins.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const path = require('path');
const fs = require('fs');
const Conf = require('../utils/conf');
const { PLUGIN_PATH } = require('../utils/paths');

// initialize a new config file
const config = new Conf();
Expand All @@ -14,6 +17,14 @@ const getAll = () => config.get('plugins') || [];
// eslint-disable-next-line no-bitwise
const isEnabled = plugin => ~getAll().indexOf(plugin);

/**
* Checks if the plugin is installed
*
* @param {String} plugin - The plugin name
* @return {Boolean}
*/
const isInstalled = plugin => fs.existsSync(path.resolve(PLUGIN_PATH, plugin));

/**
* Enables the plugin by adding it to the config
*
Expand All @@ -39,6 +50,7 @@ const disable = (plugin) => {
module.exports = {
getAll,
isEnabled,
isInstalled,
enable,
disable,
};

0 comments on commit 4b33688

Please sign in to comment.