diff --git a/package-lock.json b/package-lock.json index 5a0c5155..36a206b8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@kontent-ai/data-ops", - "version": "2.1.1", + "version": "2.1.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@kontent-ai/data-ops", - "version": "2.1.1", + "version": "2.1.2", "license": "MIT", "dependencies": { "@kontent-ai/core-sdk": "^10.8.0", diff --git a/package.json b/package.json index c22f259e..6d0adc43 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kontent-ai/data-ops", - "version": "2.1.1", + "version": "2.1.2", "description": "", "type": "module", "scripts": { diff --git a/src/commands/environment/clean/clean.ts b/src/commands/environment/clean/clean.ts index 7ef7e9bf..da1ac13f 100644 --- a/src/commands/environment/clean/clean.ts +++ b/src/commands/environment/clean/clean.ts @@ -1,5 +1,3 @@ -import chalk from "chalk"; - import { logError, LogOptions } from "../../../log.js"; import { CleanEntityChoices, @@ -8,7 +6,7 @@ import { CleanEnvironmentParams, } from "../../../modules/backupRestore/clean.js"; import { resolveIncludeExcludeCliParams } from "../../../modules/backupRestore/utils/includeExclude.js"; -import { requestConfirmation } from "../../../modules/sync/utils/consoleHelpers.js"; +import { checkConfirmation } from "../../../modules/sync/utils/consoleHelpers.js"; import { RegisterCommand } from "../../../types/yargs.js"; import { createClient } from "../../../utils/client.js"; import { simplifyErrors } from "../../../utils/error.js"; @@ -74,16 +72,12 @@ type CleanEnvironmentCliParams = const cleanEnvironmentCli = async ( params: CleanEnvironmentCliParams, ): Promise => { - const warningMessage = chalk.yellow( - `⚠ 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`, - ); - - const confirmed = !params.skipWarning ? await requestConfirmation(warningMessage) : true; - - if (!confirmed) { - logError(params, chalk.red("Operation aborted.")); - process.exit(1); - } + await checkConfirmation({ + message: + `⚠ 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`, + skipConfirmation: params.skipWarning, + logOptions: params, + }); const client = createClient({ environmentId: params.environmentId, diff --git a/src/commands/migrateContent/run/run.ts b/src/commands/migrateContent/run/run.ts index d820d249..aebda387 100644 --- a/src/commands/migrateContent/run/run.ts +++ b/src/commands/migrateContent/run/run.ts @@ -2,9 +2,10 @@ import { match, P } from "ts-pattern"; import { logError, LogOptions } from "../../../log.js"; import { - migrateContentRunIntenal, + migrateContentRunInternal, MigrateContentRunParams, } from "../../../modules/migrateContent/migrateContentRun.js"; +import { checkConfirmation } from "../../../modules/sync/utils/consoleHelpers.js"; import { RegisterCommand } from "../../../types/yargs.js"; import { simplifyErrors } from "../../../utils/error.js"; import { omit } from "../../../utils/object.js"; @@ -143,7 +144,15 @@ type MigrateContentRunCliParams = const migrateContentRunCli = async (params: MigrateContentRunCliParams) => { const resolvedParams = resolveParams(params); - migrateContentRunIntenal(resolvedParams, "migrate-content-run"); + migrateContentRunInternal(resolvedParams, "migrate-content-run", async () => { + await checkConfirmation({ + message: + `⚠ Running this operation may result in irreversible changes to the content in environment ${params.targetEnvironmentId}. +OK to proceed y/n? (suppress this message with --sw parameter)\n`, + skipConfirmation: params.skipConfirmation, + logOptions: params, + }); + }); }; const resolveParams = (params: MigrateContentRunCliParams): MigrateContentRunParams => { @@ -177,7 +186,7 @@ const resolveParams = (params: MigrateContentRunCliParams): MigrateContentRunPar .otherwise(() => { logError( params, - "You need to provide exactly one from parameters: --items or --items with --depth, --filter, --byTypesCodenames, --last with --sourceDeliveryPeviewKey", + "You need to provide exactly one from parameters: --items or --items with --depth, --filter, --byTypesCodenames, --last with --sourceDeliveryPreviewKey", ); process.exit(1); }); diff --git a/src/commands/migrateContent/snapshot/snapshot.ts b/src/commands/migrateContent/snapshot/snapshot.ts index 68d5d1ae..0a2635ad 100644 --- a/src/commands/migrateContent/snapshot/snapshot.ts +++ b/src/commands/migrateContent/snapshot/snapshot.ts @@ -142,7 +142,7 @@ const resolveParams = (params: MigrateContentSnapshotCliParams): MigrateContentS .otherwise(() => { logError( params, - "You need to provide exactly one from parameters: --items or --items with --depth, --filter, --byTypesCodenames, --last with --sourceDeliveryPeviewKey", + "You need to provide exactly one from parameters: --items or --items with --depth, --filter, --byTypesCodenames, --last with --sourceDeliveryPreviewKey", ); process.exit(1); }); diff --git a/src/commands/migrations/run/run.ts b/src/commands/migrations/run/run.ts index 2ca8858e..386b468c 100644 --- a/src/commands/migrations/run/run.ts +++ b/src/commands/migrations/run/run.ts @@ -1,4 +1,3 @@ -import chalk from "chalk"; import { match, P } from "ts-pattern"; import { logError, logInfo, LogOptions } from "../../../log.js"; @@ -6,7 +5,7 @@ import { RunMigrationsParams, withMigrationsToRun } from "../../../modules/migra import { WithErr } from "../../../modules/migrations/utils/errUtils.js"; import { executeMigrations } from "../../../modules/migrations/utils/migrationUtils.js"; import { parseRange } from "../../../modules/migrations/utils/rangeUtils.js"; -import { requestConfirmation } from "../../../modules/sync/utils/consoleHelpers.js"; +import { checkConfirmation } from "../../../modules/sync/utils/consoleHelpers.js"; import { RegisterCommand } from "../../../types/yargs.js"; import { createClient } from "../../../utils/client.js"; import { simplifyErrors } from "../../../utils/error.js"; @@ -153,15 +152,12 @@ const runMigrationsCli = async (params: RunMigrationsCliParams) => { await withMigrationsToRun(resolvedParams.value, async migrations => { const operation = resolvedParams.value.rollback ? "rollback" : "run"; - const warningMessage = chalk.yellow( - `⚠ 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`, - ); - - const confirmed = !params.skipConfirmation ? await requestConfirmation(warningMessage) : true; - - if (!confirmed) { - process.exit(0); - } + await checkConfirmation({ + message: + `⚠ 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`, + skipConfirmation: params.skipConfirmation, + logOptions: params, + }); const migrationsStatus = await executeMigrations(migrations, client, { operation, diff --git a/src/commands/sync/run/run.ts b/src/commands/sync/run/run.ts index 2952dd6f..cd74b5f3 100644 --- a/src/commands/sync/run/run.ts +++ b/src/commands/sync/run/run.ts @@ -1,11 +1,10 @@ -import chalk from "chalk"; import { match, P } from "ts-pattern"; -import { logError, logInfo, LogOptions } from "../../../log.js"; +import { logError, LogOptions } from "../../../log.js"; import { syncEntityChoices, SyncEntityName } from "../../../modules/sync/constants/entities.js"; import { printDiff } from "../../../modules/sync/printDiff.js"; import { SyncEntities, syncRunInternal, SyncRunParams } from "../../../modules/sync/syncRun.js"; -import { requestConfirmation } from "../../../modules/sync/utils/consoleHelpers.js"; +import { checkConfirmation } from "../../../modules/sync/utils/consoleHelpers.js"; import { RegisterCommand } from "../../../types/yargs.js"; import { simplifyErrors } from "../../../utils/error.js"; @@ -91,18 +90,14 @@ const syncRunCli = async (params: SyncModelRunCliParams) => { await syncRunInternal(resolvedParams, `sync-${commandName}`, async (diffModel) => { printDiff(diffModel, new Set(params.entities), params); - const warningMessage = chalk.yellow( - `⚠ Running this operation may result in irreversible changes to the content in environment ${params.targetEnvironmentId}. Mentioned changes might include: + await checkConfirmation({ + message: + `⚠ Running this operation may result in irreversible changes to the content in environment ${params.targetEnvironmentId}. Mentioned changes might include: - Removing content due to element deletion OK to proceed y/n? (suppress this message with --sw parameter)\n`, - ); - - const confirmed = !params.skipConfirmation ? await requestConfirmation(warningMessage) : true; - - if (!confirmed) { - logInfo(params, "standard", chalk.red("Operation aborted.")); - process.exit(1); - } + skipConfirmation: params.skipConfirmation, + logOptions: params, + }); }); } catch (e) { logError(params, JSON.stringify(e, Object.getOwnPropertyNames(e))); diff --git a/src/modules/migrateContent/migrateContentRun.ts b/src/modules/migrateContent/migrateContentRun.ts index 50a27d90..9b9864c2 100644 --- a/src/modules/migrateContent/migrateContentRun.ts +++ b/src/modules/migrateContent/migrateContentRun.ts @@ -43,10 +43,14 @@ export type MigrateContentFilterParams = Readonly< >; export const migrateContentRun = async (params: MigrateContentRunParams) => { - await migrateContentRunIntenal(params, "migrate-content-run-API"); + await migrateContentRunInternal(params, "migrate-content-run-API"); }; -export const migrateContentRunIntenal = async (params: MigrateContentRunParams, commandName: string) => { +export const migrateContentRunInternal = async ( + params: MigrateContentRunParams, + commandName: string, + withItemCodenames: (itemsCodenames: ReadonlyArray) => Promise = () => Promise.resolve(), +) => { if ("filename" in params) { const data = await extractAsync({ filename: params.filename }); @@ -85,6 +89,8 @@ export const migrateContentRunIntenal = async (params: MigrateContentRunParams, }`, ); + await withItemCodenames(itemsCodenames); + await migrateAsync({ targetEnvironment: { apiKey: params.targetApiKey, diff --git a/src/modules/migrations/run.ts b/src/modules/migrations/run.ts index eab6ae25..4481fff2 100644 --- a/src/modules/migrations/run.ts +++ b/src/modules/migrations/run.ts @@ -158,7 +158,7 @@ const filterMigrationsToRun = ( logInfo( params, "standard", - `${operation === "run" ? "Running" : "Rollbacking"} ${migrationsToRun.length} migrations:\n${ + `${operation === "run" ? "Running" : "Rolling back"} ${migrationsToRun.length} migrations:\n${ migrationsToRun.map(m => chalk.blue(m.name)).join("\n") }\n`, ); diff --git a/src/modules/sync/sync/types.ts b/src/modules/sync/sync/types.ts index 33f23e1f..e9d1098e 100644 --- a/src/modules/sync/sync/types.ts +++ b/src/modules/sync/sync/types.ts @@ -29,7 +29,7 @@ export const updateContentTypesAndAddReferences = async ( ) => { const typesReplaceReferencesOps = typeOps.added.map(createUpdateReferencesOps); - if (!typesReplaceReferencesOps.length) { + if (!typesReplaceReferencesOps.length && !typeOps.updated.size) { logInfo(logOptions, "standard", "No content types to update"); return; } diff --git a/src/modules/sync/utils/consoleHelpers.ts b/src/modules/sync/utils/consoleHelpers.ts index b0fa0646..85c63245 100644 --- a/src/modules/sync/utils/consoleHelpers.ts +++ b/src/modules/sync/utils/consoleHelpers.ts @@ -1,6 +1,10 @@ import readline from "node:readline"; -export const requestConfirmation = async (message: string) => { +import chalk from "chalk"; + +import { logInfo, LogOptions } from "../../../log.js"; + +const requestConfirmation = async (message: string) => { const rl = readline.createInterface({ input: process.stdin, output: process.stdout, @@ -13,3 +17,18 @@ export const requestConfirmation = async (message: string) => { }); }); }; + +export const checkConfirmation = async (options: { + message: string; + skipConfirmation: boolean | undefined; + logOptions: LogOptions; +}) => { + const warningMessage = chalk.yellow(options.message); + + const confirmed = !options.skipConfirmation ? await requestConfirmation(warningMessage) : true; + + if (!confirmed) { + logInfo(options.logOptions, "standard", chalk.red("Operation aborted.")); + process.exit(0); + } +};