nodesync-cli
Version:
A smart CLI tool to auto-detect, install, and manage the right Node.js version for your project.
36 lines (31 loc) • 1.09 kB
JavaScript
import fs from 'fs';
import path from 'path';
export async function detectVersion(projectRoot = process.cwd()) {
// 1. Check .nvmrc
const nvmrcPath = path.join(projectRoot, '.nvmrc');
if (fs.existsSync(nvmrcPath)) {
const version = fs.readFileSync(nvmrcPath, 'utf-8').trim();
if (version) {
return { source: '.nvmrc', version };
}
}
// 2. Check package.json → engines.node
const pkgJsonPath = path.join(projectRoot, 'package.json');
if (fs.existsSync(pkgJsonPath)) {
const pkg = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf-8'));
if (pkg.engines && pkg.engines.node) {
return { source: 'package.json', version: pkg.engines.node };
}
}
// 3. Check Dockerfile → FROM node:X.Y.Z
const dockerfilePath = path.join(projectRoot, 'Dockerfile');
if (fs.existsSync(dockerfilePath)) {
const content = fs.readFileSync(dockerfilePath, 'utf-8');
const match = content.match(/FROM\s+node:(\d+\.\d+\.\d+)/i);
if (match) {
return { source: 'Dockerfile', version: match[1] };
}
}
// Nothing found
return null;
}