|
| 1 | +import fs from 'node:fs' |
1 | 2 | import { resolve } from 'node:path'
|
2 | 3 | import { Command, InvalidArgumentError } from 'commander'
|
3 | 4 | import { intro, log } from '@clack/prompts'
|
4 | 5 | import chalk from 'chalk'
|
| 6 | +import semver from 'semver' |
5 | 7 |
|
6 | 8 | import {
|
7 | 9 | SUPPORTED_PACKAGE_MANAGERS,
|
@@ -87,6 +89,79 @@ export function cli({
|
87 | 89 |
|
88 | 90 | program.name(name).description(`CLI to create a new ${appName} application`)
|
89 | 91 |
|
| 92 | + program |
| 93 | + .command('pin-versions') |
| 94 | + .description('Pin versions of the TanStack libraries') |
| 95 | + .action(async () => { |
| 96 | + if (!fs.existsSync('package.json')) { |
| 97 | + console.error('package.json not found') |
| 98 | + return |
| 99 | + } |
| 100 | + const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8')) |
| 101 | + |
| 102 | + const packages: Record<string, string> = { |
| 103 | + '@tanstack/react-router': '', |
| 104 | + '@tanstack/router-generator': '', |
| 105 | + '@tanstack/react-router-devtools': '', |
| 106 | + '@tanstack/react-start': '', |
| 107 | + '@tanstack/react-start-config': '', |
| 108 | + '@tanstack/router-plugin': '', |
| 109 | + '@tanstack/react-start-client': '', |
| 110 | + '@tanstack/react-start-plugin': '1.115.0', |
| 111 | + '@tanstack/react-start-server': '', |
| 112 | + '@tanstack/start-server-core': '1.115.0', |
| 113 | + } |
| 114 | + |
| 115 | + function sortObject(obj: Record<string, string>): Record<string, string> { |
| 116 | + return Object.keys(obj) |
| 117 | + .sort() |
| 118 | + .reduce<Record<string, string>>((acc, key) => { |
| 119 | + acc[key] = obj[key] |
| 120 | + return acc |
| 121 | + }, {}) |
| 122 | + } |
| 123 | + |
| 124 | + if (!packageJson.dependencies['@tanstack/react-start']) { |
| 125 | + console.error('@tanstack/react-start not found in dependencies') |
| 126 | + return |
| 127 | + } |
| 128 | + let changed = 0 |
| 129 | + const startVersion = packageJson.dependencies[ |
| 130 | + '@tanstack/react-start' |
| 131 | + ].replace(/^\^/, '') |
| 132 | + for (const pkg of Object.keys(packages)) { |
| 133 | + if (!packageJson.dependencies[pkg]) { |
| 134 | + packageJson.dependencies[pkg] = packages[pkg].length |
| 135 | + ? semver.maxSatisfying( |
| 136 | + [startVersion, packages[pkg]], |
| 137 | + `^${packages[pkg]}`, |
| 138 | + )! |
| 139 | + : startVersion |
| 140 | + changed++ |
| 141 | + } else { |
| 142 | + if (packageJson.dependencies[pkg].startsWith('^')) { |
| 143 | + packageJson.dependencies[pkg] = packageJson.dependencies[ |
| 144 | + pkg |
| 145 | + ].replace(/^\^/, '') |
| 146 | + changed++ |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + packageJson.dependencies = sortObject(packageJson.dependencies) |
| 151 | + if (changed > 0) { |
| 152 | + fs.writeFileSync('package.json', JSON.stringify(packageJson, null, 2)) |
| 153 | + console.log( |
| 154 | + `${changed} packages updated. |
| 155 | +
|
| 156 | +Remove your node_modules directory and package lock file and re-install.`, |
| 157 | + ) |
| 158 | + } else { |
| 159 | + console.log( |
| 160 | + 'No changes needed. The relevant TanStack packages are already pinned.', |
| 161 | + ) |
| 162 | + } |
| 163 | + }) |
| 164 | + |
90 | 165 | program
|
91 | 166 | .command('add')
|
92 | 167 | .argument(
|
|
0 commit comments