|
10 | 10 | // ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃ |
11 | 11 | // ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ |
12 | 12 |
|
| 13 | +import Semver from "./migrate/semver"; |
13 | 14 | import migrate_0_0_0 from "./migrate/0-0-0"; |
14 | 15 | import migrate_2_6_1 from "./migrate/2-6-1"; |
| 16 | +import { version as PKG_VERSION } from "@finos/perspective/package.json" assert { type: "json" }; |
15 | 17 |
|
16 | 18 | /** |
17 | 19 | * A migration utility for `@finos/perspective-viewer` and |
@@ -57,74 +59,31 @@ import migrate_2_6_1 from "./migrate/2-6-1"; |
57 | 59 | */ |
58 | 60 | export function convert( |
59 | 61 | old: Record<string, unknown> | ArrayBuffer | string, |
60 | | - { warn = true, replace_defaults = false }: PerspectiveConvertOptions = {} |
| 62 | + { |
| 63 | + warn = true, |
| 64 | + verbose = false, |
| 65 | + replace_defaults = false, |
| 66 | + }: PerspectiveConvertOptions = {} |
61 | 67 | ): Record<string, unknown> | ArrayBuffer | string { |
62 | 68 | if (typeof old === "object" && !(old instanceof ArrayBuffer)) { |
63 | 69 | const copy = JSON.parse(JSON.stringify(old)); |
64 | 70 | if ("viewers" in copy && "detail" in copy) { |
65 | 71 | return migrate_workspace(copy, { warn, replace_defaults }); |
66 | 72 | } else { |
67 | | - return migrate_viewer(copy, false, { warn, replace_defaults }); |
| 73 | + return migrate_viewer(copy, false, { |
| 74 | + warn: verbose ? true : warn, |
| 75 | + verbose, |
| 76 | + replace_defaults, |
| 77 | + }); |
68 | 78 | } |
69 | 79 | } else { |
70 | 80 | return old; |
71 | 81 | } |
72 | 82 | } |
73 | 83 |
|
74 | | -function* null_iter() { |
75 | | - while (true) yield null; |
76 | | -} |
77 | | -export type Semver = { |
78 | | - major: number; |
79 | | - minor: number; |
80 | | - patch: number; |
81 | | - build?: { |
82 | | - major: number; |
83 | | - minor: number; |
84 | | - patch: number; |
85 | | - }; |
86 | | -}; |
87 | | -// This gets what the semver crate calls major, minor, patch, and build values, but does not capture release. |
88 | | -export function parse_semver(ver: string): Semver { |
89 | | - let regex = /(\d+)\.(\d+)\.(\d+)(\+.+)?/; |
90 | | - let [_ver, major, minor, patch, build_str] = ver.match(regex); |
91 | | - let [_build, build_major, build_minor, build_patch] = |
92 | | - build_str?.match(regex) ?? null_iter(); |
93 | | - let build = |
94 | | - build_major && build_minor && build_patch |
95 | | - ? { |
96 | | - major: Number(build_major), |
97 | | - minor: Number(build_minor), |
98 | | - patch: Number(build_patch), |
99 | | - } |
100 | | - : null; |
101 | | - return { |
102 | | - major: Number(major), |
103 | | - minor: Number(minor), |
104 | | - patch: Number(patch), |
105 | | - build, |
106 | | - }; |
107 | | -} |
108 | | - |
109 | | -/** |
110 | | - * Checks if left > right |
111 | | - * @param left |
112 | | - * @param right_str |
113 | | - * @returns |
114 | | - */ |
115 | | -export function cmp_semver(left: Semver, right_str: string) { |
116 | | - let right = parse_semver(right_str); |
117 | | - return ( |
118 | | - left.major > right.major || |
119 | | - (left.major === right.major && left.minor > right.minor) || |
120 | | - (left.major === right.major && |
121 | | - left.minor === right.minor && |
122 | | - left.patch > right.patch) |
123 | | - ); |
124 | | -} |
125 | | - |
126 | 84 | type PerspectiveConvertOptions = { |
127 | 85 | warn?: boolean; |
| 86 | + verbose?: boolean; |
128 | 87 | replace_defaults?: boolean; |
129 | 88 | }; |
130 | 89 |
|
@@ -166,31 +125,39 @@ function migrate_workspace(old, options) { |
166 | 125 | * @returns |
167 | 126 | */ |
168 | 127 | function migrate_viewer(old, omit_attributes, options) { |
169 | | - old.version = old.version |
170 | | - ? parse_semver(old.version) |
171 | | - : parse_semver("0.0.0"); |
| 128 | + old.version = old.version ? new Semver(old.version) : new Semver("0.0.0"); |
172 | 129 | options.omit_attributes = omit_attributes; |
173 | | - return chain( |
| 130 | + // This array details the "next version" in line. It begins with 2.6.1 |
| 131 | + // and continues until the latest migration. Then, the current package |
| 132 | + // version is appended to the end, in case the latest migration |
| 133 | + // is older than the latest release. Each migration will shift the array. |
| 134 | + // Note that because we will be working with the latest version on master, |
| 135 | + // and those versions will need to update from themselves to themselves, |
| 136 | + // migration scripts must be idempotent. |
| 137 | + options.version_chain = ["2.6.1" /*, "2.7.0", etc. */]; |
| 138 | + options.version_chain.push(PKG_VERSION); |
| 139 | + let res = chain( |
174 | 140 | old, |
175 | 141 | [migrate_0_0_0, migrate_2_6_1, assure_latest, semver_to_string], |
176 | 142 | options |
177 | 143 | ); |
| 144 | + if (options.verbose) { |
| 145 | + console.log("Final result -> ", res); |
| 146 | + } |
| 147 | + return res; |
178 | 148 | } |
179 | 149 |
|
180 | | -import { version as PKG_VERSION } from "./package.json" assert { type: "json" }; |
181 | | - |
182 | | -function assure_latest(old) { |
183 | | - if (cmp_semver(old.version, PKG_VERSION)) { |
| 150 | +function assure_latest(old: { version: Semver }) { |
| 151 | + if (old.version.gt(PKG_VERSION)) { |
184 | 152 | throw new Error("Migrated version is newer than package version!"); |
185 | 153 | } else { |
186 | | - old.version = parse_semver(PKG_VERSION); |
| 154 | + old.version = new Semver(PKG_VERSION); |
187 | 155 | return old; |
188 | 156 | } |
189 | 157 | } |
190 | 158 |
|
191 | 159 | function semver_to_string(old) { |
192 | 160 | // intentionally ignores build |
193 | | - console.warn(old.version); |
194 | 161 | old.version = `${old.version.major}.${old.version.minor}.${old.version.patch}`; |
195 | 162 | return old; |
196 | 163 | } |
|
0 commit comments