UNPKG

node-version-use

Version:

Cross-platform solution for using multiple versions of node. Useful for compatibility testing

150 lines 7.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, /** * nvu default [version] * * Set or display the global default Node version. * This is used when no .nvmrc or .nvurc is found in the project. */ "default", { enumerable: true, get: function() { return defaultCmd; } }); var _exitcompat = /*#__PURE__*/ _interop_require_default(require("exit-compat")); var _fs = /*#__PURE__*/ _interop_require_default(require("fs")); var _module = /*#__PURE__*/ _interop_require_default(require("module")); var _path = /*#__PURE__*/ _interop_require_default(require("path")); var _compatts = require("../compat.js"); var _constantsts = require("../constants.js"); var _findInstalledVersionsts = require("../lib/findInstalledVersions.js"); var _loadNodeVersionInstallts = /*#__PURE__*/ _interop_require_default(require("../lib/loadNodeVersionInstall.js")); function _interop_require_default(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var _require = typeof require === 'undefined' ? _module.default.createRequire(require("url").pathToFileURL(__filename).toString()) : require; var syncAllShims = _require('../assets/installBinaries.cjs').syncAllShims; function defaultCmd(args) { var defaultFilePath = _path.default.join(_constantsts.storagePath, 'default'); var versionsPath = _path.default.join(_constantsts.storagePath, 'installed'); var binDir = _path.default.join(_constantsts.storagePath, 'bin'); // If no version provided, display current default if (args.length === 0) { if (_fs.default.existsSync(defaultFilePath)) { var currentVersion = _fs.default.readFileSync(defaultFilePath, 'utf8').trim(); console.log("Current default: ".concat(currentVersion)); } else { console.log('No default version set.'); console.log('Usage: nvu default <version>'); } (0, _exitcompat.default)(0); return; } var version = args[0].trim(); // Special case: "system" means use system Node (no installation needed) if (version === 'system') { _fs.default.writeFileSync(defaultFilePath, 'system\n', 'utf8'); console.log('Default Node version set to: system'); (0, _exitcompat.default)(0); return; } // Validate version format (basic check, indexOf for Node 0.8+ compat) if (!version || version.indexOf('-') === 0) { console.log('Usage: nvu default <version>'); console.log('Example: nvu default 20'); (0, _exitcompat.default)(1); return; } // Ensure storage directory exists if (!_fs.default.existsSync(_constantsts.storagePath)) { (0, _compatts.mkdirpSync)(_constantsts.storagePath); } // Check if any installed versions match var matches = (0, _findInstalledVersionsts.findInstalledVersions)(versionsPath, version); if (matches.length > 0) { // Version is installed - resolve to exact and set default setDefaultToExact(defaultFilePath, matches, binDir); } else { // Version not installed - auto-install it console.log("Node ".concat(version, " is not installed. Installing...")); autoInstallAndSetDefault(version, versionsPath, defaultFilePath, binDir); } } /** * Set the default to the highest matching installed version */ function setDefaultToExact(defaultFilePath, matches, binDir) { // matches are sorted by findInstalledVersions, take the last (highest) var exactVersion = matches[matches.length - 1]; // Ensure it has v prefix for consistency if (exactVersion.indexOf('v') !== 0) { exactVersion = "v".concat(exactVersion); } // Write the exact version _fs.default.writeFileSync(defaultFilePath, "".concat(exactVersion, "\n"), 'utf8'); console.log("Default Node version set to: ".concat(exactVersion)); // Sync all shims (first time setup) syncAllShimsIfNeeded(binDir); (0, _exitcompat.default)(0); } /** * Auto-install the version and then set it as default */ function autoInstallAndSetDefault(version, versionsPath, defaultFilePath, binDir) { (0, _loadNodeVersionInstallts.default)(function(err, nodeVersionInstall) { if (err || !nodeVersionInstall) { console.error('Failed to load node-version-install:', err ? err.message : 'Module not available'); (0, _exitcompat.default)(1); return; } nodeVersionInstall(version, { installPath: versionsPath }, function(installErr, results) { if (installErr) { console.error("Failed to install Node ".concat(version, ":"), installErr.message); (0, _exitcompat.default)(1); return; } // Get the installed version from results var installedVersion; if (results && results.length > 0) { installedVersion = results[0].version; } else { // Fallback: re-scan installed versions var matches = (0, _findInstalledVersionsts.findInstalledVersions)(versionsPath, version); if (matches.length === 0) { console.error('Installation completed but version not found'); (0, _exitcompat.default)(1); return; } installedVersion = matches[matches.length - 1]; } // Ensure it has v prefix for consistency if (installedVersion.indexOf('v') !== 0) { installedVersion = "v".concat(installedVersion); } // Write the exact version _fs.default.writeFileSync(defaultFilePath, "".concat(installedVersion, "\n"), 'utf8'); console.log("Node ".concat(installedVersion, " installed successfully.")); console.log("Default Node version set to: ".concat(installedVersion)); // Sync all shims (first time setup) syncAllShimsIfNeeded(binDir); (0, _exitcompat.default)(0); }); }); } /** * Sync all shims if this is the first time setting a default * First time is detected by checking if ~/.nvu/bin/nvu exists */ function syncAllShimsIfNeeded(binDir) { var _process_env_OSTYPE; var isWindows = process.platform === 'win32' || /^(msys|cygwin)$/.test((_process_env_OSTYPE = process.env.OSTYPE) !== null && _process_env_OSTYPE !== void 0 ? _process_env_OSTYPE : ''); var nvuPath = _path.default.join(binDir, "nvu".concat(isWindows ? '.exe' : '')); // Only sync if nvu binary exists (first time setup) if (_fs.default.existsSync(nvuPath)) { syncAllShims(binDir); } } /* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }