diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-05-02 22:02:45 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-05-02 22:02:45 +0200 |
commit | 41e687255717c43d1bf2745e7b806057c7de16ae (patch) | |
tree | 6be8992936b0c0ec006fc46d5f7577791494aba1 /lib/models | |
parent | 8de0d6186e8a10cdd5a3efe9a4cf0afcae734223 (diff) | |
parent | 30c96c37c2145a28710e2875c677d37156fdaa92 (diff) | |
download | gitbook-41e687255717c43d1bf2745e7b806057c7de16ae.zip gitbook-41e687255717c43d1bf2745e7b806057c7de16ae.tar.gz gitbook-41e687255717c43d1bf2745e7b806057c7de16ae.tar.bz2 |
Merge branch 'fix/plugins_deps'
Diffstat (limited to 'lib/models')
-rw-r--r-- | lib/models/__tests__/plugin.js | 2 | ||||
-rw-r--r-- | lib/models/__tests__/pluginDependency.js | 47 | ||||
-rw-r--r-- | lib/models/config.js | 28 | ||||
-rw-r--r-- | lib/models/plugin.js | 13 | ||||
-rw-r--r-- | lib/models/pluginDependency.js | 115 |
5 files changed, 203 insertions, 2 deletions
diff --git a/lib/models/__tests__/plugin.js b/lib/models/__tests__/plugin.js index 81d9d51..b229664 100644 --- a/lib/models/__tests__/plugin.js +++ b/lib/models/__tests__/plugin.js @@ -1,5 +1,3 @@ -jest.autoMockOff(); - describe('Plugin', function() { var Plugin = require('../plugin'); diff --git a/lib/models/__tests__/pluginDependency.js b/lib/models/__tests__/pluginDependency.js new file mode 100644 index 0000000..8aa55fb --- /dev/null +++ b/lib/models/__tests__/pluginDependency.js @@ -0,0 +1,47 @@ +describe('PluginDependency', function() { + var PluginDependency = require('../pluginDependency'); + + describe('createFromString', function() { + it('must parse name', function() { + var plugin = PluginDependency.createFromString('hello'); + expect(plugin.getName()).toBe('hello'); + expect(plugin.getVersion()).toBe('*'); + }); + + it('must parse state', function() { + var plugin = PluginDependency.createFromString('-hello'); + expect(plugin.getName()).toBe('hello'); + expect(plugin.isEnabled()).toBe(false); + }); + + describe('Version', function() { + it('must parse version', function() { + var plugin = PluginDependency.createFromString('hello@1.0.0'); + expect(plugin.getName()).toBe('hello'); + expect(plugin.getVersion()).toBe('1.0.0'); + }); + + it('must parse semver', function() { + var plugin = PluginDependency.createFromString('hello@>=4.0.0'); + expect(plugin.getName()).toBe('hello'); + expect(plugin.getVersion()).toBe('>=4.0.0'); + }); + }); + + describe('GIT Version', function() { + it('must handle HTTPS urls', function() { + var plugin = PluginDependency.createFromString('hello@git+https://github.com/GitbookIO/plugin-ga.git'); + expect(plugin.getName()).toBe('hello'); + expect(plugin.getVersion()).toBe('git+https://github.com/GitbookIO/plugin-ga.git'); + }); + + it('must handle SSH urls', function() { + var plugin = PluginDependency.createFromString('hello@git+ssh://samy@github.com/GitbookIO/plugin-ga.git'); + expect(plugin.getName()).toBe('hello'); + expect(plugin.getVersion()).toBe('git+ssh://samy@github.com/GitbookIO/plugin-ga.git'); + }); + }); + }); +}); + + diff --git a/lib/models/config.js b/lib/models/config.js index 6ee03e4..83dd6d4 100644 --- a/lib/models/config.js +++ b/lib/models/config.js @@ -2,6 +2,7 @@ var is = require('is'); var Immutable = require('immutable'); var File = require('./file'); +var PluginDependency = require('./pluginDependency'); var configDefault = require('../constants/configDefault'); var Config = Immutable.Record({ @@ -53,6 +54,33 @@ Config.prototype.setValue = function(keyPath, value) { }; /** + Return a list of plugin dependencies + + @return {List<PluginDependency>} +*/ +Config.prototype.getPluginDependencies = function() { + var plugins = this.getValue('plugins'); + + if (is.string(plugins)) { + return PluginDependency.listFromString(plugins); + } else { + return PluginDependency.listFromArray(plugins); + } +}; + +/** + Update the list of plugins dependencies + + @param {List<PluginDependency>} + @return {Config} +*/ +Config.prototype.setPluginDependencies = function(deps) { + var plugins = PluginDependency.listToArray(deps); + + return this.setValue('plugins', plugins); +}; + +/** Create a new config for a file @param {File} file diff --git a/lib/models/plugin.js b/lib/models/plugin.js index dd7bc90..23019aa 100644 --- a/lib/models/plugin.js +++ b/lib/models/plugin.js @@ -140,6 +140,19 @@ Plugin.createFromString = function(s) { }; /** + Create a plugin from a dependency + + @param {PluginDependency} + @return {Plugin} +*/ +Plugin.createFromDep = function(dep) { + return new Plugin({ + name: dep.getName(), + version: dep.getVersion() + }); +}; + +/** Return NPM id for a plugin name @param {String} diff --git a/lib/models/pluginDependency.js b/lib/models/pluginDependency.js new file mode 100644 index 0000000..bb8e4b4 --- /dev/null +++ b/lib/models/pluginDependency.js @@ -0,0 +1,115 @@ +var is = require('is'); +var Immutable = require('immutable'); + +var DEFAULT_VERSION = '*'; + +/* + PluginDependency represents the informations about a plugin + stored in config.plugins +*/ +var PluginDependency = Immutable.Record({ + name: String(), + + // Requirement version (ex: ">1.0.0") + version: String(DEFAULT_VERSION), + + // Is this plugin enabled or disabled? + enabled: Boolean(true) +}, 'PluginDependency'); + +PluginDependency.prototype.getName = function() { + return this.get('name'); +}; + +PluginDependency.prototype.getVersion = function() { + return this.get('version'); +}; + +PluginDependency.prototype.isEnabled = function() { + return this.get('enabled'); +}; + +/** + Create a plugin from a string + + @param {String} + @return {Plugin|undefined} +*/ +PluginDependency.createFromString = function(s) { + var parts = s.split('@'); + var name = parts[0]; + var version = parts.slice(1).join('@'); + var enabled = true; + + if (name[0] === '-') { + enabled = false; + name = name.slice(1); + } + + return new PluginDependency({ + name: name, + version: version || DEFAULT_VERSION, + enabled: enabled + }); +}; + +/** + Create a PluginDependency from a string + + @param {String} + @return {List<PluginDependency>} +*/ +PluginDependency.listFromString = function(s) { + var parts = s.split(','); + return PluginDependency.listFromArray(parts); +}; + +/** + Create a PluginDependency from an array + + @param {Array} + @return {List<PluginDependency>} +*/ +PluginDependency.listFromArray = function(arr) { + return Immutable.List(arr) + .map(function(entry) { + if (is.string(entry)) { + return PluginDependency.createFromString(entry); + } else { + return PluginDependency({ + name: entry.name, + version: entry.version + }); + } + }) + .filter(function(dep) { + return Boolean(dep.getName()); + }); +}; + +/** + Export plugin dependencies as an array + + @param {List<PluginDependency>} + @return {Array<String>} +*/ +PluginDependency.listToArray = function(arr) { + return arr + .map(function(dep) { + var result; + + if (dep.isEnabled()) { + result += '-'; + } + + result += dep.getName(); + if (dep.getVersion() !== DEFAULT_VERSION) { + result += '@' + dep.getVersion(); + } + + return result; + }) + .toJS(); +}; + +module.exports = PluginDependency; |