-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
132 lines (115 loc) · 3.34 KB
/
index.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
(function () {
// Required modules
var path = require('path');
var fs = require('fs');
var nrc = require('node-run-cmd');
// Example call (mostly for testing purpose)
function example() {
backup(['gulpfile.js'], 'output/');
}
// Backup function to create 7z files of the elements in paths_input in the directory dir_output with the password pw
function backup(paths_input, dir_output, pw) {
// Settings for the compression
var settings = {
mx0: true, // no compression
t7z: true, // use 7z for any filename
};
// If password is given
if (pw !== undefined) {
settings.p = pw;
settings.mhe = true; // encrypt headers
}
// Zip all input paths
paths_input.forEach(function (path_input) {
// Generate an output path from path_input
var filename = path.basename(path_input);
if (['', '.', '..'].includes(filename)) {
filename = 'Backup.7z';
} else {
filename = filename + '.7z';
}
// Normalize paths
path_input = path.normalize(path_input);
path_output = path.normalize(dir_output) + filename;
path_output_tmp = path_output + '.tmp';
// Check if already an output file with the same name exists
if (fs.existsSync(path_output)) {
// Delete old tmp file if it exists
if (fs.existsSync(path_output_tmp)) {
console.log('Removing old temporary file', path_output_tmp);
fs.unlinkSync(path_output_tmp);
}
// Rename existing output to the tmp file
console.log('Creating temporary file of current backup.', path_output_tmp);
fs.renameSync(path_output, path_output_tmp);
}
// Zip it
console.log('Creating for:', path_input);
zip(path_input, path_output, settings, onSuccessfulZip);
function onSuccessfulZip(successful) {
if (successful) {
console.log('7-zip was successful.');
console.log('Removing old temporary file.', path_output_tmp);
if (fs.existsSync(path_output_tmp)) {
fs.unlinkSync(path_output_tmp);
}
}
else{
console.log('7-zip was NOT successful.');
}
}
});
}
// Zip command
function zip(path_input, path_output, settings, onSuccess, onError) {
var successful = false;
// Options for 7zip
command = '7z a "' + path_output + '" "' + path_input + '"' + settings2str(settings);
// Options for nrc
options = {
verbose: true,
logger: function (output) {
output.split('\n').forEach(function (line) {
console.log('[7zip Output]', line);
if (line.includes('Everything is Ok')) {
successful = true;
}
});
}
};
// Run command line
nrc.run(command, options)
.then(function (exitCodes) {
if (onSuccess === undefined) {
console.log('Command finished with exitCodes: ', exitCodes);
console.log('command:', command);
console.log('7-zip successful?', successful);
} else {
onSuccess(successful);
}
}, function (err) {
if (onError === undefined) {
console.err(err);
}
});
}
// Convert settings from object to string for Zip-command
function settings2str(settings) {
var str = '';
for (var key in settings) {
var val = settings[key];
if (typeof val === 'boolean' && val === true) {
str += ' -' + key;
} else if (typeof val === 'string') {
str += ' -' + key + val;
}
}
return str;
}
// If this is main file
if (require.main === module) {
example();
}
// Export these functions
exports.backup = backup;
})();