|
| 1 | +import { readFileSync, writeFileSync } from 'fs'; |
| 2 | +import { resolve } from 'path'; |
| 3 | + |
| 4 | +import semver from 'semver'; |
| 5 | + |
| 6 | +const CONSTANTS_DIR = resolve( |
| 7 | + __dirname, |
| 8 | + '../src/engine/core-modules/upgrade/constants', |
| 9 | +); |
| 10 | + |
| 11 | +const AUTO_GENERATED_HEADER = `/* |
| 12 | + * _____ _ |
| 13 | + *|_ _|_ _____ _ __ | |_ _ _ |
| 14 | + * | | \\ \\ /\\ / / _ \\ '_ \\| __| | | | Auto-generated file |
| 15 | + * | | \\ V V / __/ | | | |_| |_| | Any edits to this will be overridden |
| 16 | + * |_| \\_/\\_/ \\___|_| |_|\\__|\\__, | Generated by: npx nx version:bump twenty-server |
| 17 | + * |___/ |
| 18 | + */ |
| 19 | +
|
| 20 | +`; |
| 21 | + |
| 22 | +const incrementMinorVersion = (version: string): string => { |
| 23 | + const incremented = semver.inc(version, 'minor'); |
| 24 | + |
| 25 | + if (!incremented) { |
| 26 | + throw new Error(`Failed to increment version: ${version}`); |
| 27 | + } |
| 28 | + |
| 29 | + return incremented; |
| 30 | +}; |
| 31 | + |
| 32 | +const getCurrentVersion = (): string => { |
| 33 | + const filePath = `${CONSTANTS_DIR}/twenty-current-version.constant.ts`; |
| 34 | + const content = readFileSync(filePath, 'utf-8'); |
| 35 | + const match = content.match(/'([0-9.]+)'/); |
| 36 | + |
| 37 | + if (!match) { |
| 38 | + throw new Error('Could not extract current version'); |
| 39 | + } |
| 40 | + |
| 41 | + return match[1]; |
| 42 | +}; |
| 43 | + |
| 44 | +const getPreviousVersions = (): string[] => { |
| 45 | + const filePath = `${CONSTANTS_DIR}/twenty-previous-versions.constant.ts`; |
| 46 | + const content = readFileSync(filePath, 'utf-8'); |
| 47 | + const matches = content.match(/'[0-9.]+'/g); |
| 48 | + |
| 49 | + if (!matches) { |
| 50 | + return []; |
| 51 | + } |
| 52 | + |
| 53 | + return matches.map((m) => m.replace(/'/g, '')); |
| 54 | +}; |
| 55 | + |
| 56 | +const getNextVersions = (): string[] => { |
| 57 | + const filePath = `${CONSTANTS_DIR}/twenty-next-versions.constant.ts`; |
| 58 | + const content = readFileSync(filePath, 'utf-8'); |
| 59 | + const matches = content.match(/'[0-9.]+'/g); |
| 60 | + |
| 61 | + if (!matches) { |
| 62 | + return []; |
| 63 | + } |
| 64 | + |
| 65 | + return matches.map((m) => m.replace(/'/g, '')); |
| 66 | +}; |
| 67 | + |
| 68 | +const updateCurrentVersion = (newVersion: string): void => { |
| 69 | + const filePath = `${CONSTANTS_DIR}/twenty-current-version.constant.ts`; |
| 70 | + |
| 71 | + writeFileSync( |
| 72 | + filePath, |
| 73 | + `${AUTO_GENERATED_HEADER}export const TWENTY_CURRENT_VERSION = '${newVersion}' as const;\n`, |
| 74 | + ); |
| 75 | + |
| 76 | + console.log(`Updated TWENTY_CURRENT_VERSION to '${newVersion}'`); |
| 77 | +}; |
| 78 | + |
| 79 | +const updatePreviousVersions = (versions: string[]): void => { |
| 80 | + const filePath = `${CONSTANTS_DIR}/twenty-previous-versions.constant.ts`; |
| 81 | + const formattedVersions = versions.map((v) => ` '${v}',`).join('\n'); |
| 82 | + |
| 83 | + writeFileSync( |
| 84 | + filePath, |
| 85 | + `${AUTO_GENERATED_HEADER}export const TWENTY_PREVIOUS_VERSIONS = [\n${formattedVersions}\n] as const;\n`, |
| 86 | + ); |
| 87 | + |
| 88 | + console.log( |
| 89 | + `Updated TWENTY_PREVIOUS_VERSIONS with ${versions.length} versions`, |
| 90 | + ); |
| 91 | +}; |
| 92 | + |
| 93 | +const updateNextVersions = (versions: string[]): void => { |
| 94 | + const filePath = `${CONSTANTS_DIR}/twenty-next-versions.constant.ts`; |
| 95 | + const formattedVersions = versions.map((v) => ` '${v}',`).join('\n'); |
| 96 | + |
| 97 | + writeFileSync( |
| 98 | + filePath, |
| 99 | + `${AUTO_GENERATED_HEADER}export const TWENTY_NEXT_VERSIONS = [\n${formattedVersions}\n] as const;\n`, |
| 100 | + ); |
| 101 | + |
| 102 | + console.log(`Updated TWENTY_NEXT_VERSIONS with ${versions.length} versions`); |
| 103 | +}; |
| 104 | + |
| 105 | +const resolveNewVersion = ({ |
| 106 | + newVersionArg, |
| 107 | + currentVersion, |
| 108 | + nextVersions, |
| 109 | +}: { |
| 110 | + newVersionArg: string | undefined; |
| 111 | + currentVersion: string; |
| 112 | + nextVersions: string[]; |
| 113 | +}): string => { |
| 114 | + if (newVersionArg) { |
| 115 | + if (!semver.valid(newVersionArg)) { |
| 116 | + throw new Error(`Invalid version format: ${newVersionArg}`); |
| 117 | + } |
| 118 | + |
| 119 | + if (!semver.gt(newVersionArg, currentVersion)) { |
| 120 | + throw new Error( |
| 121 | + `New version '${newVersionArg}' must be greater than current version '${currentVersion}'`, |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + if (nextVersions.length > 0) { |
| 126 | + const highestNextVersion = nextVersions[nextVersions.length - 1]; |
| 127 | + |
| 128 | + if (semver.gt(newVersionArg, highestNextVersion)) { |
| 129 | + throw new Error( |
| 130 | + `New version '${newVersionArg}' cannot be greater than highest planned next version '${highestNextVersion}'`, |
| 131 | + ); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return newVersionArg; |
| 136 | + } |
| 137 | + |
| 138 | + if (nextVersions.length === 0) { |
| 139 | + const computed = incrementMinorVersion(currentVersion); |
| 140 | + |
| 141 | + console.log( |
| 142 | + `\nNo version provided and TWENTY_NEXT_VERSIONS is empty, computed from current: '${computed}'`, |
| 143 | + ); |
| 144 | + |
| 145 | + return computed; |
| 146 | + } |
| 147 | + |
| 148 | + console.log( |
| 149 | + `\nNo version provided, using TWENTY_NEXT_VERSIONS[0]: '${nextVersions[0]}'`, |
| 150 | + ); |
| 151 | + |
| 152 | + return nextVersions[0]; |
| 153 | +}; |
| 154 | + |
| 155 | +const partitionNextVersions = ({ |
| 156 | + nextVersions, |
| 157 | + newVersion, |
| 158 | +}: { |
| 159 | + nextVersions: string[]; |
| 160 | + newVersion: string; |
| 161 | +}): { |
| 162 | + skippedNextVersions: string[]; |
| 163 | + remainingNextVersions: string[]; |
| 164 | +} => |
| 165 | + nextVersions.reduce<{ |
| 166 | + skippedNextVersions: string[]; |
| 167 | + remainingNextVersions: string[]; |
| 168 | + }>( |
| 169 | + (acc, version) => { |
| 170 | + if (semver.lt(version, newVersion)) { |
| 171 | + return { |
| 172 | + ...acc, |
| 173 | + skippedNextVersions: [...acc.skippedNextVersions, version], |
| 174 | + }; |
| 175 | + } |
| 176 | + |
| 177 | + if (semver.gt(version, newVersion)) { |
| 178 | + return { |
| 179 | + ...acc, |
| 180 | + remainingNextVersions: [...acc.remainingNextVersions, version], |
| 181 | + }; |
| 182 | + } |
| 183 | + |
| 184 | + return acc; |
| 185 | + }, |
| 186 | + { skippedNextVersions: [], remainingNextVersions: [] }, |
| 187 | + ); |
| 188 | + |
| 189 | +const bumpVersion = (newVersionArg?: string): void => { |
| 190 | + const currentVersion = getCurrentVersion(); |
| 191 | + const previousVersions = getPreviousVersions(); |
| 192 | + const nextVersions = getNextVersions(); |
| 193 | + |
| 194 | + console.log('\nCurrent state:'); |
| 195 | + console.log(` TWENTY_CURRENT_VERSION: '${currentVersion}'`); |
| 196 | + console.log( |
| 197 | + ` TWENTY_PREVIOUS_VERSIONS: [${previousVersions.map((v) => `'${v}'`).join(', ')}]`, |
| 198 | + ); |
| 199 | + console.log( |
| 200 | + ` TWENTY_NEXT_VERSIONS: [${nextVersions.map((v) => `'${v}'`).join(', ')}]`, |
| 201 | + ); |
| 202 | + |
| 203 | + const newVersion = resolveNewVersion({ |
| 204 | + newVersionArg, |
| 205 | + currentVersion, |
| 206 | + nextVersions, |
| 207 | + }); |
| 208 | + |
| 209 | + const { skippedNextVersions, remainingNextVersions } = partitionNextVersions({ |
| 210 | + nextVersions, |
| 211 | + newVersion, |
| 212 | + }); |
| 213 | + |
| 214 | + const updatedPreviousVersions = [ |
| 215 | + ...previousVersions, |
| 216 | + currentVersion, |
| 217 | + ...skippedNextVersions, |
| 218 | + ]; |
| 219 | + const updatedNextVersions = |
| 220 | + remainingNextVersions.length > 0 |
| 221 | + ? remainingNextVersions |
| 222 | + : [incrementMinorVersion(newVersion)]; |
| 223 | + |
| 224 | + console.log('\nApplying changes:'); |
| 225 | + |
| 226 | + updatePreviousVersions(updatedPreviousVersions); |
| 227 | + updateCurrentVersion(newVersion); |
| 228 | + updateNextVersions(updatedNextVersions); |
| 229 | + |
| 230 | + console.log('\nNew state:'); |
| 231 | + console.log(` TWENTY_CURRENT_VERSION: '${newVersion}'`); |
| 232 | + console.log( |
| 233 | + ` TWENTY_PREVIOUS_VERSIONS: [${updatedPreviousVersions.map((v) => `'${v}'`).join(', ')}]`, |
| 234 | + ); |
| 235 | + console.log( |
| 236 | + ` TWENTY_NEXT_VERSIONS: [${updatedNextVersions.map((v) => `'${v}'`).join(', ')}]`, |
| 237 | + ); |
| 238 | + console.log('\nVersion bump complete!'); |
| 239 | +}; |
| 240 | + |
| 241 | +const newVersion = process.argv[2]; |
| 242 | + |
| 243 | +bumpVersion(newVersion); |
0 commit comments