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

Sign up
Appearance settings

Commit 3414013

Browse files
Refactor dir functions in helper.
Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent 08d0a72 commit 3414013

File tree

9 files changed

+43
-33
lines changed

9 files changed

+43
-33
lines changed

‎lib/chalk.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ chalk.wrap = function(pre, post) {
4747

4848
chalk.init = function() {
4949
var h = require('./helper');
50-
_.each(h.getDirData(['colors']), function(f) {
50+
_.each(h.getCodeDirData('colors'), function(f) {
5151
chalk.themes[f.name] = _.mapObject(f.data, function(v, k) {
5252
return chalk.use256 ? style.color.ansi256.hex(v) : style.color.ansi.hex(v);
5353
});

‎lib/commands/plugin.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,19 @@ cmd.handler = function(argv) {
6565
if (plugins.length === 0) return log.error('Plugin not found!');
6666

6767
var plugin = plugins[0];
68-
var oldname = Plugin.fullpath(plugin.file);
68+
var oldname = h.getPluginFile(plugin.file);
6969
var newname;
7070

7171
if (argv.enable) {
7272
if (plugin.file[0] !== '.') return;
73-
newname = Plugin.fullpath(plugin.file.substr(1));
73+
newname = h.getPluginFile(plugin.file.substr(1));
7474

7575
fs.rename(oldname, newname, function(e) {
7676
if (e) log.error(e.message);
7777
});
7878
} else if (argv.disable) {
7979
if (plugin.file[0] === '.') return;
80-
newname = Plugin.fullpath('.' + plugin.file);
80+
newname = h.getPluginFile('.' + plugin.file);
8181

8282
fs.rename(oldname, newname, function(e) {
8383
if (e) log.error(e.message);

‎lib/core.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ core.exportProblem = function(problem, opts) {
6464
input.desc = wrap(desc).split('\n');
6565
}
6666

67-
var tplfile = path.resolve(__dirname,'../templates/'+ opts.tpl + '.tpl');
67+
var tplfile = path.join(h.getCodeDir('templates'), opts.tpl + '.tpl');
6868
var output = _.template(h.getFileData(tplfile))(input);
6969

7070
if (h.isWindows()) {

‎lib/helper.js‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,8 @@ h.mkdir = function(fullpath) {
150150
mkdirp.sync(fullpath);
151151
};
152152

153-
h.getDirData = function(paths) {
154-
paths.unshift('..');
155-
paths.unshift(__dirname);
156-
var dir = path.join.apply(path, paths);
157-
153+
h.getCodeDirData = function(dir) {
154+
dir = h.getCodeDir(dir);
158155
return _.map(fs.readdirSync(dir), function(file) {
159156
var fullpath = path.join(dir, file);
160157
var ext = path.extname(file);
@@ -163,14 +160,8 @@ h.getDirData = function(paths) {
163160
var data = null;
164161

165162
switch (ext) {
166-
case '.js':
167-
data = require(fullpath);
168-
break;
169-
case '.json':
170-
data = JSON.parse(h.getFileData(fullpath));
171-
break;
172-
default:
173-
break;
163+
case '.js': data = require(fullpath); break;
164+
case '.json': data = JSON.parse(h.getFileData(fullpath)); break;
174165
}
175166
return {name: name, data: data, file: file};
176167
});
@@ -192,6 +183,10 @@ h.getCacheDir = function() {
192183
return path.join(this.getHomeDir(), '.lc');
193184
};
194185

186+
h.getCodeDir = function(dir) {
187+
return path.join(__dirname, '..', dir || '');
188+
};
189+
195190
h.getCacheFile = function(k) {
196191
return path.join(this.getCacheDir(), k + '.json');
197192
};
@@ -200,6 +195,10 @@ h.getConfigFile = function() {
200195
return path.join(this.getHomeDir(), '.lcconfig');
201196
};
202197

198+
h.getPluginFile = function(name) {
199+
return path.join(this.getCodeDir('lib/plugins'), path.basename(name));
200+
};
201+
203202
h.readStdin = function(cb) {
204203
var stdin = process.stdin;
205204
var bufs = [];

‎lib/icon.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ icons.setTheme = function(name) {
2020
};
2121

2222
icons.init = function() {
23-
_.each(h.getDirData(['icons']), function(f) {
23+
_.each(h.getCodeDirData('icons'), function(f) {
2424
icons.themes[f.name] = f.data;
2525
});
2626
};

‎lib/plugin.js‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Plugin.prototype.install = function(cb) {
3838
var cmd = 'npm install --save ' + this.deps.join(' ');
3939
log.debug(cmd);
4040
var spin = h.spin(cmd);
41-
require('child_process').exec(cmd, {cwd: Plugin.root}, function() {
41+
require('child_process').exec(cmd, {cwd: h.getCodeDir()}, function() {
4242
spin.stop();
4343
return cb();
4444
});
@@ -49,11 +49,8 @@ Plugin.prototype.help = function() {};
4949
Plugin.plugins = [];
5050

5151
Plugin.init = function(head) {
52-
Plugin.dir = path.resolve(__dirname, 'plugins/');
53-
Plugin.root = path.resolve(__dirname, '../');
54-
5552
var plugins = [];
56-
h.getDirData(['lib','plugins']).forEach(function(f) {
53+
h.getCodeDirData('lib/plugins').forEach(function(f) {
5754
var p = f.data;
5855
if (!p) return;
5956

@@ -91,7 +88,7 @@ Plugin.copy = function(src, cb) {
9188
if (path.extname(src) !== '.js') {
9289
src = config.sys.urls.plugin.replace('$name', src);
9390
}
94-
var dst = Plugin.fullpath(src);
91+
var dst = h.getPluginFile(src);
9592

9693
var srcstream = src.startsWith('https://') ? request(src) : fs.createReadStream(src);
9794
srcstream.on('response', function(resp) {
@@ -127,8 +124,4 @@ Plugin.install = function(name, cb) {
127124
});
128125
};
129126

130-
Plugin.fullpath = function(file) {
131-
return path.join(Plugin.dir, path.basename(file));
132-
};
133-
134127
module.exports = Plugin;

‎test/test_helper.js‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var path = require('path');
2+
13
var assert = require('chai').assert;
24

35
var chalk = require('../lib/chalk');
@@ -169,6 +171,8 @@ describe('helper', function() {
169171
}); // #langToCommentStyle
170172

171173
describe('#dirAndFiles', function() {
174+
var root = path.join(__dirname, '..');
175+
172176
it('should ok', function() {
173177
process.env.HOME = '/home/skygragon';
174178

@@ -183,13 +187,27 @@ describe('helper', function() {
183187
assert.equal(h.getHomeDir(), 'C:\\Users\\skygragon');
184188
});
185189

186-
it('should getDirData ok', function() {
187-
var files = h.getDirData(['lib', 'plugins']);
190+
it('should getCodeDir ok', function() {
191+
assert.equal(h.getCodeDir(), root);
192+
assert.equal(h.getCodeDir('.'), root);
193+
assert.equal(h.getCodeDir('icons'), path.join(root, 'icons'));
194+
assert.equal(h.getCodeDir('lib/plugins'), path.join(root, 'lib', 'plugins'));
195+
});
196+
197+
it('should getCodeDirData ok', function() {
198+
var files = h.getCodeDirData('lib/plugins');
188199
assert.equal(files.length, 3);
189200
assert.equal(files[0].name, 'cache');
190201
assert.equal(files[1].name, 'leetcode');
191202
assert.equal(files[2].name, 'retry');
192203
});
204+
205+
it('should getPluginFile ok', function() {
206+
var expect = path.join(root, 'lib/plugins/cache.js');
207+
assert.equal(h.getPluginFile('cache.js'), expect);
208+
assert.equal(h.getPluginFile('./cache.js'), expect);
209+
assert.equal(h.getPluginFile('https://github.com/skygragon/cache.js'), expect);
210+
});
193211
}); // #dirAndFiles
194212

195213
describe('#getSetCookieValue', function() {

‎test/test_icon.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('icon', function() {
77
var icon = null;
88

99
before(function() {
10-
h.getDirData = function() {
10+
h.getCodeDirData = function() {
1111
return [
1212
{
1313
name: 'word',

‎test/test_plugin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('plugin', function() {
2020
retry.init = noop;
2121
core.init = noop;
2222

23-
h.getDirData = function() {
23+
h.getCodeDirData = function() {
2424
return [
2525
{name: 'cache', data: cache},
2626
{name: 'leetcode', data: leetcode},

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /