Skip to content

Commit 14fb6e3

Browse files
committed
Refactor .lc folder.
* move everything to .lc * use .json ext for all files Signed-off-by: Eric Wang <[email protected]>
1 parent fd9562b commit 14fb6e3

File tree

7 files changed

+26
-33
lines changed

7 files changed

+26
-33
lines changed

Diff for: lib/cache.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ cache.get = function(k) {
1818
};
1919

2020
cache.set = function(k, v) {
21-
h.mkdir(h.getCacheDir());
2221
var fullpath = h.getCacheFile(k);
2322
fs.writeFileSync(fullpath, JSON.stringify(v));
2423
return true;
@@ -33,10 +32,7 @@ cache.del = function(k) {
3332
};
3433

3534
cache.list = function() {
36-
var dir = h.getCacheDir();
37-
if (!fs.existsSync(dir)) return [];
38-
39-
return fs.readdirSync(dir)
35+
return fs.readdirSync(h.getCacheDir())
4036
.filter(function(filename) {
4137
return path.extname(filename) === '.json';
4238
})

Diff for: lib/cli.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var _ = require('underscore');
22

33
var chalk = require('./chalk');
4+
var cache = require('./cache');
45
var config = require('./config');
56
var core = require('./core');
67
var h = require('./helper');
@@ -57,6 +58,7 @@ var cli = {};
5758

5859
cli.run = function() {
5960
config.init();
61+
cache.init();
6062

6163
initColor();
6264
initIcon();

Diff for: lib/commands/cache.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ cmd.handler = function(argv) {
3838
});
3939

4040
if (argv.delete) {
41-
caches.forEach(function(f) {
42-
if (f.name === '.user') return;
43-
cache.del(f.name);
44-
});
41+
caches.forEach(function(f) { cache.del(f.name); });
4542
} else {
4643
_.sortBy(caches, function(f) {
4744
var x = parseInt(f.name.split('.')[0], 10);

Diff for: lib/helper.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var LANGS = [
5050
var h = {};
5151

5252
h.KEYS = {
53-
user: '.user',
53+
user: '../user',
5454
problems: 'problems',
5555
problem: function(p) { return p.id + '.' + p.slug + '.' + p.category; }
5656
};
@@ -175,12 +175,16 @@ h.getFileData = function(fullpath) {
175175
return fs.existsSync(fullpath) ? fs.readFileSync(fullpath).toString() : null;
176176
};
177177

178-
h.getHomeDir = function() {
178+
h.getUserHomeDir = function() {
179179
return process.env.HOME || process.env.USERPROFILE;
180180
};
181181

182+
h.getHomeDir = function() {
183+
return path.join(this.getUserHomeDir(), '.lc');
184+
};
185+
182186
h.getCacheDir = function() {
183-
return path.join(this.getHomeDir(), '.lc');
187+
return path.join(this.getHomeDir(), 'cache');
184188
};
185189

186190
h.getCodeDir = function(dir) {
@@ -192,7 +196,7 @@ h.getCacheFile = function(k) {
192196
};
193197

194198
h.getConfigFile = function() {
195-
return path.join(this.getHomeDir(), '.lcconfig');
199+
return path.join(this.getHomeDir(), 'config.json');
196200
};
197201

198202
h.getPluginFile = function(name) {

Diff for: test/plugins/test_cache.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ describe('plugin:cache', function() {
2929
config.init();
3030
plugin.init();
3131

32-
h.getHomeDir = function() {
32+
h.getCacheDir = function() {
3333
return HOME;
3434
};
35-
3635
cache.__set__('h', h);
36+
cache.init();
37+
3738
session.__set__('cache', cache);
3839
plugin.__set__('cache', cache);
3940
plugin.__set__('session', session);
@@ -179,7 +180,7 @@ describe('plugin:cache', function() {
179180
it('should login ok', function(done) {
180181
config.autologin.enable = true;
181182
// before login
182-
cache.del('.user');
183+
cache.del(h.KEYS.user);
183184
assert.equal(session.getUser(), null);
184185
assert.equal(session.isLogin(), false);
185186

@@ -200,7 +201,7 @@ describe('plugin:cache', function() {
200201

201202
it('should login ok w/ auto login', function(done) {
202203
config.autologin.enable = false;
203-
cache.del('.user');
204+
cache.del(h.KEYS.user);
204205

205206
NEXT.login = function(user, cb) {
206207
return cb(null, user);
@@ -228,7 +229,7 @@ describe('plugin:cache', function() {
228229

229230
it('should logout ok', function(done) {
230231
// before logout
231-
cache.set('.user', USER);
232+
cache.set(h.KEYS.user, USER);
232233
assert.deepEqual(session.getUser(), USER);
233234
assert.equal(session.isLogin(), true);
234235

Diff for: test/test_cache.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ describe('cache', function() {
1818
return cachedir;
1919
};
2020
cache.__set__('h', h);
21+
cache.init();
2122
});
2223

2324
it('should ok when not cached', function() {
@@ -48,13 +49,4 @@ describe('cache', function() {
4849
assert.equal(items[0].name, k);
4950
assert.equal(items[0].size, JSON.stringify(v).length);
5051
});
51-
52-
it('should list ok when cache dir not exist', function() {
53-
h.getCacheDir = function() {
54-
return '/not-exist-dir';
55-
};
56-
57-
var items = cache.list();
58-
assert.equal(items.length, 0);
59-
});
6052
});

Diff for: test/test_helper.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,16 @@ describe('helper', function() {
176176
it('should ok', function() {
177177
process.env.HOME = '/home/skygragon';
178178

179-
assert.equal(h.getHomeDir(), '/home/skygragon');
180-
assert.equal(h.getCacheDir(), '/home/skygragon/.lc');
181-
assert.equal(h.getCacheFile('xxx'), '/home/skygragon/.lc/xxx.json');
182-
assert.equal(h.getConfigFile(), '/home/skygragon/.lcconfig');
183-
assert.equal(h.getFilename('/home/skygragon/.lc/xxx.json'), 'xxx');
179+
assert.equal(h.getUserHomeDir(), '/home/skygragon');
180+
assert.equal(h.getHomeDir(), '/home/skygragon/.lc');
181+
assert.equal(h.getCacheDir(), '/home/skygragon/.lc/cache');
182+
assert.equal(h.getCacheFile('xxx'), '/home/skygragon/.lc/cache/xxx.json');
183+
assert.equal(h.getConfigFile(), '/home/skygragon/.lc/config.json');
184+
assert.equal(h.getFilename('/home/skygragon/.lc/cache/xxx.json'), 'xxx');
184185

185186
process.env.HOME = '';
186187
process.env.USERPROFILE = 'C:\\Users\\skygragon';
187-
assert.equal(h.getHomeDir(), 'C:\\Users\\skygragon');
188+
assert.equal(h.getUserHomeDir(), 'C:\\Users\\skygragon');
188189
});
189190

190191
it('should getCodeDir ok', function() {

0 commit comments

Comments
 (0)