8000 [plugin] enable/disable wont touch plugins js. · EvsChen/leetcode-cli@a37018d · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a37018d

Browse files
committed
[plugin] enable/disable wont touch plugins js.
Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent 10e8018 commit a37018d

File tree

4 files changed

+39
-69
lines changed

4 files changed

+39
-69
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ jspm_packages
3636
# Optional REPL history
3737
.node_repl_history
3838

39+
dist/
3940
tmp/
4041
*.swp
4142
.DS_Store

lib/commands/plugin.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ function print(plugins) {
7070
log.printf(' %s %-18s %-15s %s',
7171
h.prettyText('', p.enabled && !p.missing),
7272
p.name, p.ver, p.desc);
73-
Plugin.save();
7473
}
7574

7675
cmd.handler = function(argv) {
@@ -83,6 +82,7 @@ cmd.handler = function(argv) {
8382
const cb = function(e) {
8483
if (e) return log.error(e);
8584
Plugin.init();
85+
Plugin.save();
8686
print();
8787
};
8888

@@ -102,11 +102,11 @@ cmd.handler = function(argv) {
102102
return log.error('Plugin missing, install it first');
103103

104104
if (argv.enable) {
105-
p.enable(true);
105+
p.enabled = true;
106106
p.save();
107107
print();
108108
} else if (argv.disable) {
109-
p.enable(false);
109+
p.enabled = false;
110110
p.save();
111111
print();
112112
} else if (argv.delete) {

lib/plugin.js

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,6 @@ Plugin.prototype.setNext = function(next) {
4040
this.next = next;
4141
};
4242

43-
Plugin.prototype.setFile = function(f) {
44-
this.file = f;
45-
this.enabled = (this.file[0] !== '.');
46-
};
47-
48-
Plugin.prototype.enable = function(enabled) {
49-
if (this.enabled === enabled) return;
50-
const newfile = enabled ? this.file.substr(1) : '.' + this.file;
51-
try {
52-
file.mv(file.pluginFile(this.file), file.pluginFile(newfile));
53-
} catch(e) {
54-
log.error(e.message);
55-
}
56-
this.setFile(newfile);
57-
};
58-
5943
Plugin.prototype.delete = function() {
6044
if (!this.missing) {
6145
try {
@@ -69,13 +53,13 @@ Plugin.prototype.delete = function() {
6953
};
7054

7155
Plugin.prototype.save = function() {
72-
const data = cache.get(h.KEYS.plugins) || {};
56+
const stats = cache.get(h.KEYS.plugins) || {};
7357

74-
if (this.deleted) delete data[this.name];
58+
if (this.deleted) delete stats[this.name];
7559
else if (this.missing) return;
76-
else data[this.name] = this.enabled;
60+
else stats[this.name] = this.enabled;
7761

78-
cache.set(h.KEYS.plugins, data);
62+
cache.set(h.KEYS.plugins, stats);
7963
};
8064

8165
Plugin.prototype.install = function(cb) {
@@ -84,9 +68,9 @@ Plugin.prototype.install = function(cb) {
8468
const cmd = 'npm install --save ' + this.deps.join(' ');
8569
log.debug(cmd);
8670
const spin = h.spin(cmd);
87-
cp.exec(cmd, {cwd: file.codeDir()}, function() {
71+
cp.exec(cmd, {cwd: file.codeDir()}, function(e) {
8872
spin.stop();
89-
return cb();
73+
return cb(e);
9074
});
9175
};
9276

@@ -98,14 +82,23 @@ Plugin.init = function(head) {
9882
log.trace('initializing all plugins');
9983
head = head || require('./core');
10084

85+
const stats = cache.get(h.KEYS.plugins) || {};
86+
10187
// 1. check installed plugins
10288
let plugins = [];
10389
for (let f of file.listCodeDir('lib/plugins')) {
10490
const p = f.data;
10591
if (!p) continue;
106-
107-
p.setFile(f.file);
10892
log.trace('found plugin: ' + p.name + '=' + p.ver);
93+
94+
p.file = f.file;
95+
p.enabled = stats[p.name];
96+
// not saved before? enable it by default
97+
if (!(p.name in stats)) {
98+
log.trace('new plugin, enable by default');
99+
p.enabled = true;
100+
}
101+
109102
if (p.enabled) {
110103
p.init();
111104
log.trace('inited plugin: ' + p.name);
@@ -129,11 +122,10 @@ Plugin.init = function(head) {
129122

130123
// 2. check saved plugins
131124
const missings = [];
132-
const data = cache.get(h.KEYS.plugins) || {};
133-
for (let k of _.keys(data)) {
125+
for (let k of _.keys(stats)) {
134126
if (plugins.find(x => x.name === k)) continue;
135127
const p = new Plugin(-1, k, 'missing');
136-
p.enabled = data[k];
128+
p.enabled = stats[k];
137129
missings.push(p);
138130
}
139131
log.trace('missing plugins: ' + missings.length);
@@ -177,6 +169,7 @@ Plugin.install = function(name, cb) {
177169
log.debug('copied to ' + fullpath);
178170

179171
const p = require(fullpath);
172+
p.file = path.basename(fullpath);
180173
p.install(function() {
181174
return cb(null, p);
182175
});
@@ -187,19 +180,19 @@ Plugin.installMissings = function(cb) {
187180
function doTask(plugin, queue, cb) {
188181
Plugin.install(plugin.name, function(e, p) {
189182
if (!e) {
190-
p.enable(plugin.enabled);
183+
p.enabled = plugin.enabled;
191184
p.save();
192185
p.help();
193186
}
194187
return cb(e, p);
195188
});
196189
}
197190

198-
const plugins = Plugin.plugins.filter(x => x.missing);
199-
if (plugins.length === 0) return cb();
191+
const missings = Plugin.plugins.filter(x => x.missing);
192+
if (missings.length === 0) return cb();
200193

201194
log.warn('Installing missing plugins, might take a while ...');
202-
const q = new Queue(plugins, {}, doTask);
195+
const q = new Queue(missings, {}, doTask);
203196
q.run(1, cb);
204197
};
205198

test/test_plugin.js

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,35 @@ describe('plugin', function() {
3535
});
3636

3737
describe('#Plugin.init', function() {
38-
const p1 = new Plugin(0, 'Leetcode', '2.0');
39-
const p2 = new Plugin(1, 'Cache', '1.0');
40-
const p3 = new Plugin(2, 'Retry', '3.0');
41-
const p4 = new Plugin(3, 'Core', '4.0');
38+
const p1 = new Plugin(0, 'leetcode', '2.0');
39+
const p2 = new Plugin(1, 'cache', '1.0');
40+
const p3 = new Plugin(2, 'retry', '3.0');
41+
const p4 = new Plugin(3, 'core', '4.0');
4242

4343
before(function() {
4444
p1.init = p2.init = p3.init = p4.init = NOOP;
4545
file.listCodeDir = function() {
4646
return [
4747
{name: 'cache', data: p2, file: 'cache.js'},
48-
{name: 'leetcode', data: p1, file: '.leetcode.js'}, // disabled
48+
{name: 'leetcode', data: p1, file: 'leetcode.js'},
4949
{name: 'retry', data: p3, file: 'retry.js'},
5050
{name: 'bad', data: null}
5151
];
5252
};
5353
});
5454

5555
it('should init ok', function() {
56+
cache.get = () => {
57+
return {cache: true, leetcode: false, retry: true};
58+
};
5659
assert.deepEqual(Plugin.plugins, []);
5760

5861
const res = Plugin.init(p4);
5962
assert.equal(res, true);
6063
assert.deepEqual(Plugin.plugins.length, 3);
6164

6265
const names = Plugin.plugins.map(p => p.name);
63-
assert.deepEqual(names, ['Retry', 'Cache', 'Leetcode']);
66+
assert.deepEqual(names, ['retry', 'cache', 'leetcode']);
6467

6568
assert.equal(p4.next, p3);
6669
assert.equal(p3.next, p2);
@@ -70,15 +73,15 @@ describe('plugin', function() {
7073

7174
it('should find missing ok', function() {
7275
cache.get = () => {
73-
return {company: true, solution: true};
76+
return {company: true, leetcode: false, solution: true};
7477
};
7578

7679
const res = Plugin.init(p4);
7780
assert.equal(res, false);
7881
assert.deepEqual(Plugin.plugins.length, 5);
7982

8083
const names = Plugin.plugins.map(p => p.name);
81-
assert.deepEqual(names, ['Retry', 'Cache', 'Leetcode', 'company', 'solution']);
84+
assert.deepEqual(names, ['retry', 'cache', 'leetcode', 'company', 'solution']);
8285

8386
assert.equal(p4.next, p3);
8487
assert.equal(p3.next, p2);
@@ -179,33 +182,6 @@ describe('plugin', function() {
179182
});
180183
}); // #Plugin.installMissings
181184

182-
describe('#enable', function() {
183-
const FILE = path.resolve(th.DIR, 'leetcode.js');
184-
185-
before(function() {
186-
file.pluginFile = () => FILE;
187-
});
188-
189-
it('should ok', function() {
190-
const p = new Plugin BE67 (0, 'Leetcode', '2.0', '');
191-
assert.equal(p.enabled, true);
192-
193-
p.setFile('.leetcode.js');
194-
fs.writeFileSync(FILE, '');
195-
assert.equal(p.enabled, false);
196-
assert.equal(p.file, '.leetcode.js');
197-
p.enable(false);
198-
assert.equal(p.enabled, false);
199-
assert.equal(p.file, '.leetcode.js');
200-
p.enable(true);
201-
assert.equal(p.enabled, true);
202-
assert.equal(p.file, 'leetcode.js');
203-
p.enable(false);
204-
assert.equal(p.enabled, false);
205-
assert.equal(p.file, '.leetcode.js');
206-
});
207-
}); // #enable
208-
209185
describe('#delete', function() {
210186
it('should ok', function() {
211187
file.pluginFile = x => th.DIR + x;

0 commit comments

Comments
 (0)
0