|
| 1 | +const path = require('path'); |
| 2 | +const { spawn } = require('child_process'); |
| 3 | +const rimraf = require('rimraf'); |
| 4 | +const npmName = require('npm-name'); |
| 5 | +const { |
| 6 | + ERR_MODULE_NOT_FOUND, |
| 7 | + ERR_MODULE_INSTALLED, |
| 8 | + ERR_MODULE_NOT_INSTALLED, |
| 9 | + ERR_THEME_ALREADY_ACTIVE, |
| 10 | +} = require('./errors'); |
| 11 | +const Conf = require('../utils/conf'); |
| 12 | +const { downloadPackage } = require('../utils/download'); |
| 13 | +const { PLUGIN_PATH } = require('../utils/paths'); |
| 14 | + |
| 15 | +const config = new Conf(); |
| 16 | + |
| 17 | +/** |
| 18 | + * Checks if it exists on npm |
| 19 | + * |
| 20 | + * @param {String} plugin - The name of the plugin |
| 21 | + * @return {Promise} |
| 22 | + */ |
| 23 | +const checkOnNpm = plugin => new Promise(resolve => { |
| 24 | + npmName(plugin).then(available => { |
| 25 | + if (available) { |
| 26 | + throw new Error(ERR_MODULE_NOT_FOUND); |
| 27 | + } |
| 28 | + resolve(); |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +/** |
| 33 | + * Installs a plugin |
| 34 | + * |
| 35 | + * @param {String} plugin - The name of the plugin |
| 36 | + * @return {Promise} |
| 37 | + */ |
| 38 | +const install = plugin => new Promise(resolve => { |
| 39 | + const plugins = config.get('plugins') || []; |
| 40 | + |
| 41 | + if (plugins.indexOf(plugin) > -1) { |
| 42 | + throw new Error(ERR_MODULE_INSTALLED); |
| 43 | + } |
| 44 | + |
| 45 | + checkOnNpm(plugin).then(() => { |
| 46 | + // download, install, and update configs |
| 47 | + downloadPackage(plugin).then(outputDir => { |
| 48 | + const installProcess = spawn('npm', ['install', '--prefix', outputDir]); |
| 49 | + installProcess.on('close', (code) => { |
| 50 | + if (!code) { |
| 51 | + plugins.push(plugin); |
| 52 | + config.set('plugins', plugins); |
| 53 | + resolve(); |
| 54 | + } |
| 55 | + }); |
| 56 | + }); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +/** |
| 61 | + * Uninstalls a plugin |
| 62 | + * |
| 63 | + * @param {String} plugin - The name of the plugin |
| 64 | + * @return {Promise} |
| 65 | + */ |
| 66 | +const uninstall = plugin => new Promise(resolve => { |
| 67 | + const plugins = config.get('plugins') || []; |
| 68 | + |
| 69 | + if (!plugins || !plugins.length) { |
| 70 | + throw new Error(ERR_MODULE_NOT_INSTALLED); |
| 71 | + } |
| 72 | + |
| 73 | + if (plugins.indexOf(plugin) === -1) { |
| 74 | + throw new Error(ERR_MODULE_NOT_INSTALLED); |
| 75 | + } |
| 76 | + |
| 77 | + // removes the directory |
| 78 | + const pluginDir = path.resolve(PLUGIN_PATH, plugin); |
| 79 | + rimraf(pluginDir, err => { |
| 80 | + if (err) { |
| 81 | + throw new Error(err); |
| 82 | + } |
| 83 | + plugins.splice(plugins.indexOf(plugin), 1); |
| 84 | + config.set('plugins', plugins); |
| 85 | + resolve(); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +/** |
| 90 | + * Switches your current theme |
| 91 | + * |
| 92 | + * @param {String} theme - The name of the theme |
| 93 | + * @return {Promise} |
| 94 | + */ |
| 95 | +const setTheme = theme => new Promise(resolve => { |
| 96 | + const currentTheme = config.get('theme'); |
| 97 | + const plugins = config.get('plugins') || []; |
| 98 | + |
| 99 | + if (currentTheme === theme) { |
| 100 | + throw new Error(ERR_THEME_ALREADY_ACTIVE); |
| 101 | + } |
| 102 | + |
| 103 | + if (!plugins || !plugins.length) { |
| 104 | + throw new Error(ERR_MODULE_NOT_INSTALLED); |
| 105 | + } |
| 106 | + |
| 107 | + if (plugins.indexOf(theme) === -1) { |
| 108 | + throw new Error(ERR_MODULE_NOT_INSTALLED); |
| 109 | + } |
| 110 | + |
| 111 | + config.set('theme', theme); |
| 112 | + |
| 113 | + resolve(); |
| 114 | +}); |
| 115 | + |
| 116 | +module.exports = { |
| 117 | + checkOnNpm, |
| 118 | + install, |
| 119 | + uninstall, |
| 120 | + setTheme, |
| 121 | +}; |
0 commit comments