| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -54,20 +54,7 @@ function initLogLevel() { | |||
| 54 | 54 | ||
| 55 | 55 | var cli = {}; | |
| 56 | 56 | ||
| 57 | - cli.run = function() { | ||
| 58 | - config.init(); | ||
| 59 | - cache.init(); | ||
| 60 | - | ||
| 61 | - initColor(); | ||
| 62 | - initIcon(); | ||
| 63 | - initLogLevel(); | ||
| 64 | - | ||
| 65 | - Plugin.init() && Plugin.save(); | ||
| 66 | - | ||
| 67 | - process.stdout.on('error', function(e) { | ||
| 68 | - if (e.code === 'EPIPE') process.exit(); | ||
| 69 | - }); | ||
| 70 | - | ||
| 57 | + function runCommand() { | ||
| 71 | 58 | var yargs = require('yargs'); | |
| 72 | 59 | h.width = yargs.terminalWidth(); | |
| 73 | 60 | yargs.commandDir('commands') | |
@@ -78,6 +65,30 @@ cli.run = function() { | |||
| 78 | 65 | .epilog('Seek more help at https://skygragon.github.io/leetcode-cli/commands') | |
| 79 | 66 | .wrap(Math.min(h.width, 120)) | |
| 80 | 67 | .argv; | |
| 68 | + } | ||
| 69 | + | ||
| 70 | + cli.run = function() { | ||
| 71 | + process.stdout.on('error', function(e) { | ||
| 72 | + if (e.code === 'EPIPE') process.exit(); | ||
| 73 | + }); | ||
| 74 | + | ||
| 75 | + config.init(); | ||
| 76 | + cache.init(); | ||
| 77 | + | ||
| 78 | + initColor(); | ||
| 79 | + initIcon(); | ||
| 80 | + initLogLevel(); | ||
| 81 | + | ||
| 82 | + if (Plugin.init()) { | ||
| 83 | + Plugin.save(); | ||
| 84 | + runCommand(); | ||
| 85 | + } else { | ||
| 86 | + Plugin.installMissings(function(e) { | ||
| 87 | + if (e) return log.error(e); | ||
| 88 | + Plugin.init(); | ||
| 89 | + runCommand(); | ||
| 90 | + }); | ||
| 91 | + } | ||
| 81 | 92 | }; | |
| 82 | 93 | ||
| 83 | 94 | module.exports = cli; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,7 +6,6 @@ var chalk = require('../chalk'); | |||
| 6 | 6 | var config = require('../config'); | |
| 7 | 7 | var log = require('../log'); | |
| 8 | 8 | var Plugin = require('../plugin'); | |
| 9 | - var Queue = require('../queue'); | ||
| 10 | 9 | var session = require('../session'); | |
| 11 | 10 | ||
| 12 | 11 | const cmd = { | |
@@ -74,60 +73,45 @@ function print(plugins) { | |||
| 74 | 73 | Plugin.save(); | |
| 75 | 74 | } | |
| 76 | 75 | ||
| 77 | - function install(plugins) { | ||
| 78 | - function doTask(plugin, queue, cb) { | ||
| 79 | - Plugin.install(plugin.name, function(e, p) { | ||
| 80 | - if (!e) { | ||
| 81 | - p.enable(plugin.enabled); | ||
| 82 | - p.save(); | ||
| 83 | - p.help(); | ||
| 84 | - } | ||
| 85 | - return cb(e); | ||
| 86 | - }); | ||
| 87 | - } | ||
| 88 | - | ||
| 89 | - const q = new Queue(plugins, {}, doTask); | ||
| 90 | - q.run(1, function(e) { | ||
| 91 | - if (e) return log.fail(e); | ||
| 92 | - Plugin.init(); | ||
| 93 | - print(); | ||
| 94 | - }); | ||
| 95 | - } | ||
| 96 | - | ||
| 97 | 76 | cmd.handler = function(argv) { | |
| 98 | 77 | session.argv = argv; | |
| 99 | 78 | ||
| 100 | 79 | let plugins = Plugin.plugins; | |
| 101 | 80 | const name = argv.name; | |
| 102 | 81 | ||
| 103 | 82 | if (argv.install) { | |
| 83 | + const cb = function(e) { | ||
| 84 | + if (e) return log.error(e); | ||
| 85 | + Plugin.init(); | ||
| 86 | + print(); | ||
| 87 | + }; | ||
| 88 | + | ||
| 104 | 89 | if (name) { | |
| 105 | - install([new Plugin(-1, name, 'missing')]); | ||
| 90 | + Plugin.install(name, cb); | ||
| 106 | 91 | } else { | |
| 107 | - plugins = plugins.filter(x => x.missing); | ||
| 108 | - install(plugins); | ||
| 92 | + Plugin.installMissings(cb); | ||
| 109 | 93 | } | |
| 110 | 94 | return; | |
| 111 | 95 | } | |
| 112 | 96 | ||
| 113 | 97 | if (name) plugins = plugins.filter(x => x.name === name); | |
| 114 | 98 | if (plugins.length === 0) return log.error('Plugin not found!'); | |
| 115 | 99 | ||
| 116 | - const plugin = plugins[0]; | ||
| 117 | - if (plugin.missing && (argv.enable || argv.disable)) | ||
| 100 | + const p = plugins[0]; | ||
| 101 | + if (p.missing && (argv.enable || argv.disable)) | ||
| 118 | 102 | return log.error('Plugin missing, install it first'); | |
| 119 | 103 | ||
| 120 | 104 | if (argv.enable) { | |
| 121 | - plugin.enable(true); | ||
| 122 | - plugin.save(); | ||
| 105 | + p.enable(true); | ||
| 106 | + p.save(); | ||
| 123 | 107 | print(); | |
| 124 | 108 | } else if (argv.disable) { | |
| 125 | - plugin.enable(false); | ||
| 126 | - plugin.save(); | ||
| 109 | + p.enable(false); | ||
| 110 | + p.save(); | ||
| 127 | 111 | print(); | |
| 128 | 112 | } else if (argv.delete) { | |
| 129 | - plugin.delete(); | ||
| 130 | - plugin.save(); | ||
| 113 | + p.delete(); | ||
| 114 | + p.save(); | ||
| 131 | 115 | Plugin.init(); | |
| 132 | 116 | print(); | |
| 133 | 117 | } else if (argv.config) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,6 +10,7 @@ var h = require('./helper'); | |||
| 10 | 10 | var cache = require('./cache'); | |
| 11 | 11 | var config = require('./config'); | |
| 12 | 12 | var log = require('./log'); | |
| 13 | + var Queue = require('./queue'); | ||
| 13 | 14 | ||
| 14 | 15 | function Plugin(id, name, ver, desc, deps) { | |
| 15 | 16 | this.id = id; | |
@@ -171,16 +172,36 @@ Plugin.copy = function(src, cb) { | |||
| 171 | 172 | ||
| 172 | 173 | Plugin.install = function(name, cb) { | |
| 173 | 174 | Plugin.copy(name, function(e, fullpath) { | |
| 174 | - if (e) return log.error(e); | ||
| 175 | + if (e) return cb(e); | ||
| 175 | 176 | log.debug('copied to ' + fullpath); | |
| 176 | 177 | ||
| 177 | - const plugin = require(fullpath); | ||
| 178 | - plugin.install(function() { | ||
| 179 | - return cb(null, plugin); | ||
| 178 | + const p = require(fullpath); | ||
| 179 | + p.install(function() { | ||
| 180 | + return cb(null, p); | ||
| 180 | 181 | }); | |
| 181 | 182 | }); | |
| 182 | 183 | }; | |
| 183 | 184 | ||
| 185 | + Plugin.installMissings = function(cb) { | ||
| 186 | + function doTask(plugin, queue, cb) { | ||
| 187 | + Plugin.install(plugin.name, function(e, p) { | ||
| 188 | + if (!e) { | ||
| 189 | + p.enable(plugin.enabled); | ||
| 190 | + p.save(); | ||
| 191 | + p.help(); | ||
| 192 | + } | ||
| 193 | + return cb(e, p); | ||
| 194 | + }); | ||
| 195 | + } | ||
| 196 | + | ||
| 197 | + const plugins = Plugin.plugins.filter(x => x.missing); | ||
| 198 | + if (plugins.length === 0) return cb(); | ||
| 199 | + | ||
| 200 | + log.warn('Installing missing plugins, might take a while ...'); | ||
| 201 | + const q = new Queue(plugins, {}, doTask); | ||
| 202 | + q.run(1, cb); | ||
| 203 | + }; | ||
| 204 | + | ||
| 184 | 205 | Plugin.save = function() { | |
| 185 | 206 | for (let p of this.plugins) p.save(); | |
| 186 | 207 | }; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments