Skip to content

Commit f569455

Browse files
authored
Merge pull request #22 from Lazyt3ch/master
More code refactoring
2 parents 8f857e2 + 25fce7f commit f569455

File tree

3 files changed

+57
-110
lines changed

3 files changed

+57
-110
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,8 @@ binaries
4545
# VS Code
4646
.vs/
4747
.vscode/
48-
win32/
4948
.nyc_output/
49+
50+
# 7-Zip
51+
win32/
52+
other32/

src/index.ts

Lines changed: 37 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,19 @@ function retry(
2828
archive: string
2929
) {
3030
// Start the command
31-
return Run('7z', command, options, override)
32-
.progress(function (data: any) {
33-
return progress(onprogress(data));
34-
}) // When all is done resolve the Promise.
35-
.then(function (args: string[]) {
36-
return resolve(args);
37-
}) // Catch the error and pass it to the reject function of the Promise.
38-
.catch(function () {
39-
console.error(archive + ' failed using `7z`, retying with `7za`.');
40-
Run('7za', command, options, override)
41-
.progress(function (data: any) {
42-
return progress(onprogress(data));
43-
})
44-
.then(function (args: string[]) {
45-
return resolve(args);
46-
})
47-
.catch(function (err: any) {
48-
return reject(err);
49-
});
31+
const executables = ['7z', '7za']; // Two or more items
32+
let position = 0;
33+
const runner = () => Run(executables[position], command, options, override)
34+
.progress((data: any) => progress(onprogress(data)))
35+
.then((args: string[]) => resolve(args)) // When all is done resolve the Promise.
36+
.catch((err: any) => { // Catch the error and pass it to the reject function of the Promise.
37+
if (position === executables.length - 1) return reject(err);
38+
console.error(archive + ' failed using `' + executables[position] +
39+
'`, retrying with `' + executables[position + 1] + '`.');
40+
position++;
41+
runner();
5042
});
43+
return runner();
5144
}
5245

5346
/**
@@ -124,24 +117,20 @@ export const deleteArchive =
124117
// Convert array of files into a string if needed.
125118
files = Files(files);
126119
// Create a string that can be parsed by `run`.
127-
let command = 'd "' + filepath + '" ' + files;
120+
let command = `d "${filepath}" ${files}`;
128121
// Start the command
129-
Run('7z', command, options, override) // When all is done resolve the Promise.
130-
.then(function (args) {
131-
return resolve(args);
132-
}) // Catch the error and pass it to the reject function of the Promise.
133-
.catch(function () {
134-
console.error(
135-
'DeleteArchive failed using `7z`, retying with `7za`.'
136-
);
137-
Run('7za', command, options, override)
138-
.then(function (args) {
139-
return resolve(args);
140-
})
141-
.catch(function (err) {
142-
return reject(err);
143-
});
122+
const executables = ['7z', '7za']; // Two or more items
123+
let position = 0;
124+
const runner = () => Run(executables[position], command, options, override)
125+
.then((args: any[]) => resolve(args)) // When all is done resolve the Promise.
126+
.catch((err: any) => { // Catch the error and pass it to the reject function of the Promise.
127+
if (position === executables.length - 1) return reject(err);
128+
console.error('DeleteArchive failed using `' + executables[position] +
129+
'`, retrying with `' + executables[position + 1] + '`.');
130+
position++;
131+
runner();
144132
});
133+
return runner();
145134
});
146135
});
147136

@@ -339,32 +328,20 @@ export const listArchive =
339328

340329
// Create a string that can be parsed by `run`.
341330
let command = 'l "' + filepath + '" ';
342-
Run(isWindows() ? '7z' : '7za', command, options, override)
343-
.progress(function (data: string) {
344-
return progress(onprogress(data));
345-
})
346-
.then(function () {
347-
return resolve(spec);
348-
})
349-
.catch(function (err: any) {
350-
if (isWindows()) {
351-
console.error(
352-
'ListArchive failed using `7z`, retying with `7za`.'
353-
);
354-
Run('7za', command, options, override)
355-
.progress(function (data: string) {
356-
return progress(onprogress(data));
357-
})
358-
.then(function (args: any) {
359-
return resolve(args);
360-
})
361-
.catch(function (err: any) {
362-
return reject(err);
363-
});
364-
} else {
365-
return reject(err);
366-
}
331+
// Start the command
332+
const executables = isWindows() ? ['7z', '7za'] : ['7za'];
333+
let position = 0;
334+
const runner = () => Run(executables[position], command, options, override)
335+
.progress((data: string) => progress(onprogress(data)))
336+
.then((args: any) => resolve(position === 0 ? spec : args))
337+
.catch((err: any) => {
338+
if (position === executables.length - 1) return reject(err);
339+
console.error('ListArchive failed using `' + executables[position] +
340+
'`, retrying with `' + executables[position + 1] + '`.');
341+
position++;
342+
runner();
367343
});
344+
return runner();
368345
});
369346
});
370347

src/utility.ts

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,15 @@ const __filename = fileURLToPath(import.meta.url);
1111
const __dirname = dirname(__filename);
1212

1313
export const Binary = function (override = false, binary = '7z') {
14-
let path = join(
14+
const path = join(
1515
__dirname,
1616
'..',
1717
'binaries',
18-
override === true
19-
? process.platform + sep + 'other32'
20-
: process.platform
18+
`${process.platform}${override === true ? sep + 'other32' : ''}`
2119
);
22-
let filename = isWindows() ? binary + '.exe' : binary;
23-
return {
24-
path: path,
25-
filename: filename,
26-
filepath: join(path, filename),
27-
};
20+
const filename = `${binary}${isWindows() ? '.exe' : ''}`;
21+
const filepath = join(path, filename);
22+
return { path, filename, filepath };
2823
};
2924

3025
/**
@@ -34,37 +29,18 @@ export const Binary = function (override = false, binary = '7z') {
3429
* @return {string}
3530
*/
3631
export const Files = function (files: string | string[]): string {
37-
if (isUndefined(files)) {
38-
return '';
39-
}
40-
41-
let toProcess = '';
42-
43-
if (Array.isArray(files)) {
44-
files.forEach(function (f) {
45-
toProcess += '"' + f + '" ';
46-
});
47-
toProcess = toProcess.trim();
48-
} else {
49-
toProcess = '"' + files + '"';
50-
}
51-
52-
return toProcess;
32+
if (isUndefined(files)) return '';
33+
return (Array.isArray(files) ? files : [files])
34+
.map((file) => `"${file}"`)
35+
.join(' ');
5336
};
5437

5538
/**
5639
* @param {string} path A path with the native directory separator.
5740
* @return {string} A path with / for directory separator.
5841
*/
5942
export const ReplaceNativeSeparator = function (path: string): string {
60-
let result = path,
61-
next;
62-
63-
while ((next = result.replace(nativeSeparator, '/')) !== result) {
64-
result = next;
65-
}
66-
67-
return result;
43+
return path.replace(new RegExp(`\\${nativeSeparator}`, 'g'), '/');
6844
};
6945

7046
/**
@@ -103,15 +79,10 @@ export function Run(
10379
let args = [command.split(' ')[0]];
10480
// Parse and add command (non-switches parameters) to `args`.
10581
let regexpCommands = /"((?:\\.|[^"\\])*)"/g;
106-
let commands = command.match(regexpCommands);
107-
108-
if (commands) {
109-
commands.forEach(function (c) {
110-
c = c.replace(/\//g, sep);
111-
c = c.replace(/\\/g, sep);
112-
c = normalize(c);
113-
args.push(c);
114-
});
82+
let commands = command.match(regexpCommands) || [];
83+
for (command of commands) {
84+
const arg = command.replace(/(\/|\\)/g, sep);
85+
args.push(normalize(arg));
11586
}
11687

11788
// Special treatment for the output switch because it is exposed as a
@@ -121,12 +92,8 @@ export function Run(
12192

12293
if (output) {
12394
args.pop();
124-
let o = output[0];
125-
o = o.replace(/\//g, sep);
126-
o = o.replace(/\\/g, sep);
127-
o = o.replace(/"/g, '');
128-
o = normalize(o);
129-
args.push(o);
95+
const arg = output[0].replace(/(\/|\\|")/g, (match) => match === '"' ? '' : sep);
96+
args.push(normalize(arg));
13097
}
13198

13299
if (switches.files) {

0 commit comments

Comments
 (0)