Passed
Push — master ( b75e6a...c8e729 )
by Evgeny
01:46
created

lib/file.js   A

Complexity

Total Complexity 15
Complexity/F 1.67

Size

Lines of Code 52
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 31
mnd 6
bc 6
fnc 9
dl 0
loc 52
rs 10
bpm 0.6666
cpm 1.6666
noi 4
c 0
b 0
f 0
1
var args = require('./args.js');
2
3
var S3 = null;
4
var fs = null;
5
6
if (args.s3) {
7
	var AWS = require('aws-sdk');
8
	S3 = new AWS.S3();
9
} else {
10
	fs = require('fs');
11
	
12
	// Otherwise, it'll created automatically
13
	if (!fs.existsSync(args.folder)){
14
		return fs.mkdirSync(args.folder, {recursive: true});
15
	}
16
}
17
18
19
module.exports = {
20
	'readFile': function(file, callback) {
21
		if (args.s3) {
22
			return S3.getObject({Bucket: args.s3, Key: args.folder + file}, function(err, data) {
23
				return callback(err, !err && Buffer.from(data.Body).toString('utf8'));
0 ignored issues
show
Bug introduced by
The variable Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
24
			});
25
		}
26
		return fs.readFile(file, callback);
27
	},
28
	'writeFile': function(file, data, callback) {
29
		if (args.s3) {
30
			return S3.putObject({Bucket: args.s3, Key: args.folder + file, Body: data}, function(err, data) {
0 ignored issues
show
Unused Code introduced by
The parameter data is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
31
				return callback(err);
32
			});
33
		}
34
		return fs.writeFile(file, data, callback);
35
	},
36
	'unlink': function(file, callback) {
37
		if (args.s3) {
38
			return S3.deleteObject({Bucket: args.s3, Key: args.folder + file}, function(err, data) {
0 ignored issues
show
Unused Code introduced by
The parameter data is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
39
				return callback(err);
40
			});
41
		}
42
		return fs.unlink(file, callback);
43
	},
44
	'readdir': function(dir, callback) {
45
		if (args.s3) {
46
			return S3.listObjectsV2({Bucket: args.s3, Prefix: args.folder + file}, function(err, data) {
0 ignored issues
show
Bug introduced by
The variable file seems to be never declared. If this is a global, consider adding a /** global: file */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
47
				return callback(err, !err && data.Contents.map(function(v) { return v.Key; }));
48
			});
49
		}
50
		return fs.readdir(dir, callback);
51
	}
52
};