diff --git a/package.json b/package.json index cac26fe..2a17463 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kontent-ai/cli", - "version": "0.7.4", + "version": "0.7.5", "description": "Command line interface tool that can be used for generating and runningKontent.ai migration scripts", "main": "./lib/index.js", "types": "./lib/types/index.d.ts", diff --git a/src/cmds/migration/run.ts b/src/cmds/migration/run.ts index a883ebc..2bc1587 100644 --- a/src/cmds/migration/run.ts +++ b/src/cmds/migration/run.ts @@ -150,7 +150,13 @@ const runMigrationCommand: yargs.CommandModule = { saveStatusFromPlugin: plugin?.saveStatus ?? null, }; - const migrationsStatus = await loadMigrationsExecutionStatus(plugin?.readStatus ?? null); + let migrationsStatus: IStatus; + try { + migrationsStatus = await loadMigrationsExecutionStatus(plugin?.readStatus ?? null); + } catch (e) { + console.error(`An error ${chalk.red(e)} occured when trying to read status`); + process.exit(1); + } if (runAll || runRange) { let migrationsToRun = await loadMigrationFiles(); diff --git a/src/tests/statusManager.test.ts b/src/tests/statusManager.test.ts index 771b2f9..5bb12be 100644 --- a/src/tests/statusManager.test.ts +++ b/src/tests/statusManager.test.ts @@ -49,13 +49,25 @@ describe('Status manager', () => { }); it('loadMigrationsExecutionStatus to be called with plugins', async () => { - jest.spyOn(fileUtils, 'fileExists').mockReturnValue(true); + const expectedStatus = { '30816c62-8d41-4dc4-a1ab-40440070b7bf': [] }; + + const readStatusPlugin = async () => { + return expectedStatus; + }; + + const returnedStatus = await statusManager.loadMigrationsExecutionStatus(readStatusPlugin); + + expect(returnedStatus).toEqual(expectedStatus); + }); - const readStatusMocked = jest.fn().mockResolvedValue({}); + it('loadMigrationsExecutionStatus to be called with plugin that throw', async () => { + const readStatusPlugin = async () => { + throw new Error('Error during plugin function'); + }; - await statusManager.loadMigrationsExecutionStatus(readStatusMocked); + expect.assertions(1); - expect(readStatusMocked).toHaveBeenCalled(); + return statusManager.loadMigrationsExecutionStatus(readStatusPlugin).catch((e) => expect((e as Error).message).toMatch('Error during plugin function')); }); it('MarkAsCompleted to be called with plugins', async () => { diff --git a/src/utils/migrationUtils.ts b/src/utils/migrationUtils.ts index 2195905..b39dab2 100644 --- a/src/utils/migrationUtils.ts +++ b/src/utils/migrationUtils.ts @@ -59,7 +59,7 @@ interface RunMigrationOptions { export const runMigration = async (migrationsStatus: IStatus, migration: IMigration, options: RunMigrationOptions): Promise => { const { client, projectId, operation, saveStatusFromPlugin } = options; - console.log(`Running the ${operation === 'rollback' && 'rollback of'} ${migration.name} migration.`); + console.log(`Running the ${operation === 'rollback' ? 'rollback of' : ''} ${migration.name} migration.`); let isSuccess = true; @@ -115,7 +115,7 @@ export const runMigration = async (migrationsStatus: IStatus, migration: IMigrat return 1; } - console.log(chalk.green(`The \"${migration.name}\" migration on a project with ID \"${projectId}\" executed successfully.`)); + console.log(chalk.green(`The ${operation === 'rollback' ? 'rollback of ' : ''}\"${migration.name}\" migration on a project with ID \"${projectId}\" executed successfully.`)); return 0; }; diff --git a/src/utils/statusManager.ts b/src/utils/statusManager.ts index 9736304..739f513 100644 --- a/src/utils/statusManager.ts +++ b/src/utils/statusManager.ts @@ -6,7 +6,6 @@ import { type StatusPlugin } from './status/statusPlugin'; const migrationStatusFilename = 'status.json'; const pluginsFilename = 'plugins.js'; -// let status: IStatus = {}; const updateMigrationStatus = async (status: IStatus, projectId: string, migrationStatus: IMigrationStatus, saveStatusFromPlugin: StatusPlugin['saveStatus'] | null) => { status[projectId] = status[projectId] === undefined ? [] : status[projectId]; @@ -40,10 +39,11 @@ const saveStatusFile = async (migrationsStatus: IStatus, saveStatusFromPlugin: S if (saveStatusFromPlugin) { try { await saveStatusFromPlugin(statusJSON); - } catch (e) { - console.error(`The error ${e} occured when using saveStatus function from plugin.`); - } finally { return; + } catch (e) { + console.error(`The error ${e} occured when using saveStatus function from plugin. Fallbacking to write status into status.json`); + saveStatusToFile(statusJSON); + throw new Error((e as Error).message); } } @@ -72,13 +72,7 @@ const getStatusFilepath = (): string => { export const loadMigrationsExecutionStatus = async (readStatusFromPlugin: StatusPlugin['readStatus'] | null): Promise => { if (readStatusFromPlugin) { - try { - return await readStatusFromPlugin(); - } catch (e) { - console.error(`The error ${e} occured when using readStatus function from plugin.`); - } finally { - return {}; - } + return await readStatusFromPlugin(); } return readFromStatus();