Skip to content

Commit 35f3763

Browse files
committed
make several functions more DRY via reusing global onProgress function (index.ts)
1 parent 7715d6b commit 35f3763

File tree

1 file changed

+21
-120
lines changed

1 file changed

+21
-120
lines changed

src/index.ts

Lines changed: 21 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ import { Files, ReplaceNativeSeparator, Run } from './utility.js';
55
import { createSfx } from './createSfx.js';
66
import { isWindows } from 'node-sys';
77

8+
/**
9+
* When a stdout is emitted, parse each line and search for a pattern. When
10+
* the pattern is found, extract the file (or directory) name from it and
11+
* pass it to an array. Finally returns this array.
12+
*/
13+
const onProgress = (firstChar: string) =>
14+
function(data: string): string[] {
15+
return data.split('\n')
16+
.filter((line) => line.startsWith(firstChar))
17+
.map((line) => ReplaceNativeSeparator(line.slice(2)));
18+
}
19+
820
function retry(
921
command: string | undefined,
1022
options: {},
@@ -66,16 +78,7 @@ export const createArchive =
6678
reject: (reason: any) => void,
6779
progress: (arg0: any) => any
6880
) {
69-
/**
70-
* When a stdout is emitted, parse each line and search for a pattern.When
71-
* the pattern is found, extract the file (or directory) name from it and
72-
* pass it to an array. Finally returns this array.
73-
*/
74-
function onprogress(data: string) {
75-
return data.split('\n')
76-
.filter((line) => line.startsWith('+'))
77-
.map((line) => ReplaceNativeSeparator(line.slice(2)));
78-
}
81+
const onprogress = onProgress('+');
7982

8083
// Convert array of files into a string if needed.
8184
files = Files(files);
@@ -170,24 +173,7 @@ export const extractArchive =
170173
reject: (reason: any) => void,
171174
progress: (arg0: any) => any
172175
) {
173-
/**
174-
* When a stdout is emitted, parse each line and search for a pattern.When
175-
* the pattern is found, extract the file (or directory) name from it and
176-
* pass it to an array. Finally returns this array.
177-
*/
178-
function onprogress(data: string) {
179-
let entries: string[] = [];
180-
data.split('\n').forEach(function (line) {
181-
if (line.substr(0, 1) === '-') {
182-
entries.push(
183-
ReplaceNativeSeparator(
184-
line.substr(2, line.length)
185-
)
186-
);
187-
}
188-
});
189-
return entries;
190-
}
176+
const onprogress = onProgress('-');
191177

192178
// Create a string that can be parsed by `run`.
193179
let command = 'e "' + filepath + '" -o"' + dest + '" ';
@@ -233,25 +219,8 @@ export const fullArchive =
233219
reject: (reason: any) => void,
234220
progress: (arg0: any) => any
235221
) {
236-
/**
237-
* When a stdout is emitted, parse each line and search for a pattern.When
238-
* the pattern is found, extract the file (or directory) name from it and
239-
* pass it to an array. Finally returns this array.
240-
*/
241-
function onprogress(data: string) {
242-
let entries: string[] = [];
243-
data.split('\n').forEach(function (line) {
244-
if (line.substr(0, 1) === '-') {
245-
entries.push(
246-
ReplaceNativeSeparator(
247-
line.substr(2, line.length)
248-
)
249-
);
250-
}
251-
});
252-
return entries;
253-
}
254-
222+
const onprogress = onProgress('-');
223+
255224
// Create a string that can be parsed by `run`.
256225
let command = 'x "' + filepath + '" -o"' + dest + '" ';
257226
return retry(
@@ -425,25 +394,8 @@ export const onlyArchive =
425394
files: files,
426395
});
427396

428-
/**
429-
* When a stdout is emitted, parse each line and search for a pattern.When
430-
* the pattern is found, extract the file (or directory) name from it and
431-
* pass it to an array. Finally returns this array.
432-
*/
433-
function onprogress(data: string) {
434-
let entries: string[] = [];
435-
data.split('\n').forEach(function (line) {
436-
if (line.substr(0, 1) === '-') {
437-
entries.push(
438-
ReplaceNativeSeparator(
439-
line.substr(2, line.length)
440-
)
441-
);
442-
}
443-
});
444-
return entries;
445-
}
446-
397+
const onprogress = onProgress('-');
398+
447399
// Create a string that can be parsed by `run`.
448400
let command = 'e "' + filepath + '" -o"' + dest + '"';
449401
// Start the command
@@ -488,24 +440,7 @@ export const renameArchive =
488440
reject: (reason: any) => void,
489441
progress: (arg0: any) => any
490442
) {
491-
/**
492-
* When a stdout is emitted, parse each line and search for a pattern.When
493-
* the pattern is found, extract the file (or directory) name from it and
494-
* pass it to an array. Finally returns this array.
495-
*/
496-
function onprogress(data: string) {
497-
let entries: string[] = [];
498-
data.split('\n').forEach(function (line) {
499-
if (line.substr(0, 1) === 'U') {
500-
entries.push(
501-
ReplaceNativeSeparator(
502-
line.substr(2, line.length)
503-
)
504-
);
505-
}
506-
});
507-
return entries;
508-
}
443+
const onprogress = onProgress('U');
509444

510445
// Convert array of files into a string if needed.
511446
files = Files(files);
@@ -547,24 +482,7 @@ export const testArchive =
547482
reject: (reason: any) => void,
548483
progress: (arg0: any) => any
549484
) {
550-
/**
551-
* When a stdout is emitted, parse each line and search for a pattern.When
552-
* the pattern is found, extract the file (or directory) name from it and
553-
* pass it to an array. Finally returns this array.
554-
*/
555-
function onprogress(data: string) {
556-
let entries: string[] = [];
557-
data.split('\n').forEach(function (line) {
558-
if (line.substr(0, 1) === 'T') {
559-
entries.push(
560-
ReplaceNativeSeparator(
561-
line.substr(2, line.length)
562-
)
563-
);
564-
}
565-
});
566-
return entries;
567-
}
485+
const onprogress = onProgress('T');
568486

569487
// Create a string that can be parsed by `run`.
570488
let command = 't "' + filepath + '"';
@@ -610,24 +528,7 @@ export const updateArchive =
610528
reject: (reason: any) => void,
611529
progress: (arg0: any) => any
612530
) {
613-
/**
614-
* When a stdout is emitted, parse each line and search for a pattern.When
615-
* the pattern is found, extract the file (or directory) name from it and
616-
* pass it to an array. Finally returns this array.
617-
*/
618-
function onprogress(data: string) {
619-
let entries: string[] = [];
620-
data.split('\n').forEach(function (line) {
621-
if (line.substr(0, 1) === 'U') {
622-
entries.push(
623-
ReplaceNativeSeparator(
624-
line.substr(2, line.length)
625-
)
626-
);
627-
}
628-
});
629-
return entries;
630-
}
531+
const onprogress = onProgress('U');
631532

632533
// Convert array of files into a string if needed.
633534
files = Files(files);

0 commit comments

Comments
 (0)