-
Notifications
You must be signed in to change notification settings - Fork 131
feat(migrate): migrate .nvmrc to .node-version during vp migrate
#1159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
070e2e7
8cf6d89
5b302a2
ff66ccf
d2e4dfd
5a2b00f
db3da18
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1977,3 +1977,86 @@ function setPackageManager( | |
| return pkg; | ||
| }); | ||
| } | ||
|
|
||
| export interface NodeVersionManagerDetection { | ||
| file: string; | ||
| } | ||
|
|
||
| /** | ||
| * Detect a .nvmrc file in the project directory. | ||
| * Returns undefined if not found or .node-version already exists. | ||
| */ | ||
| export function detectNodeVersionManagerFile( | ||
| projectPath: string, | ||
| ): NodeVersionManagerDetection | undefined { | ||
| // already has .node-version β skip detection to avoid false positives and preserve existing file | ||
| if (fs.existsSync(path.join(projectPath, '.node-version'))) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const configs = detectConfigs(projectPath); | ||
| if (configs.nvmrcFile) { | ||
| return { file: '.nvmrc' }; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Parse a version string from a .nvmrc file. | ||
| * Returns null for unsupported aliases like "node", "stable", "system". | ||
| */ | ||
| export function parseNvmrcVersion(content: string): string | null { | ||
| const version = content.split('\n')[0]?.trim(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with πΒ / π.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The existing process for reading .node-version (parse_node_version_content) is also designed to read only the first line without excluding comments. Therefore, this implementation follows the same limitations to maintain consistency across the codebase. |
||
|
|
||
| if (!version) { | ||
| return null; | ||
| } | ||
|
|
||
| // Unsupported nvm aliases that have no direct version equivalent | ||
| if (['node', 'stable', 'iojs', 'system', 'default'].includes(version)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can convert all these to |
||
| return null; | ||
| } | ||
|
|
||
| // LTS aliases (lts/*, lts/iron, etc.) pass through as-is | ||
| if (version.startsWith('lts/')) { | ||
| return version; | ||
| } | ||
|
|
||
| // Strip optional 'v' prefix, then validate as a semver version or range | ||
| const normalized = version.startsWith('v') ? version.slice(1) : version; | ||
| if (!normalized || !semver.validRange(normalized)) { | ||
| return null; | ||
| } | ||
| return normalized; | ||
| } | ||
|
|
||
| /** | ||
| * Migrate .nvmrc to .node-version and remove .nvmrc. | ||
| * Returns true on success, false if migration was skipped or failed. | ||
| */ | ||
| export function migrateNodeVersionManagerFile( | ||
| projectPath: string, | ||
| _detection: NodeVersionManagerDetection, | ||
| report?: MigrationReport, | ||
| ): boolean { | ||
| const sourcePath = path.join(projectPath, '.nvmrc'); | ||
| const nodeVersionPath = path.join(projectPath, '.node-version'); | ||
| const content = fs.readFileSync(sourcePath, 'utf8'); | ||
| const version = parseNvmrcVersion(content); | ||
|
|
||
| if (!version) { | ||
| warnMigration( | ||
| '.nvmrc contains an unsupported version alias. Create .node-version manually with your desired Node.js version.', | ||
| report, | ||
| ); | ||
| return false; | ||
| } | ||
|
|
||
| fs.writeFileSync(nodeVersionPath, `${version}\n`); | ||
| fs.unlinkSync(sourcePath); | ||
|
|
||
| if (report) { | ||
| report.nodeVersionFileMigrated = true; | ||
| } | ||
| return true; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Vite+, the Node.js version is determined based on the following priority order:
The sequence is ".node-version", followed by "engines.node" in package.json, and then "devEngines.runtime" in package.json.
For this reason, we are treating the migration of the ".node-version" file as the highest priority in this migration process.
vite-plus/crates/vite_global_cli/src/commands/env/config.rs
Lines 197 to 207 in d2e4dfd