diff --git a/package.json b/package.json index f32dff1..2aab020 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "typescript": "^5.3.3" }, "engines": { - "node": ">=18" + "node": "^18 || ^20 || ^22" }, "files": [ "/bin", diff --git a/src/index.js b/src/index.js index 8d76e7d..e21edb1 100644 --- a/src/index.js +++ b/src/index.js @@ -11,6 +11,8 @@ */ const { Command, run, Config } = require('@oclif/core') +const semver = require('semver') +const chalk = require('chalk') class AIOCommand extends Command { } @@ -25,6 +27,12 @@ AIOCommand.run = async (argv, opts) => { // || module.parent && module.parent.parent && module.parent.parent.filename const config = await Config.load(opts || __dirname) + // Check Node.js version + const nodeVersion = process.version + if (!semver.satisfies(nodeVersion, config.pjson.engines.node)) { + console.log(chalk.yellow(`⚠️ Warning: Node.js version ${nodeVersion} is not supported. Supported versions are ${config.pjson.engines.node}.`)) + } + // the second parameter is the root path to the CLI containing the command try { return await run(argv, config.options) diff --git a/test/hookerror.test.js b/test/hookerror.test.js index 96c9c14..e9e9e6c 100644 --- a/test/hookerror.test.js +++ b/test/hookerror.test.js @@ -28,7 +28,13 @@ jest.mock('@oclif/core', () => { Config: { load: () => { return { - runHook: mockRunHook + pjson: { + engines: { + node: '>=18 <23' + } + }, + runHook: mockRunHook, + options: {} } } }, diff --git a/test/index.test.js b/test/index.test.js index 64e9bce..31e2709 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -16,7 +16,14 @@ jest.mock('@oclif/core', () => { return { ...jest.requireActual('@oclif/core'), Config: { - load: () => ({}) + load: () => ({ + pjson: { + engines: { + node: '>=18 <23' + } + }, + options: {} + }) }, Command: jest.fn(), run: async function (cmd) { @@ -45,3 +52,47 @@ describe('run command', () => { process.argv = temp }) }) + +describe('Node.js version check', () => { + const originalVersion = process.version + let logSpy + + beforeEach(() => { + logSpy = jest.spyOn(console, 'log').mockImplementation() + }) + + afterEach(() => { + jest.restoreAllMocks() + Object.defineProperty(process, 'version', { + value: originalVersion + }) + }) + + test('should not show warning for supported Node.js version', async () => { + Object.defineProperty(process, 'version', { + value: 'v22.14.0' + }) + + const AIOCommand = require('../src/index') + await AIOCommand.run(['--version']) + + // Check warning is not displayed + expect(logSpy).not.toHaveBeenCalledWith( + expect.stringContaining('Warning: Node.js version') + ) + }) + + test('should show warning for unsupported Node.js version', async () => { + Object.defineProperty(process, 'version', { + value: 'v23.0.0' + }) + + const AIOCommand = require('../src/index') + await AIOCommand.run(['--version']) + + // Check warning is displayed + expect(logSpy).toHaveBeenCalledWith( + expect.stringContaining('Warning: Node.js version v23.0.0 is not supported') + ) + }) +})