From 4b586c78678d3c95ad8db486aa4dd8232063d9bc Mon Sep 17 00:00:00 2001 From: Ivan Kiral Date: Mon, 26 Aug 2024 14:35:45 +0200 Subject: [PATCH] extract resolving include exclude params from cli --- src/commands/clean/clean.ts | 12 ++++++------ src/commands/importExport/export.ts | 13 ++++++------- src/commands/importExport/import.ts | 13 ++++++------- .../importExport/utils/includeExclude.ts | 17 +++++++++++++---- 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/commands/clean/clean.ts b/src/commands/clean/clean.ts index a18c6878..e99a731c 100644 --- a/src/commands/clean/clean.ts +++ b/src/commands/clean/clean.ts @@ -1,5 +1,4 @@ import chalk from "chalk"; -import { match, P } from "ts-pattern"; import { logError, LogOptions } from "../../log.js"; import { @@ -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"; @@ -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), +}); diff --git a/src/commands/importExport/export.ts b/src/commands/importExport/export.ts index 6d87184e..9bae1530 100644 --- a/src/commands/importExport/export.ts +++ b/src/commands/importExport/export.ts @@ -1,5 +1,3 @@ -import { match, P } from "ts-pattern"; - import { logError, LogOptions } from "../../log.js"; import { ExportEntityChoices, @@ -7,9 +5,11 @@ import { 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"; @@ -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), +}); diff --git a/src/commands/importExport/import.ts b/src/commands/importExport/import.ts index 04354de4..2bc1fe86 100644 --- a/src/commands/importExport/import.ts +++ b/src/commands/importExport/import.ts @@ -1,5 +1,3 @@ -import { match, P } from "ts-pattern"; - import { logError, LogOptions } from "../../log.js"; import { ImportEntityChoices, @@ -7,9 +5,11 @@ import { 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"; @@ -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), +}); diff --git a/src/modules/importExport/utils/includeExclude.ts b/src/modules/importExport/utils/includeExclude.ts index 1427180e..0cf6c8a4 100644 --- a/src/modules/importExport/utils/includeExclude.ts +++ b/src/modules/importExport/utils/includeExclude.ts @@ -1,12 +1,21 @@ import { match, P } from "ts-pattern"; -export type IncludeExclude = - | { include: ReadonlyArray; exclude?: undefined } - | { exclude: ReadonlyArray; include?: undefined } // Exclude is optional because it's possible to use the type without - | {}; +export type IncludeExclude = Readonly< + | { include?: ReadonlyArray; exclude?: undefined } + | { exclude?: ReadonlyArray; include?: undefined } +>; export const includeExcludePredicate = (params: IncludeExclude) => (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 = ( + params: { include?: ReadonlyArray; exclude?: ReadonlyArray }, +): IncludeExclude => + match(params) + .returnType>() + .with({ include: P.nonNullable }, ({ include }) => ({ ...params, include, exclude: undefined })) + .with({ exclude: P.nonNullable }, ({ exclude }) => ({ ...params, exclude, include: undefined })) + .otherwise(() => ({ ...params, include: undefined, exclude: undefined }));