diff --git a/src/services/changes/changes.apply.services.ts b/src/services/changes/changes.apply.services.ts index 74c3bd43..b4b55e71 100644 --- a/src/services/changes/changes.apply.services.ts +++ b/src/services/changes/changes.apply.services.ts @@ -1,5 +1,6 @@ -import {hexStringToUint8Array} from '@dfinity/utils'; -import {commitProposal} from '@junobuild/cdn'; +import {fromNullable, hexStringToUint8Array, isNullish} from '@dfinity/utils'; +import {commitProposal, getProposal, Proposal} from '@junobuild/cdn'; +import {red} from 'kleur'; import ora from 'ora'; import type {SatelliteParametersWithId} from '../../types/satellite'; import {readChangesIdAndHash} from '../../utils/changes.utils'; @@ -9,7 +10,13 @@ import {clearProposalStagedAssets} from './changes.clear.services'; export const applyChanges = async (args?: string[]) => { const {satellite} = await assertConfigAndLoadSatelliteContext(args); - const {proposalId, hash} = readChangesIdAndHash(args); + const result = await init({args, satellite}); + + if (result.result === "error") { + return; + } + + const {proposalId, proposal, hash} = result; await executeApplyChanges({satellite, proposalId, hash}); @@ -17,6 +24,54 @@ export const applyChanges = async (args?: string[]) => { args, proposalId }); + + switch (Object.keys(proposal.proposal_type)[0]) { + case 'SegmentsDeployment': + // TODO + default: + return; + } +}; + +const init = async ({ + args, + satellite +}: { + args?: string[]; + satellite: SatelliteParametersWithId; +}): Promise< + {result: 'success'; proposalId: bigint; hash: string; proposal: Proposal} | {result: 'error'} +> => { + const spinner = ora('Loading...').start(); + + try { + const {proposalId, ...rest} = readChangesIdAndHash(args); + + const result = await getProposal({ + cdn: { + satellite + }, + proposal_id: proposalId + }); + + const proposal = fromNullable(result); + + if (isNullish(proposal)) { + console.log(red(`Unknown proposal ID ${proposalId}.`)); + return {result: 'error'}; + } + + return { + result: 'success', + ...rest, + proposal, + proposalId + }; + } catch (err: unknown) { + spinner.stop(); + + throw err; + } }; const executeApplyChanges = async ({