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 61becf9

Browse files
[refactor] extract into file utils
Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent 657c6c5 commit 61becf9

24 files changed

+217
-183
lines changed

‎lib/cache.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,41 @@
22
var fs = require('fs');
33
var path = require('path');
44

5-
var h = require('./helper');
5+
var file = require('./file');
66

77
const cache = {};
88

99
cache.init = function() {
10-
h.mkdir(h.getCacheDir());
10+
file.mkdir(file.cacheDir());
1111
};
1212

1313
cache.get = function(k) {
14-
const fullpath = h.getCacheFile(k);
14+
const fullpath = file.cacheFile(k);
1515
if (!fs.existsSync(fullpath)) return null;
1616

1717
return JSON.parse(fs.readFileSync(fullpath));
1818
};
1919

2020
cache.set = function(k, v) {
21-
const fullpath = h.getCacheFile(k);
21+
const fullpath = file.cacheFile(k);
2222
fs.writeFileSync(fullpath, JSON.stringify(v));
2323
return true;
2424
};
2525

2626
cache.del = function(k) {
27-
const fullpath = h.getCacheFile(k);
27+
const fullpath = file.cacheFile(k);
2828
if (!fs.existsSync(fullpath)) return false;
2929

3030
fs.unlinkSync(fullpath);
3131
return true;
3232
};
3333

3434
cache.list = function() {
35-
return fs.readdirSync(h.getCacheDir())
35+
return file.list(file.cacheDir())
3636
.filter(x => path.extname(x) === '.json')
3737
.map(function(filename) {
3838
const k = path.basename(filename, '.json');
39-
const stat = fs.statSync(h.getCacheFile(k));
39+
const stat = fs.statSync(file.cacheFile(k));
4040
return {
4141
name: k,
4242
size: stat.size,

‎lib/chalk.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var _ = require('underscore');
33
var style = require('ansi-styles');
44
var supportsColor = require('supports-color');
55

6+
var file = require('./file');
7+
68
const chalk = {
79
enabled: supportsColor.stdout,
810
use256: supportsColor.stdout && supportsColor.stdout.has256,
@@ -54,8 +56,7 @@ chalk.wrap = function(pre, post) {
5456
const bgName = x => 'bg' + x[0].toUpperCase() + x.substr(1);
5557

5658
chalk.init = function() {
57-
const h = require('./helper');
58-
for (let f of h.getCodeDirData('colors')) {
59+
for (let f of file.listCodeDir('colors')) {
5960
const theme = {};
6061
const data = _.extendOwn({}, DEFAULT, f.data);
6162
for (let x of _.pairs(data)) {

‎lib/cli.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var chalk = require('./chalk');
55
var cache = require('./cache');
66
var config = require('./config');
77
var h = require('./helper');
8+
var file = require('./file');
89
var icon = require('./icon');
910
var log = require('./log');
1011
var Plugin = require('./plugin');
@@ -53,7 +54,7 @@ function initLogLevel() {
5354
}
5455

5556
function initDir() {
56-
h.mkdir(h.getHomeDir())
57+
file.mkdir(file.homeDir())
5758
}
5859

5960
function initPlugins(cb) {

‎lib/commands/config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
var _ = require('underscore');
33
var nconf = require('nconf');
44

5-
var h = require('../helper');
5+
var file = require('../file');
66
var chalk = require('../chalk');
77
var config = require('../config');
88
var log = require('../log');
@@ -55,12 +55,12 @@ function loadConfig(showall) {
5555
}
5656

5757
function saveConfig() {
58-
require('fs').writeFileSync(h.getConfigFile(), prettyConfig(loadConfig(false)));
58+
require('fs').writeFileSync(file.configFile(), prettyConfig(loadConfig(false)));
5959
}
6060

6161
cmd.handler = function(argv) {
6262
session.argv = argv;
63-
nconf.file('local', h.getConfigFile());
63+
nconf.file('local', file.configFile());
6464

6565
// show all
6666
if (argv.key.length === 0)

‎lib/commands/show.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var _ = require('underscore');
66
var childProcess = require('child_process');
77

88
var h = require('../helper');
9+
var file = require('../file');
910
var chalk = require('../chalk');
1011
var icon = require('../icon');
1112
var log = require('../log');
@@ -123,7 +124,7 @@ function showProblem(problem, argv) {
123124
let filename;
124125
if (argv.gen) {
125126
filename = genFileName(problem, argv);
126-
h.mkdir(argv.outdir);
127+
file.mkdir(argv.outdir);
127128
fs.writeFileSync(filename, code);
128129

129130
if (argv.editor !== undefined) {

‎lib/commands/submission.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var fs = require('fs');
44
var sprintf = require('sprintf-js').sprintf;
55

66
var h = require('../helper');
7+
var file = require('../file');
78
var chalk = require('../chalk');
89
var log = require('../log');
910
var Queue = require('../queue');
@@ -99,7 +100,7 @@ function exportSubmission(problem, argv, cb) {
99100
submission.ac ? 'ac' : 'notac',
100101
h.langToExt(submission.lang));
101102

102-
h.mkdir(argv.outdir);
103+
file.mkdir(argv.outdir);
103104
// skip the existing cached submissions
104105
if (fs.existsSync(f))
105106
return cb(null, chalk.underline(f));

‎lib/commands/submit.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var fs = require('fs');
33
var util = require('util');
44

55
var h = require('../helper');
6+
var file = require('../file');
67
var chalk = require('../chalk');
78
var log = require('../log');
89
var core = require('../core');
@@ -48,7 +49,7 @@ cmd.handler = function(argv) {
4849

4950
// use the 1st section in filename as keyword
5051
// e.g. two-sum.cpp, or two-sum.78502271.ac.cpp
51-
const keyword = h.getFilename(argv.filename).split('.')[0];
52+
const keyword = file.name(argv.filename).split('.')[0];
5253

5354
core.getProblem(keyword, function(e, problem) {
5455
if (e) return log.fail(e);

‎lib/commands/test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var fs = require('fs');
33
var _ = require('underscore');
44

55
var h = require('../helper');
6+
var file = require('../file');
67
var chalk = require('../chalk');
78
var log = require('../log');
89
var core = require('../core');
@@ -58,7 +59,7 @@ function runTest(argv) {
5859

5960
// use the 1st section in filename as keyword
6061
// e.g. two-sum.cpp, or two-sum.78502271.ac.cpp
61-
const keyword = h.getFilename(argv.filename).split('.')[0];
62+
const keyword = file.name(argv.filename).split('.')[0];
6263

6364
core.getProblem(keyword, function(e, problem) {
6465
if (e) return log.fail(e);

‎lib/commands/version.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
var _ = require('underscore');
33

4+
var file = require('../file');
45
var chalk = require('../chalk');
56
var icon = require('../icon');
67
var log = require('../log');
@@ -50,15 +51,14 @@ cmd.handler = function(argv) {
5051
].join('\n');
5152
log.info(logo);
5253

53-
const h = require('../helper');
5454
const os = require('os');
5555
const config = require('../config');
5656

5757
log.info('\n[Environment]');
5858
printLine('Node', process.version);
5959
printLine('OS', os.platform() + ' ' + os.release());
60-
printLine('Cache', h.getCacheDir());
61-
printLine('Config', h.getConfigFile());
60+
printLine('Cache', file.cacheDir());
61+
printLine('Config', file.configFile());
6262

6363
log.info('\n[Configuration]');
6464
_.each(config.getAll(true), function(v, k) {

‎lib/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
var _ = require('underscore');
33
var nconf = require('nconf');
44

5-
var h = require('./helper');
5+
var file = require('./file');
66

77
const DEFAULT_CONFIG = {
88
// usually you don't wanna change those
@@ -71,7 +71,7 @@ const DEFAULT_CONFIG = {
7171
function Config() {}
7272

7373
Config.prototype.init = function() {
74-
nconf.file('local', h.getConfigFile())
74+
nconf.file('local', file.configFile())
7575
.add('global', {type: 'literal', store: DEFAULT_CONFIG})
7676
.defaults({});
7777

0 commit comments

Comments
(0)

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