Skip to content

Commit 6b2d514

Browse files
committed
[refactor] use file utils.
Signed-off-by: Eric Wang <[email protected]>
1 parent e7ac6a6 commit 6b2d514

File tree

8 files changed

+37
-22
lines changed

8 files changed

+37
-22
lines changed

lib/cache.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict';
2-
var fs = require('fs');
32
var path = require('path');
43

54
var file = require('./file');
@@ -12,22 +11,22 @@ cache.init = function() {
1211

1312
cache.get = function(k) {
1413
const fullpath = file.cacheFile(k);
15-
if (!fs.existsSync(fullpath)) return null;
14+
if (!file.exist(fullpath)) return null;
1615

17-
return JSON.parse(fs.readFileSync(fullpath));
16+
return JSON.parse(file.data(fullpath));
1817
};
1918

2019
cache.set = function(k, v) {
2120
const fullpath = file.cacheFile(k);
22-
fs.writeFileSync(fullpath, JSON.stringify(v));
21+
file.write(fullpath, JSON.stringify(v));
2322
return true;
2423
};
2524

2625
cache.del = function(k) {
2726
const fullpath = file.cacheFile(k);
28-
if (!fs.existsSync(fullpath)) return false;
27+
if (!file.exist(fullpath)) return false;
2928

30-
fs.unlinkSync(fullpath);
29+
file.rm(fullpath);
3130
return true;
3231
};
3332

@@ -36,7 +35,7 @@ cache.list = function() {
3635
.filter(x => path.extname(x) === '.json')
3736
.map(function(filename) {
3837
const k = path.basename(filename, '.json');
39-
const stat = fs.statSync(file.cacheFile(k));
38+
const stat = file.stat(file.cacheFile(k));
4039
return {
4140
name: k,
4241
size: stat.size,

lib/commands/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function loadConfig(showall) {
5555
}
5656

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

6161
cmd.handler = function(argv) {

lib/commands/show.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict';
2-
var fs = require('fs');
32
var util = require('util');
43

54
var _ = require('underscore');
@@ -84,7 +83,7 @@ function genFileName(problem, opts) {
8483
// try new name to avoid overwrite by mistake
8584
for (let i = 0; ; ++i) {
8685
const name = path.join(opts.outdir, params.join('.').replace(/\.+/g, '.'));
87-
if (!fs.existsSync(name))
86+
if (!file.exist(name))
8887
return name;
8988
params[2] = i;
9089
}
@@ -123,7 +122,7 @@ function showProblem(problem, argv) {
123122
if (argv.gen) {
124123
file.mkdir(argv.outdir);
125124
filename = genFileName(problem, argv);
126-
fs.writeFileSync(filename, code);
125+
file.write(filename, code);
127126

128127
if (argv.editor !== undefined) {
129128
childProcess.spawn(argv.editor || config.code.editor, [filename], {

lib/commands/submission.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict';
2-
var fs = require('fs');
32
var path = require('path');
43

54
var _ = require('underscore');
@@ -102,7 +101,7 @@ function exportSubmission(problem, argv, cb) {
102101

103102
file.mkdir(argv.outdir);
104103
// skip the existing cached submissions
105-
if (fs.existsSync(f))
104+
if (file.exist(f))
106105
return cb(null, chalk.underline(f));
107106

108107
core.getSubmission(submission, function(e, submission) {
@@ -113,7 +112,7 @@ function exportSubmission(problem, argv, cb) {
113112
code: submission.code,
114113
tpl: argv.extra ? 'detailed' : 'codeonly'
115114
};
116-
fs.writeFileSync(f, core.exportProblem(problem, opts));
115+
file.write(f, core.exportProblem(problem, opts));
117116
cb(null, submission.ac ? chalk.green.underline(f)
118117
: chalk.yellow.underline(f));
119118
});

lib/commands/submit.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict';
2-
var fs = require('fs');
32
var util = require('util');
43

54
var h = require('../helper');
@@ -44,7 +43,7 @@ function printLine() {
4443

4544
cmd.handler = function(argv) {
4645
session.argv = argv;
47-
if (!fs.existsSync(argv.filename))
46+
if (!file.exist(argv.filename))
4847
return log.error('File ' + argv.filename + ' not exist!');
4948

5049
const meta = file.meta(argv.filename);

lib/commands/test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict';
2-
var fs = require('fs');
32
var _ = require('underscore');
43

54
var h = require('../helper');
@@ -54,7 +53,7 @@ function printResult(actual, expect, k) {
5453
}
5554

5655
function runTest(argv) {
57-
if (!fs.existsSync(argv.filename))
56+
if (!file.exist(argv.filename))
5857
return log.error('File ' + argv.filename + ' not exist!');
5958

6059
const meta = file.meta(argv.filename);

lib/file.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,29 @@ file.mkdir = function(fullpath) {
7575
mkdirp.sync(fullpath);
7676
};
7777

78+
file.exist = function(fullpath) {
79+
return fs.existsSync(fullpath);
80+
};
81+
82+
file.rm = function(fullpath) {
83+
return fs.unlinkSync(fullpath);
84+
};
85+
86+
file.mv = function(src, dst) {
87+
return fs.renameSync(src, dst);
88+
};
89+
7890
file.list = function(dir) {
7991
return fs.readdirSync(dir);
80-
}
92+
};
93+
94+
file.stat = function(fullpath) {
95+
return fs.statSync(fullpath);
96+
};
97+
98+
file.write = function(fullpath, data) {
99+
return fs.writeFileSync(fullpath, data);
100+
};
81101

82102
file.name = function(fullpath) {
83103
return path.basename(fullpath, path.extname(fullpath));

lib/plugin.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Plugin.prototype.enable = function(enabled) {
4949
if (this.enabled === enabled) return;
5050
const newfile = enabled ? this.file.substr(1) : '.' + this.file;
5151
try {
52-
fs.renameSync(file.pluginFile(this.file), file.pluginFile(newfile));
52+
file.mv(file.pluginFile(this.file), file.pluginFile(newfile));
5353
} catch(e) {
5454
log.error(e.message);
5555
}
@@ -60,7 +60,7 @@ Plugin.prototype.delete = function() {
6060
if (!this.missing) {
6161
try {
6262
const fullpath = file.pluginFile(this.file);
63-
fs.unlinkSync(fullpath);
63+
file.rm(fullpath);
6464
} catch(e) {
6565
return log.error(e.message);
6666
}
@@ -156,7 +156,7 @@ Plugin.copy = function(src, cb) {
156156
});
157157
srcstream.on('error', function(e) {
158158
spin.stop();
159-
fs.unlinkSync(dst);
159+
file.rm(dst);
160160
return cb(e);
161161
});
162162

0 commit comments

Comments
 (0)