From 8fe667eb0f421a21d213385e993675af8308052c Mon Sep 17 00:00:00 2001 From: Rashid Lazytech Date: Thu, 12 Jan 2023 20:55:32 +0500 Subject: [PATCH 01/15] add directory other32, change position of directory win32 (.gitignore) --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 45530ba..ca5b2b2 100644 --- a/.gitignore +++ b/.gitignore @@ -45,5 +45,8 @@ binaries # VS Code .vs/ .vscode/ -win32/ .nyc_output/ + +# 7-Zip +win32/ +other32/ \ No newline at end of file From cb95fe1759e303054158efa2040d97a1d62a5186 Mon Sep 17 00:00:00 2001 From: Rashid Lazytech Date: Thu, 12 Jan 2023 21:57:07 +0500 Subject: [PATCH 02/15] refactor 'retry' function (index.ts) --- src/index.ts | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/src/index.ts b/src/index.ts index b047959..fda41fd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,26 +28,16 @@ function retry( archive: string ) { // Start the command - return Run('7z', command, options, override) - .progress(function (data: any) { - return progress(onprogress(data)); - }) // When all is done resolve the Promise. - .then(function (args: string[]) { - return resolve(args); - }) // Catch the error and pass it to the reject function of the Promise. - .catch(function () { - console.error(archive + ' failed using `7z`, retying with `7za`.'); - Run('7za', command, options, override) - .progress(function (data: any) { - return progress(onprogress(data)); - }) - .then(function (args: string[]) { - return resolve(args); - }) - .catch(function (err: any) { - return reject(err); - }); + let executables = ['7z', '7za']; // Two or more executables + const runner = () => Run(executables.shift(), command, options, override) + .progress((data: any) => progress(onprogress(data))) + .then((args: string[]) => resolve(args)) // When all is done resolve the Promise. + .catch((err: any) => { // Catch the error and pass it to the reject function of the Promise. + if (!executables.length) return reject(err); + console.error(archive + ' failed using `7z`, retrying with `7za`.'); + runner(); }); + return runner(); } /** From 064873c11f368a70dbd7a94a851fa478946e7724 Mon Sep 17 00:00:00 2001 From: Rashid Lazytech Date: Thu, 12 Jan 2023 21:57:07 +0500 Subject: [PATCH 03/15] refactor 'retry' function (index.ts) --- src/index.ts | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/src/index.ts b/src/index.ts index b047959..7983990 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,26 +28,17 @@ function retry( archive: string ) { // Start the command - return Run('7z', command, options, override) - .progress(function (data: any) { - return progress(onprogress(data)); - }) // When all is done resolve the Promise. - .then(function (args: string[]) { - return resolve(args); - }) // Catch the error and pass it to the reject function of the Promise. - .catch(function () { - console.error(archive + ' failed using `7z`, retying with `7za`.'); - Run('7za', command, options, override) - .progress(function (data: any) { - return progress(onprogress(data)); - }) - .then(function (args: string[]) { - return resolve(args); - }) - .catch(function (err: any) { - return reject(err); - }); + let executables = ['7z', '7za']; + const runner = () => Run(executables.shift(), command, options, override) + .progress((data: any) => progress(onprogress(data))) + .then((args: string[]) => resolve(args)) // When all is done resolve the Promise. + .catch((err: any) => { // Catch the error and pass it to the reject function of the Promise. + if (!executables.length) return reject(err); + console.error(archive + ' failed using `' + executables[0] + + '`, retrying with `' + executables[1] + '`.'); + runner(); }); + return runner(); } /** From 7e87f4ba267c78af955a396035876e15b3dba37e Mon Sep 17 00:00:00 2001 From: Rashid Lazytech Date: Sat, 14 Jan 2023 09:20:40 +0500 Subject: [PATCH 04/15] refactor deleteArchive function (index.ts) --- src/index.ts | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/index.ts b/src/index.ts index 5098b22..71dd01b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -117,24 +117,20 @@ export const deleteArchive = // Convert array of files into a string if needed. files = Files(files); // Create a string that can be parsed by `run`. - let command = 'd "' + filepath + '" ' + files; + let command = `d "${filepath}" ${files}`; // Start the command - Run('7z', command, options, override) // When all is done resolve the Promise. - .then(function (args) { - return resolve(args); - }) // Catch the error and pass it to the reject function of the Promise. - .catch(function () { - console.error( - 'DeleteArchive failed using `7z`, retying with `7za`.' - ); - Run('7za', command, options, override) - .then(function (args) { - return resolve(args); - }) - .catch(function (err) { - return reject(err); - }); + const executables = ['7z', '7za']; // Two or more items + let position = 0; + const runner = () => Run(executables[position], command, options, override) + .then((args: any[]) => resolve(args)) // When all is done resolve the Promise. + .catch((err: any) => { // Catch the error and pass it to the reject function of the Promise. + if (position === executables.length - 1) return reject(err); + console.error('DeleteArchive failed using `' + executables[position] + + '`, retrying with `' + executables[position + 1] + '`.'); + position++; + runner(); }); + return runner(); }); }); From b44c004678ab3f27f688e13d5b7b7a88f508160d Mon Sep 17 00:00:00 2001 From: Rashid Lazytech Date: Sat, 14 Jan 2023 09:41:52 +0500 Subject: [PATCH 05/15] partially refactor listArchive function (index.ts) --- src/index.ts | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/src/index.ts b/src/index.ts index 71dd01b..fa39b79 100644 --- a/src/index.ts +++ b/src/index.ts @@ -328,32 +328,20 @@ export const listArchive = // Create a string that can be parsed by `run`. let command = 'l "' + filepath + '" '; - Run(isWindows() ? '7z' : '7za', command, options, override) - .progress(function (data: string) { - return progress(onprogress(data)); - }) - .then(function () { - return resolve(spec); - }) - .catch(function (err: any) { - if (isWindows()) { - console.error( - 'ListArchive failed using `7z`, retying with `7za`.' - ); - Run('7za', command, options, override) - .progress(function (data: string) { - return progress(onprogress(data)); - }) - .then(function (args: any) { - return resolve(args); - }) - .catch(function (err: any) { - return reject(err); - }); - } else { - return reject(err); - } + // Start the command + const executables = ['7z', '7za']; + let position = isWindows() ? 0 : 1; // Windows - 2 attempts, others - 1 attempt + const runner = () => Run(executables[position], command, options, override) + .progress((data: string) => progress(onprogress(data))) + .then((args: any) => resolve(position === 1 && isWindows() ? args : spec)) + .catch((err: any) => { + if (position === executables.length - 1) return reject(err); + console.error('ListArchive failed using `' + executables[position] + + '`, retrying with `' + executables[position + 1] + '`.'); + position++; + runner(); }); + return runner(); }); }); From 4754b21d9e264ee1de9b73c43b657bf3d78cc0b8 Mon Sep 17 00:00:00 2001 From: Rashid Lazytech Date: Sat, 14 Jan 2023 12:06:22 +0500 Subject: [PATCH 06/15] partially refactor listArchive function (index.ts) --- src/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index fa39b79..050e1bc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -329,11 +329,11 @@ export const listArchive = // Create a string that can be parsed by `run`. let command = 'l "' + filepath + '" '; // Start the command - const executables = ['7z', '7za']; - let position = isWindows() ? 0 : 1; // Windows - 2 attempts, others - 1 attempt + const executables = isWindows() ? ['7z', '7za'] : ['7za']; + let position = 0; const runner = () => Run(executables[position], command, options, override) .progress((data: string) => progress(onprogress(data))) - .then((args: any) => resolve(position === 1 && isWindows() ? args : spec)) + .then((args: any) => resolve(position === 0 ? spec : args)) .catch((err: any) => { if (position === executables.length - 1) return reject(err); console.error('ListArchive failed using `' + executables[position] + From e9d7d5355a181425246b08d22f13523068a919a7 Mon Sep 17 00:00:00 2001 From: Rashid Lazytech Date: Sun, 15 Jan 2023 08:19:15 +0500 Subject: [PATCH 07/15] refactor Files function (utility.ts) --- src/utility.ts | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/src/utility.ts b/src/utility.ts index 906bbe4..caaed82 100644 --- a/src/utility.ts +++ b/src/utility.ts @@ -34,22 +34,10 @@ export const Binary = function (override = false, binary = '7z') { * @return {string} */ export const Files = function (files: string | string[]): string { - if (isUndefined(files)) { - return ''; - } - - let toProcess = ''; - - if (Array.isArray(files)) { - files.forEach(function (f) { - toProcess += '"' + f + '" '; - }); - toProcess = toProcess.trim(); - } else { - toProcess = '"' + files + '"'; - } - - return toProcess; + if (isUndefined(files)) return ''; + return (Array.isArray(files) ? files : [files]) + .map((file) => `"${file}"`) + .join(' '); }; /** From 278a0415133bbf0b7d612c614cccaed5854651fe Mon Sep 17 00:00:00 2001 From: Rashid Lazytech Date: Sun, 15 Jan 2023 08:24:11 +0500 Subject: [PATCH 08/15] refactor Binary function (utility.ts) --- src/utility.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/utility.ts b/src/utility.ts index caaed82..16f5338 100644 --- a/src/utility.ts +++ b/src/utility.ts @@ -15,16 +15,11 @@ export const Binary = function (override = false, binary = '7z') { __dirname, '..', 'binaries', - override === true - ? process.platform + sep + 'other32' - : process.platform + `${process.platform}${override === true ? sep + 'other32' : ''}` ); - let filename = isWindows() ? binary + '.exe' : binary; - return { - path: path, - filename: filename, - filepath: join(path, filename), - }; + const filename = `${binary}${isWindows() ? '.exe' : ''}`; + const filepath = join(path, filename); + return { path, filename, filepath }; }; /** From bb33f69acc051d44bb47d2ed012e72e8474c1b41 Mon Sep 17 00:00:00 2001 From: Rashid Lazytech Date: Sun, 15 Jan 2023 08:24:11 +0500 Subject: [PATCH 09/15] refactor Binary function (utility.ts) --- src/utility.ts | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/utility.ts b/src/utility.ts index caaed82..841fe56 100644 --- a/src/utility.ts +++ b/src/utility.ts @@ -11,20 +11,15 @@ const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); export const Binary = function (override = false, binary = '7z') { - let path = join( + const path = join( __dirname, '..', 'binaries', - override === true - ? process.platform + sep + 'other32' - : process.platform + `${process.platform}${override === true ? sep + 'other32' : ''}` ); - let filename = isWindows() ? binary + '.exe' : binary; - return { - path: path, - filename: filename, - filepath: join(path, filename), - }; + const filename = `${binary}${isWindows() ? '.exe' : ''}`; + const filepath = join(path, filename); + return { path, filename, filepath }; }; /** From d3ce72d9f34cf47101863f4a1899511148ccda12 Mon Sep 17 00:00:00 2001 From: Rashid Lazytech Date: Sun, 15 Jan 2023 08:36:20 +0500 Subject: [PATCH 10/15] partially refactor function returned by Run function (utility.ts) --- src/utility.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/utility.ts b/src/utility.ts index 841fe56..392252f 100644 --- a/src/utility.ts +++ b/src/utility.ts @@ -86,15 +86,9 @@ export function Run( let args = [command.split(' ')[0]]; // Parse and add command (non-switches parameters) to `args`. let regexpCommands = /"((?:\\.|[^"\\])*)"/g; - let commands = command.match(regexpCommands); - - if (commands) { - commands.forEach(function (c) { - c = c.replace(/\//g, sep); - c = c.replace(/\\/g, sep); - c = normalize(c); - args.push(c); - }); + let commands = command.match(regexpCommands) || []; + for (command of commands) { + args.push(normalize(command.replace(/(\/|\\)/g, sep))); } // Special treatment for the output switch because it is exposed as a From b749cc15272c80d9179d9bc2fd6b62d4e96e5cd6 Mon Sep 17 00:00:00 2001 From: Rashid Lazytech Date: Sun, 15 Jan 2023 09:22:29 +0500 Subject: [PATCH 11/15] partially refactor function returned by Run function (utility.ts) --- src/utility.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/utility.ts b/src/utility.ts index 392252f..5175318 100644 --- a/src/utility.ts +++ b/src/utility.ts @@ -98,12 +98,12 @@ export function Run( if (output) { args.pop(); - let o = output[0]; - o = o.replace(/\//g, sep); - o = o.replace(/\\/g, sep); - o = o.replace(/"/g, ''); - o = normalize(o); - args.push(o); + args.push( + normalize(output[0].replace( + /(\/|\\|")/g, + (match) => match === '"' ? '' : sep) + ) + ); } if (switches.files) { From 0d9d4ef1c7ae84f303414e7cbb08b4396e4c5c4f Mon Sep 17 00:00:00 2001 From: Rashid Lazytech Date: Sun, 15 Jan 2023 09:31:32 +0500 Subject: [PATCH 12/15] partially refactor function returned by Run function (utility.ts) --- src/utility.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/utility.ts b/src/utility.ts index 5175318..6aef4fd 100644 --- a/src/utility.ts +++ b/src/utility.ts @@ -88,7 +88,8 @@ export function Run( let regexpCommands = /"((?:\\.|[^"\\])*)"/g; let commands = command.match(regexpCommands) || []; for (command of commands) { - args.push(normalize(command.replace(/(\/|\\)/g, sep))); + const arg = command.replace(/(\/|\\)/g, sep); + args.push(normalize(arg)); } // Special treatment for the output switch because it is exposed as a @@ -98,12 +99,8 @@ export function Run( if (output) { args.pop(); - args.push( - normalize(output[0].replace( - /(\/|\\|")/g, - (match) => match === '"' ? '' : sep) - ) - ); + const arg = output[0].replace(/(\/|\\|")/g, (match) => match === '"' ? '' : sep); + args.push(normalize(arg)); } if (switches.files) { From f8fac3ef83915f41693fd20caf44bd00c53d0d1d Mon Sep 17 00:00:00 2001 From: Rashid Lazytech Date: Sun, 15 Jan 2023 12:18:49 +0500 Subject: [PATCH 13/15] refactor ReplaceNativeSeparator function (utility.ts) --- src/utility.ts | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/utility.ts b/src/utility.ts index 6aef4fd..7ad9412 100644 --- a/src/utility.ts +++ b/src/utility.ts @@ -40,14 +40,7 @@ export const Files = function (files: string | string[]): string { * @return {string} A path with / for directory separator. */ export const ReplaceNativeSeparator = function (path: string): string { - let result = path, - next; - - while ((next = result.replace(nativeSeparator, '/')) !== result) { - result = next; - } - - return result; + return path.replace(new RegExp(`\\${nativeSeparator}`, 'g'), '/'); }; /** @@ -64,7 +57,7 @@ export const ReplaceNativeSeparator = function (path: string): string { */ export function Run( binary: string = '7z', - command: string | null = null, + command: string | null = null, switches: { files?: string[] } = {}, override: boolean = false ) { From 58691754e30ff49dc4095e08c406191c3c9d2c22 Mon Sep 17 00:00:00 2001 From: Rashid Lazytech Date: Sun, 15 Jan 2023 12:18:49 +0500 Subject: [PATCH 14/15] refactor ReplaceNativeSeparator function (utility.ts) --- src/utility.ts | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/utility.ts b/src/utility.ts index 6aef4fd..f991804 100644 --- a/src/utility.ts +++ b/src/utility.ts @@ -40,14 +40,7 @@ export const Files = function (files: string | string[]): string { * @return {string} A path with / for directory separator. */ export const ReplaceNativeSeparator = function (path: string): string { - let result = path, - next; - - while ((next = result.replace(nativeSeparator, '/')) !== result) { - result = next; - } - - return result; + return path.replace(new RegExp(`\\${nativeSeparator}`, 'g'), '/'); }; /** From 25fce7f3ff5a0b1946762bee56d2a59c12aa1c18 Mon Sep 17 00:00:00 2001 From: Rashid Lazytech Date: Sun, 15 Jan 2023 12:30:50 +0500 Subject: [PATCH 15/15] fix line indent (utility.ts) --- src/utility.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utility.ts b/src/utility.ts index 7ad9412..f991804 100644 --- a/src/utility.ts +++ b/src/utility.ts @@ -57,7 +57,7 @@ export const ReplaceNativeSeparator = function (path: string): string { */ export function Run( binary: string = '7z', - command: string | null = null, + command: string | null = null, switches: { files?: string[] } = {}, override: boolean = false ) {