Skip to content

Commit b0d8189

Browse files
committed
Refactor: extract into getDirData()
Signed-off-by: Eric Wang <[email protected]>
1 parent 4e1469c commit b0d8189

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

lib/chalk.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,9 @@ chalk.wrap = function(pre, post) {
4646
};
4747

4848
chalk.init = function() {
49-
var fs = require('fs');
50-
var path = require('path');
51-
52-
var dir = path.join(__dirname, '..', 'colors');
53-
_.each(fs.readdirSync(dir), function(f) {
54-
var theme = JSON.parse(fs.readFileSync(path.join(dir, f)));
55-
chalk.themes[path.basename(f, '.json')] = _.mapObject(theme, function(v, k) {
49+
var h = require('./helper');
50+
_.each(h.getDirData('colors'), function(f) {
51+
chalk.themes[f.name] = _.mapObject(f.data, function(v, k) {
5652
return chalk.use256 ? style.color.ansi256.hex(v) : style.color.ansi.hex(v);
5753
});
5854
});

lib/helper.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var fs = require('fs');
22
var path = require('path');
33

4+
var _ = require('underscore');
5+
46
var h = {};
57

68
h.prettyState = function(state) {
@@ -100,8 +102,18 @@ h.langToCommentStyle = function(lang) {
100102
}
101103
};
102104

103-
h.getFileData = function(path) {
104-
return fs.existsSync(path) ? fs.readFileSync(path).toString() : null;
105+
h.getFileData = function(p) {
106+
return fs.existsSync(p) ? fs.readFileSync(p).toString() : null;
107+
};
108+
109+
h.getDirData = function(p) {
110+
var dir = path.join(__dirname, '..', p);
111+
return _.map(fs.readdirSync(dir), function(f) {
112+
return {
113+
name: path.basename(f, '.json'),
114+
data: JSON.parse(fs.readFileSync(path.join(dir, f)))
115+
};
116+
});
105117
};
106118

107119
h.getFilename = function(fullpath) {

0 commit comments

Comments
 (0)