diff options
Diffstat (limited to 'lib/plugins')
27 files changed, 0 insertions, 1002 deletions
diff --git a/lib/plugins/__tests__/findForBook.js b/lib/plugins/__tests__/findForBook.js deleted file mode 100644 index d8af2e9..0000000 --- a/lib/plugins/__tests__/findForBook.js +++ /dev/null @@ -1,19 +0,0 @@ -var path = require('path'); - -var Book = require('../../models/book'); -var createNodeFS = require('../../fs/node'); -var findForBook = require('../findForBook'); - -describe('findForBook', function() { - var fs = createNodeFS( - path.resolve(__dirname, '../../..') - ); - var book = Book.createForFS(fs); - - it('should list default plugins', function() { - return findForBook(book) - .then(function(plugins) { - expect(plugins.has('fontsettings')).toBeTruthy(); - }); - }); -}); diff --git a/lib/plugins/__tests__/findInstalled.js b/lib/plugins/__tests__/findInstalled.js deleted file mode 100644 index 9377190..0000000 --- a/lib/plugins/__tests__/findInstalled.js +++ /dev/null @@ -1,25 +0,0 @@ -var path = require('path'); -var Immutable = require('immutable'); - -describe('findInstalled', function() { - var findInstalled = require('../findInstalled'); - - it('must list default plugins for gitbook directory', function() { - // Read gitbook-plugins from package.json - var pkg = require(path.resolve(__dirname, '../../../package.json')); - var gitbookPlugins = Immutable.Seq(pkg.dependencies) - .filter(function(v, k) { - return k.indexOf('gitbook-plugin') === 0; - }) - .cacheResult(); - - return findInstalled(path.resolve(__dirname, '../../../')) - .then(function(plugins) { - expect(plugins.size >= gitbookPlugins.size).toBeTruthy(); - - expect(plugins.has('fontsettings')).toBe(true); - expect(plugins.has('search')).toBe(true); - }); - }); - -}); diff --git a/lib/plugins/__tests__/installPlugin.js b/lib/plugins/__tests__/installPlugin.js deleted file mode 100644 index 0c1a346..0000000 --- a/lib/plugins/__tests__/installPlugin.js +++ /dev/null @@ -1,29 +0,0 @@ -var path = require('path'); - -var PluginDependency = require('../../models/pluginDependency'); -var Book = require('../../models/book'); -var NodeFS = require('../../fs/node'); -var installPlugin = require('../installPlugin'); - -var Parse = require('../../parse'); - -describe('installPlugin', function() { - var book; - - this.timeout(30000); - - before(function() { - var fs = NodeFS(path.resolve(__dirname, '../../../')); - var baseBook = Book.createForFS(fs); - - return Parse.parseConfig(baseBook) - .then(function(_book) { - book = _book; - }); - }); - - it('must install a plugin from NPM', function() { - var dep = PluginDependency.createFromString('ga'); - return installPlugin(book, dep); - }); -}); diff --git a/lib/plugins/__tests__/installPlugins.js b/lib/plugins/__tests__/installPlugins.js deleted file mode 100644 index 1a66f90..0000000 --- a/lib/plugins/__tests__/installPlugins.js +++ /dev/null @@ -1,30 +0,0 @@ -var path = require('path'); - -var Book = require('../../models/book'); -var NodeFS = require('../../fs/node'); -var installPlugins = require('../installPlugins'); - -var Parse = require('../../parse'); - -describe('installPlugins', function() { - var book; - - this.timeout(30000); - - before(function() { - var fs = NodeFS(path.resolve(__dirname, '../../../')); - var baseBook = Book.createForFS(fs); - - return Parse.parseConfig(baseBook) - .then(function(_book) { - book = _book; - }); - }); - - it('must install all plugins from NPM', function() { - return installPlugins(book) - .then(function(n) { - expect(n).toBe(2); - }); - }); -}); diff --git a/lib/plugins/__tests__/listDependencies.js b/lib/plugins/__tests__/listDependencies.js deleted file mode 100644 index 940faba..0000000 --- a/lib/plugins/__tests__/listDependencies.js +++ /dev/null @@ -1,38 +0,0 @@ -var PluginDependency = require('../../models/pluginDependency'); -var listDependencies = require('../listDependencies'); -var toNames = require('../toNames'); - -describe('listDependencies', function() { - it('must list default', function() { - var deps = PluginDependency.listFromString('ga,great'); - var plugins = listDependencies(deps); - var names = toNames(plugins); - - expect(names).toEqual([ - 'ga', 'great', - 'highlight', 'search', 'lunr', 'sharing', 'fontsettings', - 'theme-default' ]); - }); - - it('must list from array with -', function() { - var deps = PluginDependency.listFromString('ga,-great'); - var plugins = listDependencies(deps); - var names = toNames(plugins); - - expect(names).toEqual([ - 'ga', - 'highlight', 'search', 'lunr', 'sharing', 'fontsettings', - 'theme-default' ]); - }); - - it('must remove default plugins using -', function() { - var deps = PluginDependency.listFromString('ga,-search'); - var plugins = listDependencies(deps); - var names = toNames(plugins); - - expect(names).toEqual([ - 'ga', - 'highlight', 'lunr', 'sharing', 'fontsettings', - 'theme-default' ]); - }); -}); diff --git a/lib/plugins/__tests__/locateRootFolder.js b/lib/plugins/__tests__/locateRootFolder.js deleted file mode 100644 index bb414a3..0000000 --- a/lib/plugins/__tests__/locateRootFolder.js +++ /dev/null @@ -1,10 +0,0 @@ -var path = require('path'); -var locateRootFolder = require('../locateRootFolder'); - -describe('locateRootFolder', function() { - it('should correctly resolve the node_modules for gitbook', function() { - expect(locateRootFolder()).toBe( - path.resolve(__dirname, '../../../') - ); - }); -}); diff --git a/lib/plugins/__tests__/resolveVersion.js b/lib/plugins/__tests__/resolveVersion.js deleted file mode 100644 index 1877c9e..0000000 --- a/lib/plugins/__tests__/resolveVersion.js +++ /dev/null @@ -1,22 +0,0 @@ -var PluginDependency = require('../../models/pluginDependency'); -var resolveVersion = require('../resolveVersion'); - -describe('resolveVersion', function() { - it('must skip resolving and return non-semver versions', function() { - var plugin = PluginDependency.createFromString('ga@git+ssh://samy@github.com/GitbookIO/plugin-ga.git'); - - return resolveVersion(plugin) - .then(function(version) { - expect(version).toBe('git+ssh://samy@github.com/GitbookIO/plugin-ga.git'); - }); - }); - - it('must resolve a normal plugin dependency', function() { - var plugin = PluginDependency.createFromString('ga@>0.9.0 < 1.0.1'); - - return resolveVersion(plugin) - .then(function(version) { - expect(version).toBe('1.0.0'); - }); - }); -}); diff --git a/lib/plugins/__tests__/sortDependencies.js b/lib/plugins/__tests__/sortDependencies.js deleted file mode 100644 index 87df477..0000000 --- a/lib/plugins/__tests__/sortDependencies.js +++ /dev/null @@ -1,42 +0,0 @@ -var PluginDependency = require('../../models/pluginDependency'); -var sortDependencies = require('../sortDependencies'); -var toNames = require('../toNames'); - -describe('sortDependencies', function() { - it('must load themes after plugins', function() { - var allPlugins = PluginDependency.listFromArray([ - 'hello', - 'theme-test', - 'world' - ]); - - var sorted = sortDependencies(allPlugins); - var names = toNames(sorted); - - expect(names).toEqual([ - 'hello', - 'world', - 'theme-test' - ]); - }); - - it('must keep order of themes', function() { - var allPlugins = PluginDependency.listFromArray([ - 'theme-test', - 'theme-test1', - 'hello', - 'theme-test2', - 'world' - ]); - var sorted = sortDependencies(allPlugins); - var names = toNames(sorted); - - expect(names).toEqual([ - 'hello', - 'world', - 'theme-test', - 'theme-test1', - 'theme-test2' - ]); - }); -});
\ No newline at end of file diff --git a/lib/plugins/__tests__/validatePlugin.js b/lib/plugins/__tests__/validatePlugin.js deleted file mode 100644 index 635423c..0000000 --- a/lib/plugins/__tests__/validatePlugin.js +++ /dev/null @@ -1,16 +0,0 @@ -var Promise = require('../../utils/promise'); -var Plugin = require('../../models/plugin'); -var validatePlugin = require('../validatePlugin'); - -describe('validatePlugin', function() { - it('must not validate a not loaded plugin', function() { - var plugin = Plugin.createFromString('test'); - - return validatePlugin(plugin) - .then(function() { - throw new Error('Should not be validate'); - }, function(err) { - return Promise(); - }); - }); -}); diff --git a/lib/plugins/findForBook.js b/lib/plugins/findForBook.js deleted file mode 100644 index be2ad9f..0000000 --- a/lib/plugins/findForBook.js +++ /dev/null @@ -1,34 +0,0 @@ -var Immutable = require('immutable'); - -var Promise = require('../utils/promise'); -var timing = require('../utils/timing'); -var findInstalled = require('./findInstalled'); -var locateRootFolder = require('./locateRootFolder'); - -/** - * List all plugins installed in a book - * - * @param {Book} - * @return {Promise<OrderedMap<String:Plugin>>} - */ -function findForBook(book) { - return timing.measure( - 'plugins.findForBook', - - Promise.all([ - findInstalled(locateRootFolder()), - findInstalled(book.getRoot()) - ]) - - // Merge all plugins - .then(function(results) { - return Immutable.List(results) - .reduce(function(out, result) { - return out.merge(result); - }, Immutable.OrderedMap()); - }) - ); -} - - -module.exports = findForBook; diff --git a/lib/plugins/findInstalled.js b/lib/plugins/findInstalled.js deleted file mode 100644 index 06cc6c4..0000000 --- a/lib/plugins/findInstalled.js +++ /dev/null @@ -1,91 +0,0 @@ -var readInstalled = require('read-installed'); -var Immutable = require('immutable'); -var path = require('path'); - -var Promise = require('../utils/promise'); -var fs = require('../utils/fs'); -var Plugin = require('../models/plugin'); -var PREFIX = require('../constants/pluginPrefix'); - -/** - * Validate if a package name is a GitBook plugin - * - * @return {Boolean} - */ -function validateId(name) { - return name && name.indexOf(PREFIX) === 0; -} - - -/** - * List all packages installed inside a folder - * - * @param {String} folder - * @return {OrderedMap<String:Plugin>} - */ -function findInstalled(folder) { - var options = { - dev: false, - log: function() {}, - depth: 4 - }; - var results = Immutable.OrderedMap(); - - function onPackage(pkg, parent) { - if (!pkg.name) return; - - var name = pkg.name; - var version = pkg.version; - var pkgPath = pkg.realPath; - var depth = pkg.depth; - var dependencies = pkg.dependencies; - - var pluginName = name.slice(PREFIX.length); - - if (!validateId(name)){ - if (parent) return; - } else { - results = results.set(pluginName, Plugin({ - name: pluginName, - version: version, - path: pkgPath, - depth: depth, - parent: parent - })); - } - - Immutable.Map(dependencies).forEach(function(dep) { - onPackage(dep, pluginName); - }); - } - - // Search for gitbook-plugins in node_modules folder - var node_modules = path.join(folder, 'node_modules'); - - // List all folders in node_modules - return fs.readdir(node_modules) - .fail(function() { - return Promise([]); - }) - .then(function(modules) { - return Promise.serie(modules, function(module) { - // Not a gitbook-plugin - if (!validateId(module)) { - return Promise(); - } - - // Read gitbook-plugin package details - var module_folder = path.join(node_modules, module); - return Promise.nfcall(readInstalled, module_folder, options) - .then(function(data) { - onPackage(data); - }); - }); - }) - .then(function() { - // Return installed plugins - return results; - }); -} - -module.exports = findInstalled; diff --git a/lib/plugins/index.js b/lib/plugins/index.js deleted file mode 100644 index 607a7f1..0000000 --- a/lib/plugins/index.js +++ /dev/null @@ -1,10 +0,0 @@ - -module.exports = { - loadForBook: require('./loadForBook'), - validateConfig: require('./validateConfig'), - installPlugins: require('./installPlugins'), - listResources: require('./listResources'), - listBlocks: require('./listBlocks'), - listFilters: require('./listFilters') -}; - diff --git a/lib/plugins/installPlugin.js b/lib/plugins/installPlugin.js deleted file mode 100644 index 37852df..0000000 --- a/lib/plugins/installPlugin.js +++ /dev/null @@ -1,47 +0,0 @@ -var npmi = require('npmi'); - -var Promise = require('../utils/promise'); -var resolveVersion = require('./resolveVersion'); - -/** - Install a plugin for a book - - @param {Book} - @param {PluginDependency} - @return {Promise} -*/ -function installPlugin(book, plugin) { - var logger = book.getLogger(); - - var installFolder = book.getRoot(); - var name = plugin.getName(); - var requirement = plugin.getVersion(); - - logger.info.ln(''); - logger.info.ln('installing plugin "' + name + '"'); - - // Find a version to install - return resolveVersion(plugin) - .then(function(version) { - if (!version) { - throw new Error('Found no satisfactory version for plugin "' + name + '" with requirement "' + requirement + '"'); - } - - logger.info.ln('install plugin "' + name +'" (' + requirement + ') from NPM with version', version); - return Promise.nfcall(npmi, { - 'name': plugin.getNpmID(), - 'version': version, - 'path': installFolder, - 'npmLoad': { - 'loglevel': 'silent', - 'loaded': true, - 'prefix': installFolder - } - }); - }) - .then(function() { - logger.info.ok('plugin "' + name + '" installed with success'); - }); -} - -module.exports = installPlugin; diff --git a/lib/plugins/installPlugins.js b/lib/plugins/installPlugins.js deleted file mode 100644 index 307c41e..0000000 --- a/lib/plugins/installPlugins.js +++ /dev/null @@ -1,48 +0,0 @@ -var npmi = require('npmi'); - -var DEFAULT_PLUGINS = require('../constants/defaultPlugins'); -var Promise = require('../utils/promise'); -var installPlugin = require('./installPlugin'); - -/** - Install plugin requirements for a book - - @param {Book} - @return {Promise<Number>} -*/ -function installPlugins(book) { - var logger = book.getLogger(); - var config = book.getConfig(); - var plugins = config.getPluginDependencies(); - - // Remove default plugins - // (only if version is same as installed) - plugins = plugins.filterNot(function(plugin) { - var dependency = DEFAULT_PLUGINS.find(function(dep) { - return dep.getName() === plugin.getName(); - }); - - return ( - // Disabled plugin - !plugin.isEnabled() || - - // Or default one installed in GitBook itself - (dependency && - plugin.getVersion() === dependency.getVersion()) - ); - }); - - if (plugins.size == 0) { - logger.info.ln('nothing to install!'); - return Promise(); - } - - logger.info.ln('installing', plugins.size, 'plugins using npm@' + npmi.NPM_VERSION); - - return Promise.forEach(plugins, function(plugin) { - return installPlugin(book, plugin); - }) - .thenResolve(plugins.size); -} - -module.exports = installPlugins; diff --git a/lib/plugins/listBlocks.js b/lib/plugins/listBlocks.js deleted file mode 100644 index 3ac28af..0000000 --- a/lib/plugins/listBlocks.js +++ /dev/null @@ -1,18 +0,0 @@ -var Immutable = require('immutable'); - -/** - List blocks from a list of plugins - - @param {OrderedMap<String:Plugin>} - @return {Map<String:TemplateBlock>} -*/ -function listBlocks(plugins) { - return plugins - .reverse() - .reduce(function(result, plugin) { - var blocks = plugin.getBlocks(); - return result.merge(blocks); - }, Immutable.Map()); -} - -module.exports = listBlocks; diff --git a/lib/plugins/listDependencies.js b/lib/plugins/listDependencies.js deleted file mode 100644 index d52eaa9..0000000 --- a/lib/plugins/listDependencies.js +++ /dev/null @@ -1,33 +0,0 @@ -var DEFAULT_PLUGINS = require('../constants/defaultPlugins'); -var sortDependencies = require('./sortDependencies'); - -/** - * List all dependencies for a book, including default plugins. - * It returns a concat with default plugins and remove disabled ones. - * - * @param {List<PluginDependency>} deps - * @return {List<PluginDependency>} - */ -function listDependencies(deps) { - // Extract list of plugins to disable (starting with -) - var toRemove = deps - .filter(function(plugin) { - return !plugin.isEnabled(); - }) - .map(function(plugin) { - return plugin.getName(); - }); - - // Concat with default plugins - deps = deps.concat(DEFAULT_PLUGINS); - - // Remove plugins - deps = deps.filterNot(function(plugin) { - return toRemove.includes(plugin.getName()); - }); - - // Sort - return sortDependencies(deps); -} - -module.exports = listDependencies; diff --git a/lib/plugins/listDepsForBook.js b/lib/plugins/listDepsForBook.js deleted file mode 100644 index 196e3aa..0000000 --- a/lib/plugins/listDepsForBook.js +++ /dev/null @@ -1,18 +0,0 @@ -var listDependencies = require('./listDependencies'); - -/** - * List all plugin requirements for a book. - * It can be different from the final list of plugins, - * since plugins can have their own dependencies - * - * @param {Book} - * @return {List<PluginDependency>} - */ -function listDepsForBook(book) { - var config = book.getConfig(); - var plugins = config.getPluginDependencies(); - - return listDependencies(plugins); -} - -module.exports = listDepsForBook; diff --git a/lib/plugins/listFilters.js b/lib/plugins/listFilters.js deleted file mode 100644 index 4d8a471..0000000 --- a/lib/plugins/listFilters.js +++ /dev/null @@ -1,17 +0,0 @@ -var Immutable = require('immutable'); - -/** - List filters from a list of plugins - - @param {OrderedMap<String:Plugin>} - @return {Map<String:Function>} -*/ -function listFilters(plugins) { - return plugins - .reverse() - .reduce(function(result, plugin) { - return result.merge(plugin.getFilters()); - }, Immutable.Map()); -} - -module.exports = listFilters; diff --git a/lib/plugins/listResources.js b/lib/plugins/listResources.js deleted file mode 100644 index fe31b5a..0000000 --- a/lib/plugins/listResources.js +++ /dev/null @@ -1,45 +0,0 @@ -var Immutable = require('immutable'); -var path = require('path'); - -var LocationUtils = require('../utils/location'); -var PLUGIN_RESOURCES = require('../constants/pluginResources'); - -/** - List all resources from a list of plugins - - @param {OrderedMap<String:Plugin>} - @param {String} type - @return {Map<String:List<{url, path}>} -*/ -function listResources(plugins, resources) { - return plugins.reduce(function(result, plugin) { - var npmId = plugin.getNpmID(); - var pluginResources = resources.get(plugin.getName()); - - PLUGIN_RESOURCES.forEach(function(resourceType) { - var assets = pluginResources.get(resourceType); - if (!assets) return; - - var list = result.get(resourceType) || Immutable.List(); - - assets = assets.map(function(assetFile) { - if (LocationUtils.isExternal(assetFile)) { - return { - url: assetFile - }; - } else { - return { - path: LocationUtils.normalize(path.join(npmId, assetFile)) - }; - } - }); - - list = list.concat(assets); - result = result.set(resourceType, list); - }); - - return result; - }, Immutable.Map()); -} - -module.exports = listResources; diff --git a/lib/plugins/loadForBook.js b/lib/plugins/loadForBook.js deleted file mode 100644 index 757677e..0000000 --- a/lib/plugins/loadForBook.js +++ /dev/null @@ -1,73 +0,0 @@ -var Immutable = require('immutable'); - -var Promise = require('../utils/promise'); -var listDepsForBook = require('./listDepsForBook'); -var findForBook = require('./findForBook'); -var loadPlugin = require('./loadPlugin'); - - -/** - * Load all plugins in a book - * - * @param {Book} - * @return {Promise<Map<String:Plugin>} - */ -function loadForBook(book) { - var logger = book.getLogger(); - - // List the dependencies - var requirements = listDepsForBook(book); - - // List all plugins installed in the book - return findForBook(book) - .then(function(installedMap) { - var missing = []; - var plugins = requirements.reduce(function(result, dep) { - var name = dep.getName(); - var installed = installedMap.get(name); - - if (installed) { - var deps = installedMap - .filter(function(plugin) { - return plugin.getParent() === name; - }) - .toArray(); - - result = result.concat(deps); - result.push(installed); - } else { - missing.push(name); - } - - return result; - }, []); - - // Convert plugins list to a map - plugins = Immutable.List(plugins) - .map(function(plugin) { - return [ - plugin.getName(), - plugin - ]; - }); - plugins = Immutable.OrderedMap(plugins); - - // Log state - logger.info.ln(installedMap.size + ' plugins are installed'); - if (requirements.size != installedMap.size) { - logger.info.ln(requirements.size + ' explicitly listed'); - } - - // Verify that all plugins are present - if (missing.length > 0) { - throw new Error('Couldn\'t locate plugins "' + missing.join(', ') + '", Run \'gitbook install\' to install plugins from registry.'); - } - - return Promise.map(plugins, function(plugin) { - return loadPlugin(book, plugin); - }); - }); -} - - -module.exports = loadForBook; diff --git a/lib/plugins/loadPlugin.js b/lib/plugins/loadPlugin.js deleted file mode 100644 index 9ed83a1..0000000 --- a/lib/plugins/loadPlugin.js +++ /dev/null @@ -1,89 +0,0 @@ -var path = require('path'); -var resolve = require('resolve'); -var Immutable = require('immutable'); - -var Promise = require('../utils/promise'); -var error = require('../utils/error'); -var timing = require('../utils/timing'); - -var validatePlugin = require('./validatePlugin'); - -// Return true if an error is a "module not found" -// Wait on https://github.com/substack/node-resolve/pull/81 to be merged -function isModuleNotFound(err) { - return err.code == 'MODULE_NOT_FOUND' || err.message.indexOf('Cannot find module') >= 0; -} - -/** - Load a plugin in a book - - @param {Book} book - @param {Plugin} plugin - @param {String} pkgPath (optional) - @return {Promise<Plugin>} -*/ -function loadPlugin(book, plugin) { - var logger = book.getLogger(); - - var name = plugin.getName(); - var pkgPath = plugin.getPath(); - - // Try loading plugins from different location - var p = Promise() - .then(function() { - var packageContent; - var packageMain; - var content; - - // Locate plugin and load package.json - try { - var res = resolve.sync('./package.json', { basedir: pkgPath }); - - pkgPath = path.dirname(res); - packageContent = require(res); - } catch (err) { - if (!isModuleNotFound(err)) throw err; - - packageContent = undefined; - content = undefined; - - return; - } - - // Locate the main package - try { - var indexJs = path.normalize(packageContent.main || 'index.js'); - packageMain = resolve.sync('./' + indexJs, { basedir: pkgPath }); - } catch (err) { - if (!isModuleNotFound(err)) throw err; - packageMain = undefined; - } - - // Load plugin JS content - if (packageMain) { - try { - content = require(packageMain); - } catch(err) { - throw new error.PluginError(err, { - plugin: name - }); - } - } - - // Update plugin - return plugin.merge({ - 'package': Immutable.fromJS(packageContent), - 'content': Immutable.fromJS(content || {}) - }); - }) - - .then(validatePlugin); - - p = timing.measure('plugin.load', p); - - logger.info('loading plugin "' + name + '"... '); - return logger.info.promise(p); -} - - -module.exports = loadPlugin; diff --git a/lib/plugins/locateRootFolder.js b/lib/plugins/locateRootFolder.js deleted file mode 100644 index 1139510..0000000 --- a/lib/plugins/locateRootFolder.js +++ /dev/null @@ -1,22 +0,0 @@ -var path = require('path'); -var resolve = require('resolve'); - -var DEFAULT_PLUGINS = require('../constants/defaultPlugins'); - -/** - * Resolve the root folder containing for node_modules - * since gitbook can be used as a library and dependency can be flattened. - * - * @return {String} folderPath - */ -function locateRootFolder() { - var firstDefaultPlugin = DEFAULT_PLUGINS.first(); - var pluginPath = resolve.sync(firstDefaultPlugin.getNpmID() + '/package.json', { - basedir: __dirname - }); - var nodeModules = path.resolve(pluginPath, '../../..'); - - return nodeModules; -} - -module.exports = locateRootFolder; diff --git a/lib/plugins/resolveVersion.js b/lib/plugins/resolveVersion.js deleted file mode 100644 index 61aef8d..0000000 --- a/lib/plugins/resolveVersion.js +++ /dev/null @@ -1,71 +0,0 @@ -var npm = require('npm'); -var semver = require('semver'); -var Immutable = require('immutable'); - -var Promise = require('../utils/promise'); -var Plugin = require('../models/plugin'); -var gitbook = require('../gitbook'); - -var npmIsReady; - -/** - Initialize and prepare NPM - - @return {Promise} -*/ -function initNPM() { - if (npmIsReady) return npmIsReady; - - npmIsReady = Promise.nfcall(npm.load, { - silent: true, - loglevel: 'silent' - }); - - return npmIsReady; -} - -/** - Resolve a plugin dependency to a version - - @param {PluginDependency} plugin - @return {Promise<String>} -*/ -function resolveVersion(plugin) { - var npmId = Plugin.nameToNpmID(plugin.getName()); - var requiredVersion = plugin.getVersion(); - - if (plugin.isGitDependency()) { - return Promise.resolve(requiredVersion); - } - - return initNPM() - .then(function() { - return Promise.nfcall(npm.commands.view, [npmId + '@' + requiredVersion, 'engines'], true); - }) - .then(function(versions) { - versions = Immutable.Map(versions).entrySeq(); - - var result = versions - .map(function(entry) { - return { - version: entry[0], - gitbook: (entry[1].engines || {}).gitbook - }; - }) - .filter(function(v) { - return v.gitbook && gitbook.satisfies(v.gitbook); - }) - .sort(function(v1, v2) { - return semver.lt(v1.version, v2.version)? 1 : -1; - }) - .get(0); - - if (!result) { - return undefined; - } else { - return result.version; - } - }); -} - -module.exports = resolveVersion; diff --git a/lib/plugins/sortDependencies.js b/lib/plugins/sortDependencies.js deleted file mode 100644 index 7f10095..0000000 --- a/lib/plugins/sortDependencies.js +++ /dev/null @@ -1,34 +0,0 @@ -var Immutable = require('immutable'); - -var THEME_PREFIX = require('../constants/themePrefix'); - -var TYPE_PLUGIN = 'plugin'; -var TYPE_THEME = 'theme'; - - -/** - * Returns the type of a plugin given its name - * @param {Plugin} plugin - * @return {String} - */ -function pluginType(plugin) { - var name = plugin.getName(); - return (name && name.indexOf(THEME_PREFIX) === 0) ? TYPE_THEME : TYPE_PLUGIN; -} - - -/** - * Sort the list of dependencies to match list in book.json - * The themes should always be loaded after the plugins - * - * @param {List<PluginDependency>} deps - * @return {List<PluginDependency>} - */ -function sortDependencies(plugins) { - var byTypes = plugins.groupBy(pluginType); - - return byTypes.get(TYPE_PLUGIN, Immutable.List()) - .concat(byTypes.get(TYPE_THEME, Immutable.List())); -} - -module.exports = sortDependencies;
\ No newline at end of file diff --git a/lib/plugins/toNames.js b/lib/plugins/toNames.js deleted file mode 100644 index ad0dd8f..0000000 --- a/lib/plugins/toNames.js +++ /dev/null @@ -1,16 +0,0 @@ - -/** - * Return list of plugin names. This method is nly used in unit tests. - * - * @param {OrderedMap<String:Plugin} plugins - * @return {Array<String>} - */ -function toNames(plugins) { - return plugins - .map(function(plugin) { - return plugin.getName(); - }) - .toArray(); -} - -module.exports = toNames; diff --git a/lib/plugins/validateConfig.js b/lib/plugins/validateConfig.js deleted file mode 100644 index fab1fef..0000000 --- a/lib/plugins/validateConfig.js +++ /dev/null @@ -1,71 +0,0 @@ -var Immutable = require('immutable'); -var jsonschema = require('jsonschema'); -var jsonSchemaDefaults = require('json-schema-defaults'); - -var Promise = require('../utils/promise'); -var error = require('../utils/error'); -var mergeDefaults = require('../utils/mergeDefaults'); - -/** - Validate one plugin for a book and update book's confiration - - @param {Book} - @param {Plugin} - @return {Book} -*/ -function validatePluginConfig(book, plugin) { - var config = book.getConfig(); - var packageInfos = plugin.getPackage(); - - var configKey = [ - 'pluginsConfig', - plugin.getName() - ].join('.'); - - var pluginConfig = config.getValue(configKey, {}).toJS(); - - var schema = (packageInfos.get('gitbook') || Immutable.Map()).toJS(); - if (!schema) return book; - - // Normalize schema - schema.id = '/' + configKey; - schema.type = 'object'; - - // Validate and throw if invalid - var v = new jsonschema.Validator(); - var result = v.validate(pluginConfig, schema, { - propertyName: configKey - }); - - // Throw error - if (result.errors.length > 0) { - throw new error.ConfigurationError(new Error(result.errors[0].stack)); - } - - // Insert default values - var defaults = jsonSchemaDefaults(schema); - pluginConfig = mergeDefaults(pluginConfig, defaults); - - - // Update configuration - config = config.setValue(configKey, pluginConfig); - - // Return new book - return book.set('config', config); -} - -/** - Validate a book configuration for plugins and - returns an update configuration with default values. - - @param {Book} - @param {OrderedMap<String:Plugin>} - @return {Promise<Book>} -*/ -function validateConfig(book, plugins) { - return Promise.reduce(plugins, function(newBook, plugin) { - return validatePluginConfig(newBook, plugin); - }, book); -} - -module.exports = validateConfig; diff --git a/lib/plugins/validatePlugin.js b/lib/plugins/validatePlugin.js deleted file mode 100644 index 4baa911..0000000 --- a/lib/plugins/validatePlugin.js +++ /dev/null @@ -1,34 +0,0 @@ -var gitbook = require('../gitbook'); - -var Promise = require('../utils/promise'); - -/** - Validate a plugin - - @param {Plugin} - @return {Promise<Plugin>} -*/ -function validatePlugin(plugin) { - var packageInfos = plugin.getPackage(); - - var isValid = ( - plugin.isLoaded() && - packageInfos && - packageInfos.get('name') && - packageInfos.get('engines') && - packageInfos.get('engines').get('gitbook') - ); - - if (!isValid) { - return Promise.reject(new Error('Error loading plugin "' + plugin.getName() + '" at "' + plugin.getPath() + '"')); - } - - var engine = packageInfos.get('engines').get('gitbook'); - if (!gitbook.satisfies(engine)) { - return Promise.reject(new Error('GitBook doesn\'t satisfy the requirements of this plugin: ' + engine)); - } - - return Promise(plugin); -} - -module.exports = validatePlugin; |