Skip to content

Commit 5850f5a

Browse files
committed
Update index.js
1 parent 1a60c5f commit 5850f5a

File tree

1 file changed

+38
-164
lines changed

1 file changed

+38
-164
lines changed

lib/index.js

Lines changed: 38 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,36 @@ import { Files, ReplaceNativeSeparator, Run } from './utility.js';
55

66
import { createSfx } from './createSfx.js';
77

8+
function retry(command, options, override, progress, onprogress, resolve, reject, archive, noRetry = false) {
9+
// Start the command
10+
return Run('7z', command, options, override)
11+
.progress(function (data) {
12+
return progress(onprogress(data));
13+
})
14+
15+
// When all is done resolve the Promise.
16+
.then(function (args) {
17+
return resolve(args);
18+
})
19+
20+
// Catch the error and pass it to the reject function of the Promise.
21+
.catch(function () {
22+
console.error(archive + ' failed using `7z`, retying with `7za`.');
23+
Run('7za', command, options, override)
24+
.progress(function (data) {
25+
return progress(onprogress(data));
26+
})
27+
28+
.then(function (args) {
29+
return resolve(args);
30+
})
31+
.catch(function (err) {
32+
return reject(err);
33+
});
34+
});
35+
};
36+
37+
838
/**
939
* Create/add content to an archive.
1040
*
@@ -43,35 +73,10 @@ export const createArchive = SevenZip.createArchive = function (filepath, files,
4373
let command = 'a "' + filepath + '" ' + files;
4474

4575
// Start the command
46-
Run('7z', command, options, override)
47-
.progress(function (data) {
48-
return progress(onprogress(data));
49-
})
50-
51-
// When all is done resolve the Promise.
52-
.then(function (args) {
53-
return resolve(args);
54-
})
55-
56-
// Catch the error and pass it to the reject function of the Promise.
57-
.catch(function () {
58-
console.error('CreateArchive failed using `7z`, retying with `7za`.');
59-
Run('7za', command, options, override)
60-
.progress(function (data) {
61-
return progress(onprogress(data));
62-
})
63-
64-
.then(function (args) {
65-
return resolve(args);
66-
})
67-
.catch(function (err) {
68-
return reject(err);
69-
});
70-
});
76+
return retry(command, options, override, progress, onprogress, resolve, reject, 'CreateArchive');
7177
});
7278
};
7379

74-
7580
/**
7681
* Delete content from an archive.
7782
*
@@ -151,30 +156,7 @@ export const extractArchive = SevenZip.extractArchive = function (filepath, dest
151156
let command = 'e "' + filepath + '" -o"' + dest + '" ';
152157

153158
// Start the command
154-
Run('7z', command, options, override)
155-
.progress(function (data) {
156-
return progress(onprogress(data));
157-
})
158-
159-
// When all is done resolve the Promise.
160-
.then(function (args) {
161-
return resolve(args);
162-
})
163-
164-
// Catch the error and pass it to the reject function of the Promise.
165-
.catch(function () {
166-
console.error('ExtractArchive failed using `7z`, retying with `7za`.');
167-
Run('7za', command, options, override)
168-
.progress(function (data) {
169-
return progress(onprogress(data));
170-
})
171-
.then(function (args) {
172-
return resolve(args);
173-
})
174-
.catch(function (err) {
175-
return reject(err);
176-
});
177-
});
159+
return retry(command, options, override, progress, onprogress, resolve, reject, 'ExtractArchive');
178160
});
179161
};
180162

@@ -212,26 +194,7 @@ export const fullArchive = SevenZip.fullArchive = function (filepath, dest = '*'
212194
// Create a string that can be parsed by `run`.
213195
let command = 'x "' + filepath + '" -o"' + dest + '" ';
214196

215-
Run('7z', command, options, override)
216-
.progress(function (data) {
217-
return progress(onprogress(data));
218-
})
219-
.then(function (args) {
220-
return resolve(args);
221-
})
222-
.catch(function () {
223-
console.error('FullArchive failed using `7z`, retying with `7za`.');
224-
Run('7za', command, options, override)
225-
.progress(function (data) {
226-
return progress(onprogress(data));
227-
})
228-
.then(function (args) {
229-
return resolve(args);
230-
})
231-
.catch(function (err) {
232-
return reject(err);
233-
});
234-
});
197+
return retry(command, options, override, progress, onprogress, resolve, reject, 'FullArchive');
235198
});
236199
};
237200

@@ -303,6 +266,7 @@ export const listArchive = SevenZip.listArchive = function (filepath, options, o
303266

304267
// Create a string that can be parsed by `run`.
305268
let command = 'l "' + filepath + '" ';
269+
306270
Run((process.platform == 'win32' ? '7z' : '7za'), command, options, override)
307271
.progress(function (data) {
308272
return progress(onprogress(data));
@@ -368,30 +332,7 @@ export const onlyArchive = SevenZip.onlyArchive = function (filepath, dest, file
368332
let command = 'e "' + filepath + '" -o"' + dest + '"';
369333

370334
// Start the command
371-
Run('7z', command, options, override)
372-
.progress(function (data) {
373-
return progress(onprogress(data));
374-
})
375-
376-
// When all is done resolve the Promise.
377-
.then(function (args) {
378-
return resolve(args);
379-
})
380-
381-
// Catch the error and pass it to the reject function of the Promise.
382-
.catch(function () {
383-
console.error('OnlyArchive failed using `7z`, retying with `7za`.');
384-
Run('7za', command, options, override)
385-
.progress(function (data) {
386-
return progress(onprogress(data));
387-
})
388-
.then(function (args) {
389-
return resolve(args);
390-
})
391-
.catch(function (err) {
392-
return reject(err);
393-
});
394-
});
335+
return retry(command, options, override, progress, onprogress, resolve, reject, 'OnlyArchive');
395336
});
396337
};
397338

@@ -433,26 +374,7 @@ export const renameArchive = SevenZip.renameArchive = function (filepath, files,
433374
let command = 'rn "' + filepath + '" ' + files;
434375

435376
// Start the command
436-
Run('7z', command, options, override)
437-
.progress(function (data) {
438-
return progress(onprogress(data));
439-
})
440-
.then(function () {
441-
return resolve();
442-
})
443-
.catch(function () {
444-
console.error('RenameArchive failed using `7z`, retying with `7za`.');
445-
Run('7za', command, options, override)
446-
.progress(function (data) {
447-
return progress(onprogress(data));
448-
})
449-
.then(function (args) {
450-
return resolve(args);
451-
})
452-
.catch(function (err) {
453-
return reject(err);
454-
});
455-
});
377+
return retry(command, options, override, progress, onprogress, resolve, reject, 'RenameArchive');
456378
});
457379
};
458380

@@ -490,31 +412,7 @@ export const testArchive = SevenZip.testArchive = function (filepath, options, o
490412
let command = 't "' + filepath + '"';
491413

492414
// Start the command
493-
Run('7z', command, options, override)
494-
.progress(function (data) {
495-
return progress(onprogress(data));
496-
})
497-
498-
// When all is done resolve the Promise.
499-
.then(function (args) {
500-
return resolve(args);
501-
})
502-
503-
// Catch the error and pass it to the reject function of the Promise.
504-
.catch(function () {
505-
console.error('TestArchive failed using `7z`, retying with `7za`.');
506-
Run('7za', command, options, override)
507-
.progress(function (data) {
508-
return progress(onprogress(data));
509-
})
510-
.then(function (args) {
511-
return resolve(args);
512-
})
513-
.catch(function (err) {
514-
return reject(err);
515-
});
516-
});
517-
415+
return retry(command, options, override, progress, onprogress, resolve, reject, 'TestArchive');
518416
});
519417
};
520418

@@ -556,31 +454,7 @@ export const updateArchive = SevenZip.updateArchive = function (filepath, files,
556454
let command = 'u "' + filepath + '" ' + files;
557455

558456
// Start the command
559-
Run('7z', command, options, override)
560-
.progress(function (data) {
561-
return progress(onprogress(data));
562-
})
563-
564-
// When all is done resolve the Promise.
565-
.then(function () {
566-
return resolve();
567-
})
568-
569-
// Catch the error and pass it to the reject function of the Promise.
570-
.catch(function () {
571-
console.error('UpdateArchive failed using `7z`, retying with `7za`.');
572-
Run('7za', command, options, override)
573-
.progress(function (data) {
574-
return progress(onprogress(data));
575-
})
576-
.then(function (args) {
577-
return resolve(args);
578-
})
579-
.catch(function (err) {
580-
return reject(err);
581-
});
582-
});
583-
457+
return retry(command, options, override, progress, onprogress, resolve, reject, 'UpdateArchive');
584458
});
585459
};
586460

0 commit comments

Comments
 (0)