Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More code refactoring #22

Merged
merged 20 commits into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
8fe667e
add directory other32, change position of directory win32 (.gitignore)
Lazyt3ch Jan 12, 2023
cb95fe1
refactor 'retry' function (index.ts)
Lazyt3ch Jan 12, 2023
064873c
refactor 'retry' function (index.ts)
Lazyt3ch Jan 12, 2023
32dd1b6
refactor 'retry' function (index.ts)
Lazyt3ch Jan 13, 2023
e47a53a
refactor 'retry' function (index.ts)
Lazyt3ch Jan 13, 2023
407c3e3
refactor 'retry' function (index.ts)
Lazyt3ch Jan 13, 2023
7e87f4b
refactor deleteArchive function (index.ts)
Lazyt3ch Jan 14, 2023
b44c004
partially refactor listArchive function (index.ts)
Lazyt3ch Jan 14, 2023
4754b21
partially refactor listArchive function (index.ts)
Lazyt3ch Jan 14, 2023
e9d7d53
refactor Files function (utility.ts)
Lazyt3ch Jan 15, 2023
278a041
refactor Binary function (utility.ts)
Lazyt3ch Jan 15, 2023
bb33f69
refactor Binary function (utility.ts)
Lazyt3ch Jan 15, 2023
012b1de
Merge branch 'master' of https://github.com/Lazyt3ch/node-7z-archive
Lazyt3ch Jan 15, 2023
d3ce72d
partially refactor function returned by Run function (utility.ts)
Lazyt3ch Jan 15, 2023
b749cc1
partially refactor function returned by Run function (utility.ts)
Lazyt3ch Jan 15, 2023
0d9d4ef
partially refactor function returned by Run function (utility.ts)
Lazyt3ch Jan 15, 2023
f8fac3e
refactor ReplaceNativeSeparator function (utility.ts)
Lazyt3ch Jan 15, 2023
5869175
refactor ReplaceNativeSeparator function (utility.ts)
Lazyt3ch Jan 15, 2023
63e9234
Merge branch 'master' of https://github.com/Lazyt3ch/node-7z-archive
Lazyt3ch Jan 15, 2023
25fce7f
fix line indent (utility.ts)
Lazyt3ch Jan 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@ binaries
# VS Code
.vs/
.vscode/
win32/
.nyc_output/

# 7-Zip
win32/
other32/
97 changes: 37 additions & 60 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,19 @@ 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);
});
const executables = ['7z', '7za']; // Two or more items
let position = 0;
const runner = () => Run(executables[position], 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 (position === executables.length - 1) return reject(err);
console.error(archive + ' failed using `' + executables[position] +
'`, retrying with `' + executables[position + 1] + '`.');
position++;
runner();
});
return runner();
}

/**
Expand Down Expand Up @@ -124,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();
});
});

Expand Down Expand Up @@ -339,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 = 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 === 0 ? spec : args))
.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();
});
});

Expand Down
65 changes: 16 additions & 49 deletions src/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
};

/**
Expand All @@ -34,37 +29,18 @@ 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(' ');
};

/**
* @param {string} path A path with the native directory separator.
* @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'), '/');
};

/**
Expand Down Expand Up @@ -103,15 +79,10 @@ 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) {
const arg = command.replace(/(\/|\\)/g, sep);
args.push(normalize(arg));
}

// Special treatment for the output switch because it is exposed as a
Expand All @@ -121,12 +92,8 @@ 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);
const arg = output[0].replace(/(\/|\\|")/g, (match) => match === '"' ? '' : sep);
args.push(normalize(arg));
}

if (switches.files) {
Expand Down