-
Notifications
You must be signed in to change notification settings - Fork 13
/
split-file-cli.js
executable file
·111 lines (97 loc) · 2.61 KB
/
split-file-cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env node
var split = require('./split-file.js');
var Cli = function () {};
/**
* Parse cli option.
*/
Cli.prototype.parse = function (option) {
this.option = option;
switch (option) {
case '-m':
this.method = this.merge;
break;
case '-s':
this.method = this.split;
break;
case '-x':
this.method = this.splitFileBySize
default:
this.method = this.help;
}
return this;
}
/**
* Print the legend.
*/
Cli.prototype.help = function () {
console.log("Usage: split-file -s input.bin 5");
console.log(" split-file -x input.bin 457000");
console.log(" split-file -m output.bin part1 part2 ...");
console.log("");
console.log(" -s <input> <num_parts>");
console.log(" Split the input file in the number of parts given.");
console.log("");
console.log(" -x <input> <max_size>");
console.log(" Split the input file into multiple parts with file size maximum of max_size bytes");
console.log("");
console.log(" -m <output> <part> <part> ...");
console.log(" Merge the given parts into the output file.");
console.log("");
console.log("");
console.log("NPM Module 'split-file' by Tom Valk.");
console.log("Visit https://github.com/tomvlk/node-split-file for info and help.");
}
/**
* Split command.
*/
Cli.prototype.split = function () {
var file = process.argv[3];
var parts = parseInt(process.argv[4]);
if (isNaN(parts)) {
return this.help();
}
split.splitFile(file, parts).then(function (names) {
console.log('Successfully splitted into: ' + names);
}).catch(function (err) {
console.log('An error occured:');
console.log(err);
});
}
Cli.prototype.splitFileBySize = function() {
var file = process.argv[3];
var max_size = parseInt(process.argv[4]);
if (isNaN(max_size)) {
return this.help();
}
split.splitFileBySize(file, max_size).then(function (names) {
console.log('Successfully splitted into: ' + names);
}).catch(function (err) {
console.log('An error occured:');
console.log(err);
});
}
/**
* Merge command.
*/
Cli.prototype.merge = function () {
var files = [];
var output_file = process.argv[3];
for (var i = 4; i < process.argv.length; i++) {
files.push(process.argv[i]);
}
split.mergeFiles(files, output_file).then(function() {
console.log('Succesfully merged the parts into ' + output_file);
}).catch(function (err) {
console.log('An error occured:');
console.log(err);
});
}
Cli.prototype.run = function () {
return this.method();
}
if (require.main === module) {
var cli = new Cli();
cli
.parse(process.argv[2])
.run();
}