Skip to content

Commit

Permalink
extract resolving include exclude params from cli
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanKiral committed Aug 26, 2024
1 parent 9ef22d8 commit 4b586c7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
12 changes: 6 additions & 6 deletions src/commands/clean/clean.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import chalk from "chalk";
import { match, P } from "ts-pattern";

import { logError, LogOptions } from "../../log.js";
import {
Expand All @@ -8,9 +7,11 @@ import {
cleanEnvironmentInternal,
CleanEnvironmentParams,
} from "../../modules/importExport/clean.js";
import { resolveIncludeExcludeCliParams } from "../../modules/importExport/utils/includeExclude.js";
import { requestConfirmation } from "../../modules/sync/utils/consoleHelpers.js";
import { RegisterCommand } from "../../types/yargs.js";
import { createClient } from "../../utils/client.js";
import { omit } from "../../utils/object.js";

export const commandName = "clean";

Expand Down Expand Up @@ -99,8 +100,7 @@ const handleError = (
process.exit(1);
};

const resolveParams = (params: CleanEnvironmentCliParams): CleanEnvironmentParams =>
match(params)
.with({ include: P.nonNullable }, ({ include }) => ({ ...params, include, exclude: undefined }))
.with({ exclude: P.nonNullable }, ({ exclude }) => ({ ...params, exclude, include: undefined }))
.otherwise(() => params);
const resolveParams = (params: CleanEnvironmentCliParams): CleanEnvironmentParams => ({
...omit(params, ["include", "exclude"]),
...resolveIncludeExcludeCliParams(params),
});
13 changes: 6 additions & 7 deletions src/commands/importExport/export.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { match, P } from "ts-pattern";

import { logError, LogOptions } from "../../log.js";
import {
ExportEntityChoices,
exportEntityChoices,
exportEnvironmentInternal,
ExportEnvironmentParams,
} from "../../modules/importExport/export.js";
import { resolveIncludeExcludeCliParams } from "../../modules/importExport/utils/includeExclude.js";
import { RegisterCommand } from "../../types/yargs.js";
import { createClient } from "../../utils/client.js";
import { simplifyErrors } from "../../utils/error.js";
import { omit } from "../../utils/object.js";

const commandName = "export";

Expand Down Expand Up @@ -78,8 +78,7 @@ const exportEnvironmentCli = async (params: ExportEnvironmentCliParams): Promise
}
};

const resolveParams = (params: ExportEnvironmentCliParams): ExportEnvironmentParams =>
match(params)
.with({ include: P.nonNullable }, ({ include }) => ({ ...params, include, exclude: undefined }))
.with({ exclude: P.nonNullable }, ({ exclude }) => ({ ...params, exclude, include: undefined }))
.otherwise(() => params);
const resolveParams = (params: ExportEnvironmentCliParams): ExportEnvironmentParams => ({
...omit(params, ["include", "exclude"]),
...resolveIncludeExcludeCliParams(params),
});
13 changes: 6 additions & 7 deletions src/commands/importExport/import.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { match, P } from "ts-pattern";

import { logError, LogOptions } from "../../log.js";
import {
ImportEntityChoices,
importEntityChoices,
importEnvironmentInternal,
ImportEnvironmentParams,
} from "../../modules/importExport/import.js";
import { resolveIncludeExcludeCliParams } from "../../modules/importExport/utils/includeExclude.js";
import { RegisterCommand } from "../../types/yargs.js";
import { createClient } from "../../utils/client.js";
import { simplifyErrors } from "../../utils/error.js";
import { omit } from "../../utils/object.js";

const commandName = "import";

Expand Down Expand Up @@ -81,8 +81,7 @@ const importEnvironmentCli = async (params: ImportEnvironmentCliParams) => {
}
};

const resolveParams = (params: ImportEnvironmentCliParams): ImportEnvironmentParams =>
match(params)
.with({ include: P.nonNullable }, ({ include }) => ({ ...params, include, exclude: undefined }))
.with({ exclude: P.nonNullable }, ({ exclude }) => ({ ...params, exclude, include: undefined }))
.otherwise(() => params);
const resolveParams = (params: ImportEnvironmentCliParams): ImportEnvironmentParams => ({
...omit(params, ["include", "exclude"]),
...resolveIncludeExcludeCliParams(params),
});
17 changes: 13 additions & 4 deletions src/modules/importExport/utils/includeExclude.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { match, P } from "ts-pattern";

export type IncludeExclude<Choices extends string> =
| { include: ReadonlyArray<Choices>; exclude?: undefined }
| { exclude: ReadonlyArray<Choices>; include?: undefined } // Exclude is optional because it's possible to use the type without
| {};
export type IncludeExclude<Choices extends string> = Readonly<
| { include?: ReadonlyArray<Choices>; exclude?: undefined }
| { exclude?: ReadonlyArray<Choices>; include?: undefined }
>;

export const includeExcludePredicate = (params: IncludeExclude<string>) => (entity: Readonly<{ name: string }>) =>
match(params)
.with({ include: P.nonNullable }, ({ include }) => include.includes(entity.name))
.with({ exclude: P.nonNullable }, ({ exclude }) => !exclude.includes(entity.name))
.otherwise(() => true);

export const resolveIncludeExcludeCliParams = <Choices extends string>(
params: { include?: ReadonlyArray<Choices>; exclude?: ReadonlyArray<Choices> },
): IncludeExclude<Choices> =>
match(params)
.returnType<IncludeExclude<Choices>>()
.with({ include: P.nonNullable }, ({ include }) => ({ ...params, include, exclude: undefined }))
.with({ exclude: P.nonNullable }, ({ exclude }) => ({ ...params, exclude, include: undefined }))
.otherwise(() => ({ ...params, include: undefined, exclude: undefined }));

0 comments on commit 4b586c7

Please sign in to comment.