|
| 1 | +import { AmplifyMigrationCloneStep } from './gen2-migration/clone'; |
| 2 | +import { $TSContext } from '@aws-amplify/amplify-cli-core'; |
| 3 | +import { AmplifyMigrationStep } from './gen2-migration/_step'; |
| 4 | +import { printer } from '@aws-amplify/amplify-prompts'; |
| 5 | +import { AmplifyMigrationCleanupStep } from './gen2-migration/cleanup'; |
| 6 | +import { AmplifyMigrationDecommissionStep } from './gen2-migration/decommission'; |
| 7 | +import { AmplifyMigrationGenerateStep } from './gen2-migration/generate'; |
| 8 | +import { AmplifyMigrationLockStep } from './gen2-migration/lock'; |
| 9 | +import { AmplifyMigrationRefactorStep } from './gen2-migration/refactor'; |
| 10 | +import { AmplifyMigrationShiftStep } from './gen2-migration/shift'; |
| 11 | + |
| 12 | +const STEPS = { |
| 13 | + cleanup: { |
| 14 | + class: AmplifyMigrationCleanupStep, |
| 15 | + description: 'TODO', |
| 16 | + }, |
| 17 | + clone: { |
| 18 | + class: AmplifyMigrationCloneStep, |
| 19 | + description: 'TODO', |
| 20 | + }, |
| 21 | + decommission: { |
| 22 | + class: AmplifyMigrationDecommissionStep, |
| 23 | + description: 'TODO', |
| 24 | + }, |
| 25 | + generate: { |
| 26 | + class: AmplifyMigrationGenerateStep, |
| 27 | + description: 'TODO', |
| 28 | + }, |
| 29 | + lock: { |
| 30 | + class: AmplifyMigrationLockStep, |
| 31 | + description: 'TODO', |
| 32 | + }, |
| 33 | + refactor: { |
| 34 | + class: AmplifyMigrationRefactorStep, |
| 35 | + description: 'TODO', |
| 36 | + }, |
| 37 | + shift: { |
| 38 | + class: AmplifyMigrationShiftStep, |
| 39 | + description: 'TODO', |
| 40 | + }, |
| 41 | +}; |
| 42 | + |
| 43 | +export const run = async (context: $TSContext) => { |
| 44 | + const step = STEPS[(context.input.subCommands ?? [])[0]]; |
| 45 | + if (!step) { |
| 46 | + displayHelp(context); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + shiftParams(context); |
| 51 | + |
| 52 | + const implementation: AmplifyMigrationStep = new step.class(context); |
| 53 | + |
| 54 | + try { |
| 55 | + printer.info('Validating'); |
| 56 | + await implementation.validate(); |
| 57 | + printer.info('Executing'); |
| 58 | + await implementation.execute(); |
| 59 | + } catch (error: unknown) { |
| 60 | + printer.warn(`${error}. Rolling back.`); |
| 61 | + await implementation.rollback(); |
| 62 | + throw error; |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +function shiftParams(context) { |
| 67 | + delete context.parameters.first; |
| 68 | + delete context.parameters.second; |
| 69 | + delete context.parameters.third; |
| 70 | + const { subCommands } = context.input; |
| 71 | + /* eslint-disable */ |
| 72 | + if (subCommands && subCommands.length > 1) { |
| 73 | + if (subCommands.length > 1) { |
| 74 | + context.parameters.first = subCommands[1]; |
| 75 | + } |
| 76 | + if (subCommands.length > 2) { |
| 77 | + context.parameters.second = subCommands[2]; |
| 78 | + } |
| 79 | + if (subCommands.length > 3) { |
| 80 | + context.parameters.third = subCommands[3]; |
| 81 | + } |
| 82 | + } |
| 83 | + /* eslint-enable */ |
| 84 | +} |
| 85 | + |
| 86 | +function displayHelp(context: $TSContext) { |
| 87 | + context.amplify.showHelp( |
| 88 | + 'amplify gen2-migration <subcommands>', |
| 89 | + Object.entries(STEPS).map(([name, v]) => ({ name, description: v.description })), |
| 90 | + ); |
| 91 | + printer.info(''); |
| 92 | +} |
0 commit comments