Skip to content

Commit 1dc47e3

Browse files
committed
extract confirmation logic in commands to function
1 parent 51b642f commit 1dc47e3

File tree

6 files changed

+51
-52
lines changed

6 files changed

+51
-52
lines changed

src/commands/environment/clean/clean.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import chalk from "chalk";
2-
31
import { logError, LogOptions } from "../../../log.js";
42
import {
53
CleanEntityChoices,
@@ -8,7 +6,7 @@ import {
86
CleanEnvironmentParams,
97
} from "../../../modules/backupRestore/clean.js";
108
import { resolveIncludeExcludeCliParams } from "../../../modules/backupRestore/utils/includeExclude.js";
11-
import { requestConfirmation } from "../../../modules/sync/utils/consoleHelpers.js";
9+
import { checkConfirmation } from "../../../modules/sync/utils/consoleHelpers.js";
1210
import { RegisterCommand } from "../../../types/yargs.js";
1311
import { createClient } from "../../../utils/client.js";
1412
import { simplifyErrors } from "../../../utils/error.js";
@@ -74,16 +72,12 @@ type CleanEnvironmentCliParams =
7472
const cleanEnvironmentCli = async (
7573
params: CleanEnvironmentCliParams,
7674
): Promise<void> => {
77-
const warningMessage = chalk.yellow(
78-
`⚠ Running this operation may result in irreversible changes to the content in environment ${params.environmentId}.\n\nOK to proceed y/n? (suppress this message with -s parameter)\n`,
79-
);
80-
81-
const confirmed = !params.skipWarning ? await requestConfirmation(warningMessage) : true;
82-
83-
if (!confirmed) {
84-
logError(params, chalk.red("Operation aborted."));
85-
process.exit(1);
86-
}
75+
await checkConfirmation({
76+
message:
77+
`⚠ Running this operation may result in irreversible changes to the content in environment ${params.environmentId}.\n\nOK to proceed y/n? (suppress this message with -s parameter)\n`,
78+
skipConfirmation: params.skipWarning,
79+
logOptions: params,
80+
});
8781

8882
const client = createClient({
8983
environmentId: params.environmentId,

src/commands/migrateContent/run/run.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import chalk from "chalk";
21
import { match, P } from "ts-pattern";
32

4-
import { logError, logInfo, LogOptions } from "../../../log.js";
3+
import { logError, LogOptions } from "../../../log.js";
54
import {
65
migrateContentRunInternal,
76
MigrateContentRunParams,
87
} from "../../../modules/migrateContent/migrateContentRun.js";
9-
import { requestConfirmation } from "../../../modules/sync/utils/consoleHelpers.js";
8+
import { checkConfirmation } from "../../../modules/sync/utils/consoleHelpers.js";
109
import { RegisterCommand } from "../../../types/yargs.js";
1110
import { simplifyErrors } from "../../../utils/error.js";
1211
import { omit } from "../../../utils/object.js";
@@ -146,17 +145,13 @@ const migrateContentRunCli = async (params: MigrateContentRunCliParams) => {
146145
const resolvedParams = resolveParams(params);
147146

148147
migrateContentRunInternal(resolvedParams, "migrate-content-run", async () => {
149-
const warningMessage = chalk.yellow(
150-
`⚠ Running this operation may result in irreversible changes to the content in environment ${params.targetEnvironmentId}.
148+
await checkConfirmation({
149+
message:
150+
`⚠ Running this operation may result in irreversible changes to the content in environment ${params.targetEnvironmentId}.
151151
OK to proceed y/n? (suppress this message with --sw parameter)\n`,
152-
);
153-
154-
const confirmed = !params.skipConfirmation ? await requestConfirmation(warningMessage) : true;
155-
156-
if (!confirmed) {
157-
logInfo(params, "standard", chalk.red("Operation aborted."));
158-
process.exit(1);
159-
}
152+
skipConfirmation: params.skipConfirmation,
153+
logOptions: params,
154+
});
160155
});
161156
};
162157

src/commands/migrateContent/snapshot/snapshot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ const resolveParams = (params: MigrateContentSnapshotCliParams): MigrateContentS
142142
.otherwise(() => {
143143
logError(
144144
params,
145-
"You need to provide exactly one from parameters: --items or --items with --depth, --filter, --byTypesCodenames, --last with --sourceDeliveryPeviewKey",
145+
"You need to provide exactly one from parameters: --items or --items with --depth, --filter, --byTypesCodenames, --last with --sourceDeliveryPreviewKey",
146146
);
147147
process.exit(1);
148148
});

src/commands/migrations/run/run.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import chalk from "chalk";
21
import { match, P } from "ts-pattern";
32

43
import { logError, logInfo, LogOptions } from "../../../log.js";
54
import { RunMigrationsParams, withMigrationsToRun } from "../../../modules/migrations/run.js";
65
import { WithErr } from "../../../modules/migrations/utils/errUtils.js";
76
import { executeMigrations } from "../../../modules/migrations/utils/migrationUtils.js";
87
import { parseRange } from "../../../modules/migrations/utils/rangeUtils.js";
9-
import { requestConfirmation } from "../../../modules/sync/utils/consoleHelpers.js";
8+
import { checkConfirmation } from "../../../modules/sync/utils/consoleHelpers.js";
109
import { RegisterCommand } from "../../../types/yargs.js";
1110
import { createClient } from "../../../utils/client.js";
1211
import { simplifyErrors } from "../../../utils/error.js";
@@ -153,15 +152,12 @@ const runMigrationsCli = async (params: RunMigrationsCliParams) => {
153152
await withMigrationsToRun(resolvedParams.value, async migrations => {
154153
const operation = resolvedParams.value.rollback ? "rollback" : "run";
155154

156-
const warningMessage = chalk.yellow(
157-
`⚠ Running this operation may result in irreversible changes to your environment ${params.environmentId}.\nOK to proceed y/n? (suppress this message with --skipConfirmation parameter)\n`,
158-
);
159-
160-
const confirmed = !params.skipConfirmation ? await requestConfirmation(warningMessage) : true;
161-
162-
if (!confirmed) {
163-
process.exit(0);
164-
}
155+
await checkConfirmation({
156+
message:
157+
`⚠ Running this operation may result in irreversible changes to your environment ${params.environmentId}.\nOK to proceed y/n? (suppress this message with --skipConfirmation parameter)\n`,
158+
skipConfirmation: params.skipConfirmation,
159+
logOptions: params,
160+
});
165161

166162
const migrationsStatus = await executeMigrations(migrations, client, {
167163
operation,

src/commands/sync/run/run.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import chalk from "chalk";
21
import { match, P } from "ts-pattern";
32

4-
import { logError, logInfo, LogOptions } from "../../../log.js";
3+
import { logError, LogOptions } from "../../../log.js";
54
import { syncEntityChoices, SyncEntityName } from "../../../modules/sync/constants/entities.js";
65
import { printDiff } from "../../../modules/sync/printDiff.js";
76
import { SyncEntities, syncRunInternal, SyncRunParams } from "../../../modules/sync/syncRun.js";
8-
import { requestConfirmation } from "../../../modules/sync/utils/consoleHelpers.js";
7+
import { checkConfirmation } from "../../../modules/sync/utils/consoleHelpers.js";
98
import { RegisterCommand } from "../../../types/yargs.js";
109
import { simplifyErrors } from "../../../utils/error.js";
1110

@@ -91,18 +90,14 @@ const syncRunCli = async (params: SyncModelRunCliParams) => {
9190
await syncRunInternal(resolvedParams, `sync-${commandName}`, async (diffModel) => {
9291
printDiff(diffModel, new Set(params.entities), params);
9392

94-
const warningMessage = chalk.yellow(
95-
`⚠ Running this operation may result in irreversible changes to the content in environment ${params.targetEnvironmentId}. Mentioned changes might include:
93+
await checkConfirmation({
94+
message:
95+
`⚠ Running this operation may result in irreversible changes to the content in environment ${params.targetEnvironmentId}. Mentioned changes might include:
9696
- Removing content due to element deletion
9797
OK to proceed y/n? (suppress this message with --sw parameter)\n`,
98-
);
99-
100-
const confirmed = !params.skipConfirmation ? await requestConfirmation(warningMessage) : true;
101-
102-
if (!confirmed) {
103-
logInfo(params, "standard", chalk.red("Operation aborted."));
104-
process.exit(1);
105-
}
98+
skipConfirmation: params.skipConfirmation,
99+
logOptions: params,
100+
});
106101
});
107102
} catch (e) {
108103
logError(params, JSON.stringify(e, Object.getOwnPropertyNames(e)));

src/modules/sync/utils/consoleHelpers.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import readline from "node:readline";
22

3-
export const requestConfirmation = async (message: string) => {
3+
import chalk from "chalk";
4+
5+
import { logInfo, LogOptions } from "../../../log.js";
6+
7+
const requestConfirmation = async (message: string) => {
48
const rl = readline.createInterface({
59
input: process.stdin,
610
output: process.stdout,
@@ -13,3 +17,18 @@ export const requestConfirmation = async (message: string) => {
1317
});
1418
});
1519
};
20+
21+
export const checkConfirmation = async (options: {
22+
message: string;
23+
skipConfirmation: boolean | undefined;
24+
logOptions: LogOptions;
25+
}) => {
26+
const warningMessage = chalk.yellow(options.message);
27+
28+
const confirmed = !options.skipConfirmation ? await requestConfirmation(warningMessage) : true;
29+
30+
if (!confirmed) {
31+
logInfo(options.logOptions, "standard", chalk.red("Operation aborted."));
32+
process.exit(0);
33+
}
34+
};

0 commit comments

Comments
 (0)