Skip to content

Commit

Permalink
Reusable function to append suffix parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
steveukx committed Jun 26, 2023
1 parent 2ab1936 commit 516736f
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions simple-git/src/lib/plugins/suffix-paths.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,30 @@ export function suffixPathsPlugin(): SimpleGitPlugin<'spawn.args'> {
type: 'spawn.args',
action(data) {
const prefix: string[] = [];
const suffix: string[] = [];
let suffixFound: boolean = false;
let suffix: undefined | string[];
function append(args: string[]) {
(suffix = suffix || []).push(...args);
}

for (let i = 0; i < data.length; i++) {
const param = data[i];

if (isPathSpec(param)) {
suffixFound = true;
suffix.push(...toPaths(param));
append(toPaths(param));
continue;
}

if (param === '--') {
suffixFound = true;
suffix.push(
...data
.slice(i + 1)
.flatMap((item) => (isPathSpec(item) && toPaths(item)) || item)
append(
data.slice(i + 1).flatMap((item) => (isPathSpec(item) && toPaths(item)) || item)
);
break;
}

prefix.push(param);
}

return suffixFound ? [...prefix, '--', ...suffix.map(String)] : prefix;
return !suffix ? prefix : [...prefix, '--', ...suffix.map(String)];
},
};
}

0 comments on commit 516736f

Please sign in to comment.