From 1dd6e108f968128301d72b5b02d2e70cacd5d01b Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Fri, 20 Sep 2024 10:59:30 -0600 Subject: [PATCH] fix: warn about node version mismatch --- package.json | 1 + src/index.ts | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/package.json b/package.json index 887e4bbbc..b2eb51f54 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "is-wsl": "^2.2.0", "lilconfig": "^3.1.2", "minimatch": "^9.0.5", + "semver": "^7.6.3", "string-width": "^4.2.3", "supports-color": "^8", "widest-line": "^3.1.0", diff --git a/src/index.ts b/src/index.ts index 417f337ed..b014f5ae6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +import {isTruthy} from './util/util' function checkCWD() { try { process.cwd() @@ -8,7 +9,25 @@ function checkCWD() { } } +function checkNodeVersion() { + if (process.env.OCLIF_DISABLE_ENGINE_WARNING && isTruthy(process.env.OCLIF_DISABLE_ENGINE_WARNING)) return + try { + const semver = require('semver') + const path = require('node:path') + const root = path.join(__dirname, '..') + const pjson = require(path.join(root, 'package.json')) + if (!semver.satisfies(process.versions.node, pjson.engines.node)) { + process.emitWarning( + `Node version must be ${pjson.engines.node} to use this CLI. Current node version: ${process.versions.node}`, + ) + } + } catch { + // ignore + } +} + checkCWD() +checkNodeVersion() export * as Args from './args' export {Command} from './command'