Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add method to check if a plugin is installed #42

Merged
merged 1 commit into from
Jan 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
};