Skip to content

Commit

Permalink
cli: convert 'fields' param to array, add coerce to flatten multiple …
Browse files Browse the repository at this point in the history
…params

types: add types for cli params
  • Loading branch information
ikreymer committed Nov 7, 2024
1 parent 3eb1784 commit 9b5b46e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
27 changes: 17 additions & 10 deletions src/commands/args.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { DEFAULT_CDX_FIELDS, DEFAULT_FIELDS } from "../lib/indexer";
import type yargs from "yargs";

const coerce = (array: string[]): string[] => {
return array.flatMap((v) => v.split(",")).filter((x) => !!x);
};

export const indexCommandArgs = (yarg: yargs.Argv) => {
return yarg
.positional("filenames", {
Expand All @@ -11,14 +16,15 @@ export const indexCommandArgs = (yarg: yargs.Argv) => {
.option("fields", {
alias: "f",
describe: "fields to include in index",
type: "string",
type: "array",
default: DEFAULT_FIELDS,
coerce,
});
};

//export type IndexCommandArgs = Awaited<typeof indexCommandArgs.argv>;
// todo: fix types?
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type IndexCommandArgs = any;
export type IndexCommandArgs = Awaited<
ReturnType<typeof indexCommandArgs>["argv"]
>;

export const cdxIndexCommandArgs = (yarg: yargs.Argv) => {
return yarg
Expand Down Expand Up @@ -46,11 +52,12 @@ export const cdxIndexCommandArgs = (yarg: yargs.Argv) => {
.option("fields", {
alias: "f",
describe: "fields to include in index",
type: "string",
type: "array",
default: DEFAULT_CDX_FIELDS,
coerce,
});
};

//export type CdxIndexCommandArgs = Awaited<typeof cdxIndexCommandArgs.argv>;
// todo: fix types?
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type CdxIndexCommandArgs = any; //ReturnType<cdxIndexCommandArgs>;
export type CdxIndexCommandArgs = Awaited<
ReturnType<typeof cdxIndexCommandArgs>["argv"]
>;
3 changes: 1 addition & 2 deletions src/lib/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ abstract class BaseIndexer {
) {
this.opts = opts;
if (opts.fields) {
this.fields =
typeof opts.fields === "string" ? opts.fields.split(",") : opts.fields;
this.fields = opts.fields;
this.reqFields = this.fields.filter((x) => isRequestHeader(x));
} else {
this.fields = defaultFields;
Expand Down
2 changes: 1 addition & 1 deletion test/testIndexer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ com,example,some:8080)/ 20200405201750 {"url":"http://some.example.com:8080/","m

test("test custom Indexer", async () => {
const entries = [];
const indexer = new Indexer({ fields: "warc-type,warc-target-uri" });
const indexer = new Indexer({ fields: ["warc-type", "warc-target-uri"] });

const files = [
{
Expand Down

0 comments on commit 9b5b46e

Please sign in to comment.