Skip to content

Commit bdef013

Browse files
authored
fix: Fix tooling version when vite-plus is not found + reorder migrate prompts. (#712)
1 parent f314830 commit bdef013

4 files changed

Lines changed: 54 additions & 48 deletions

File tree

packages/cli/snap-tests/cli-helper-message/snap.txt

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,5 @@ VITE+ - The Unified Toolchain for the Web
3030
vp v<semver>
3131

3232
Local vite-plus:
33-
vite-plus v<semver>
34-
35-
Tools:
36-
vite v<semver>
37-
rolldown v<semver>
38-
vitest v<semver>
39-
oxfmt v<semver>
40-
oxlint v<semver>
41-
oxlint-tsgolint v<semver>
42-
tsdown v<semver>
33+
vite-plus Not found
4334

packages/cli/snap-tests/command-version/snap.txt

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,5 @@ VITE+ - The Unified Toolchain for the Web
44
vp v<semver>
55

66
Local vite-plus:
7-
vite-plus v<semver>
8-
9-
Tools:
10-
vite v<semver>
11-
rolldown v<semver>
12-
vitest v<semver>
13-
oxfmt v<semver>
14-
oxlint v<semver>
15-
oxlint-tsgolint v<semver>
16-
tsdown v<semver>
7+
vite-plus Not found
178

packages/cli/src/migration/bin.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,20 @@ async function main() {
175175
const packageManager =
176176
workspaceInfoOptional.packageManager ?? (await selectPackageManager(options.interactive));
177177

178+
let shouldSetupHooks = await promptGitHooks(options);
179+
180+
const selectedAgentTargetPaths = await selectAgentTargetPaths({
181+
interactive: options.interactive,
182+
agent: options.agent,
183+
onCancel: () => cancelAndExit(),
184+
});
185+
186+
const selectedEditor = await selectEditor({
187+
interactive: options.interactive,
188+
editor: options.editor,
189+
onCancel: () => cancelAndExit(),
190+
});
191+
178192
// ensure the package manager is installed by vite-plus
179193
const downloadResult = await downloadPackageManager(
180194
packageManager,
@@ -223,8 +237,6 @@ async function main() {
223237
cancelAndExit('Vite+ cannot automatically migrate this project yet.', 1);
224238
}
225239

226-
let shouldSetupHooks = await promptGitHooks(options);
227-
228240
if (shouldSetupHooks) {
229241
const reason = preflightGitHooksSetup(workspaceInfo.rootDir);
230242
if (reason) {
@@ -248,24 +260,12 @@ async function main() {
248260
installGitHooks(workspaceInfo.rootDir);
249261
}
250262

251-
const selectedAgentTargetPaths = await selectAgentTargetPaths({
252-
interactive: options.interactive,
253-
agent: options.agent,
254-
onCancel: () => cancelAndExit(),
255-
});
256-
257263
await writeAgentInstructions({
258264
projectRoot: workspaceInfo.rootDir,
259265
targetPaths: selectedAgentTargetPaths,
260266
interactive: options.interactive,
261267
});
262268

263-
const selectedEditor = await selectEditor({
264-
interactive: options.interactive,
265-
editor: options.editor,
266-
onCancel: () => cancelAndExit(),
267-
});
268-
269269
await writeEditorConfigs({
270270
projectRoot: workspaceInfo.rootDir,
271271
editorId: selectedEditor,

packages/cli/src/version.ts

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import path from 'node:path';
55
import { vitePlusHeader } from '../binding/index.js';
66
import { VITE_PLUS_NAME } from './utils/constants.js';
77
import { renderCliDoc } from './utils/help.js';
8-
import { detectPackageMetadata } from './utils/package.js';
8+
import { detectPackageMetadata, hasVitePlusDependency } from './utils/package.js';
99
import { accent, log } from './utils/terminal.js';
1010

1111
const require = createRequire(import.meta.url);
1212

1313
interface PackageJson {
14-
version: string;
14+
version?: string;
1515
bundledVersions?: Record<string, string>;
16+
dependencies?: Record<string, string>;
17+
devDependencies?: Record<string, string>;
1618
}
1719

1820
interface LocalPackageMetadata {
@@ -38,9 +40,29 @@ function getCliVersion(): string | null {
3840
}
3941

4042
function getLocalMetadata(cwd: string): LocalPackageMetadata | null {
43+
if (!isVitePlusDeclaredInAncestors(cwd)) {
44+
return null;
45+
}
4146
return detectPackageMetadata(cwd, VITE_PLUS_NAME) ?? null;
4247
}
4348

49+
function isVitePlusDeclaredInAncestors(cwd: string): boolean {
50+
let currentDir = path.resolve(cwd);
51+
while (true) {
52+
const packageJsonPath = path.join(currentDir, 'package.json');
53+
const pkg = readPackageJsonFromPath(packageJsonPath);
54+
if (pkg && hasVitePlusDependency(pkg)) {
55+
return true;
56+
}
57+
const parentDir = path.dirname(currentDir);
58+
if (parentDir === currentDir) {
59+
break;
60+
}
61+
currentDir = parentDir;
62+
}
63+
return false;
64+
}
65+
4466
function readPackageJsonFromPath(packageJsonPath: string): PackageJson | null {
4567
try {
4668
return JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')) as PackageJson;
@@ -158,18 +180,20 @@ export async function printVersion(cwd: string) {
158180
},
159181
];
160182

161-
const resolvedTools = tools.map((tool) => ({
162-
tool,
163-
version: localMetadata ? resolveToolVersion(tool, localMetadata.path) : null,
164-
}));
165-
166-
sections.push({
167-
title: 'Tools',
168-
rows: resolvedTools.map(({ tool, version }) => ({
169-
label: accent(tool.displayName),
170-
description: version ? `v${version}` : 'Not found',
171-
})),
172-
});
183+
if (localMetadata) {
184+
const resolvedTools = tools.map((tool) => ({
185+
tool,
186+
version: resolveToolVersion(tool, localMetadata.path),
187+
}));
188+
189+
sections.push({
190+
title: 'Tools',
191+
rows: resolvedTools.map(({ tool, version }) => ({
192+
label: accent(tool.displayName),
193+
description: version ? `v${version}` : 'Not found',
194+
})),
195+
});
196+
}
173197

174198
log(renderCliDoc({ sections }));
175199
}

0 commit comments

Comments
 (0)