Skip to content

Commit

Permalink
Merge pull request #53 from kontent-ai/fix_plugins
Browse files Browse the repository at this point in the history
Fix plugins
  • Loading branch information
winklertomas authored Jun 23, 2023
2 parents 3b2f897 + e1aba33 commit 0c2a90f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
8 changes: 7 additions & 1 deletion src/cmds/migration/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
20 changes: 16 additions & 4 deletions src/tests/statusManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/migrationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ interface RunMigrationOptions {
export const runMigration = async (migrationsStatus: IStatus, migration: IMigration, options: RunMigrationOptions): Promise<number> => {
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;

Expand Down Expand Up @@ -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;
};

Expand Down
16 changes: 5 additions & 11 deletions src/utils/statusManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -72,13 +72,7 @@ const getStatusFilepath = (): string => {

export const loadMigrationsExecutionStatus = async (readStatusFromPlugin: StatusPlugin['readStatus'] | null): Promise<IStatus> => {
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();
Expand Down

0 comments on commit 0c2a90f

Please sign in to comment.