UNPKG

@gmetrixr/rjson

Version:
36 lines (35 loc) 2.14 kB
import { r, RT, R, rtp } from "../../r"; import initialRMigration from "./deployment-migration-commands/d000_001_initial_deployment_migrations"; import { getHighestDeploymentVersion, deploymentMigrationTree } from "./deploymentMigrations"; const deploymentMigrationVersions = Object.keys(deploymentMigrationTree).map(numStr => parseInt(numStr)).sort((a, b) => (a - b)); export const createNewDeployment = () => { const deployment = R.createRecord(RT.deployment); const recordF = r.record(deployment); recordF.set(rtp.deployment.deployment_version, getHighestDeploymentVersion()); return deployment; }; /** * Applies migrations for "r" type and returns a new project reference */ export const migrateDeployment = (deploymentJson, uptoVersion) => { var _a, _b, _c, _d; //Check if deployment hasn't been converted to recordNode yet if (((_a = deploymentJson === null || deploymentJson === void 0 ? void 0 : deploymentJson.props) === null || _a === void 0 ? void 0 : _a.deployment_version) === undefined || ((_b = deploymentJson === null || deploymentJson === void 0 ? void 0 : deploymentJson.props) === null || _b === void 0 ? void 0 : _b.deployment_version) < 1) { //The following step converts the json to "r" type and makes the version number 1 deploymentJson = initialRMigration.execute(deploymentJson); } const rDeploymentJson = deploymentJson; let jsonVersion = (_d = (_c = rDeploymentJson === null || rDeploymentJson === void 0 ? void 0 : rDeploymentJson.props) === null || _c === void 0 ? void 0 : _c.deployment_version) !== null && _d !== void 0 ? _d : 0; if (uptoVersion === undefined) { uptoVersion = deploymentMigrationVersions[deploymentMigrationVersions.length - 1] + 1; } for (const key of deploymentMigrationVersions) { if (jsonVersion === key && key < uptoVersion) { // console.log(`Running r migration ${key}`); deploymentMigrationTree[key].execute(deploymentJson); jsonVersion = deploymentJson.props.deployment_version; } } return deploymentJson; }; export { getHighestDeploymentVersion };