Skip to content

Commit

Permalink
new prefix option + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bn-l committed Apr 4, 2024
1 parent 67745c1 commit 9e57d43
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 12 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ List files. Useful as a dry run and with fzf. Does not
download. Will show show conflicts for the current
working directory if -d / --dest is not specified.

### -p, --prefix

Append the owner/repo prefix to the path in list output.
This is useful for feeding back into ghex.

### -c, --conflicts-only

Only show conflicts when listing.
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
},
"dependencies": {
"chalk": "^5.3.0",
"github-extractor": "^0.0.28",
"github-extractor": "^0.0.29",
"indent-string": "^5.0.0",
"meow": "^13.2.0",
"ora": "^8.0.1",
Expand Down
7 changes: 7 additions & 0 deletions source/cli.mts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const { name, docsUrl } = meow({ importMeta: import.meta }).pkg as { name: strin
export enum Option {
dest = "dest",
list = "list",
prefix = "prefix",
colors = "colors",
caseInsensitive = "caseInsensitive",
conflictsOnly = "conflictsOnly",
Expand All @@ -47,6 +48,8 @@ export const helpText = `
-l, --list List files. Useful as a dry run and with fzf. Does not
download. Will show show conflicts for the current
working directory if -d / --dest is not specified.
-p, --prefix Append the owner/repo prefix to the path in list output
Useful for feeding back into ${ name }.
-c, --conflicts-only Only show conflicts when listing.
-d, --dest <folder> Destination folder. Defaults to the current directory.
-i, --case-insensitive Ignores case when checking for conflicts. Default is
Expand Down Expand Up @@ -82,6 +85,10 @@ export function getCli(argv?: string[]) {
type: "boolean",
shortFlag: "l",
},
[Option.prefix]: {
type: "boolean",
shortFlag: "p",
},
[Option.colors]: {
type: "boolean",
default: !noColor(),
Expand Down
10 changes: 8 additions & 2 deletions source/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import wrapAnsi from "wrap-ansi";
// copied to the README.

// Todo:
// - list with or without owner/repo prefix
// - Quick SVG based video.
// - Longer playable .cast for website.

// - Include example of getting the fzf install script with ghex in the main video or
// an alt.
// - create video demo and add to readme
// - Show usages with fzf--upload ascii svg recording to docs/cli/ (by way of the README)

Expand All @@ -27,14 +33,14 @@ try {
const { input: paths, flags } = getCli();
c.showColor = flags.colors;

let { list: listMode = false, quiet = false, dest, keepIf, caseInsensitive = false, conflictsOnly = false } = flags;
let { list: listMode = false, quiet = false, dest, keepIf, caseInsensitive = false, conflictsOnly = false, prefix = false } = flags;

const ownerGrouping: OwnerGroup = groupByOwner({ paths });
const parsedGroups: ParsedGroup[] = parseOwnerGroups({ ownerGrouping, listMode, caseInsensitive });

if (!listMode && !quiet) spinner = ora("Downloading...").start();

await executeParsedGroups({ conflictsOnly, listMode, parsedGroups, dest, keepIf, quiet });
await executeParsedGroups({ conflictsOnly, listMode, parsedGroups, dest, keepIf, quiet, prefix });

if (spinner) {
spinner.succeed(`Successfully downloaded to file://${ pathe.resolve(dest) }`);
Expand Down
12 changes: 7 additions & 5 deletions source/main.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { c } from "./cli.mjs";

import pico from "picomatch";
import GithubExtractor, { Typo } from "github-extractor";
import GithubExtractor, { Typo, ListOptions } from "github-extractor";
import indentString from "indent-string";


Expand Down Expand Up @@ -133,15 +133,17 @@ function handleTypos(typos: Typo[], quiet: boolean) {
}

export async function executeParsedGroups(
{ listMode, conflictsOnly, parsedGroups, dest, keepIf, quiet }:
{ listMode: boolean; conflictsOnly: boolean; parsedGroups: ParsedGroup[]; dest: string; keepIf: string | undefined; quiet: boolean }
{ listMode, conflictsOnly, parsedGroups, dest, keepIf, quiet, prefix }:
{ listMode: boolean; conflictsOnly: boolean; parsedGroups: ParsedGroup[]; dest: string; keepIf: string | undefined; quiet: boolean; prefix?: boolean }

): Promise<void> {
for (const group of parsedGroups) {

if (listMode) {

const opts = { conflictsOnly, dest, match: group.regex! };
const outPrefix = prefix ?
`${ group.gheInstance.owner }/${ group.gheInstance.repo }/` :
"";
const opts: ListOptions = { conflictsOnly, dest, match: group.regex!, streamOptions: { prefix: outPrefix } };
await group.gheInstance.list(opts);
continue;
}
Expand Down
20 changes: 20 additions & 0 deletions test/main.mocked.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,24 @@ describe("Correct selected files given to GithubExtractor.downloadTo", async ()
expect(stubbedDownloadTo.callCount).toBe(2);
});

it("It correctly supplies prefix args with owner/repo when prefix is true", async () => {

const prefix = true;
listMode = true;

ownerGrouping = ownerGrouping = {
owner1: { repo1: [ "path1.txt", "path2.txt" ], repo2: [ "path1.txt", "path2.txt" ] },
};

const parsedGroups = parseOwnerGroups({ ownerGrouping, listMode, caseInsensitive });
const stubbedList = sinon.stub(GithubExtractor.prototype, "list").resolves([]);

await executeParsedGroups({conflictsOnly, quiet, listMode, parsedGroups, dest: TEMP_DIR, keepIf, prefix });

expect(stubbedList.callCount).toBe(2);
expect(stubbedList.firstCall.args?.[0]?.streamOptions?.prefix).toBe("owner1/repo1/");
expect(stubbedList.secondCall.args?.[0]?.streamOptions?.prefix).toBe("owner1/repo2/");

});

});

0 comments on commit 9e57d43

Please sign in to comment.