From 51cc5781409e176f3c431e2e278c1aeece10e168 Mon Sep 17 00:00:00 2001 From: Romuald Brillout Date: Fri, 1 Mar 2024 16:26:02 +0100 Subject: [PATCH] minor refactor --- vike/node/plugin/index.ts | 4 ++-- vike/node/plugin/utils.ts | 2 +- vike/utils/assertNodeVersion.ts | 7 +++---- .../{isVersionOrAbove.ts => assertVersion.ts} | 14 +++++++++++++- 4 files changed, 19 insertions(+), 8 deletions(-) rename vike/utils/{isVersionOrAbove.ts => assertVersion.ts} (64%) diff --git a/vike/node/plugin/index.ts b/vike/node/plugin/index.ts index 4d547fd05ee..31cc149b393 100644 --- a/vike/node/plugin/index.ts +++ b/vike/node/plugin/index.ts @@ -6,7 +6,7 @@ export type { ConfigVikeUserProvided as UserConfig } export { PROJECT_VERSION as version } from './utils.js' import { version, type Plugin } from 'vite' -import { assertNodeEnv_onVikePluginLoad, assertUsage, isVersionOrAbove, markEnvAsVikePluginLoaded } from './utils.js' +import { assertNodeEnv_onVikePluginLoad, assertUsage, assertVersion, markEnvAsVikePluginLoaded } from './utils.js' import { buildConfig } from './plugins/buildConfig.js' import { previewConfig } from './plugins/previewConfig.js' import { autoFullBuild } from './plugins/autoFullBuild.js' @@ -80,5 +80,5 @@ Object.defineProperty(plugin, 'apply', { // node_modules/vike/package.json#peerDependencies.vite isn't enough as users can ignore it function assertViteVersion() { - assertUsage(isVersionOrAbove(version, '4.4.0'), `Vite ${version} isn't supported, use Vite >= 4.4.0 instead.`) + assertVersion('Vite', version, '4.4.0') } diff --git a/vike/node/plugin/utils.ts b/vike/node/plugin/utils.ts index df71d4fd3a0..833083d291c 100644 --- a/vike/node/plugin/utils.ts +++ b/vike/node/plugin/utils.ts @@ -28,4 +28,4 @@ export * from '../../utils/assertKeys.js' export * from '../../utils/injectRollupInputs.js' export * from '../../utils/humanizeTime.js' export * from '../../utils/pLimit.js' -export * from '../../utils/isVersionOrAbove.js' +export * from '../../utils/assertVersion.js' diff --git a/vike/utils/assertNodeVersion.ts b/vike/utils/assertNodeVersion.ts index 37779da68e8..b248a1d70bc 100644 --- a/vike/utils/assertNodeVersion.ts +++ b/vike/utils/assertNodeVersion.ts @@ -1,12 +1,11 @@ export { assertNodeVersion } -import { assertUsage } from './assert.js' import { isNodeJS } from './isNodeJS.js' -import { isVersionOrAbove } from './isVersionOrAbove.js' +import { assertVersion } from './assertVersion.js' -// package.json#engines.node isn't enough as users can ignore it +// node_modules/vike/package.json#engines.node isn't enough as users can ignore it function assertNodeVersion() { if (!isNodeJS()) return const version = process.versions.node - assertUsage(isVersionOrAbove(version, '16.0.0'), `Node.js ${version} isn't supported, use Node.js >=16.0.0 instead.`) + assertVersion('Node.js', version, '16.0.0') } diff --git a/vike/utils/isVersionOrAbove.ts b/vike/utils/assertVersion.ts similarity index 64% rename from vike/utils/isVersionOrAbove.ts rename to vike/utils/assertVersion.ts index 29f7cf96b27..c3c28cd6136 100644 --- a/vike/utils/isVersionOrAbove.ts +++ b/vike/utils/assertVersion.ts @@ -1,6 +1,18 @@ +export { assertVersion } export { isVersionOrAbove } -import { assert } from './assert.js' +import { assert, assertUsage } from './assert.js' + +function assertVersion( + dependencyName: 'Vite' | 'Node.js', + versionActual: string, + versionExpected: `${number}.${number}.${number}` +) { + assertUsage( + isVersionOrAbove(versionActual, versionExpected), + `${dependencyName} ${versionActual} isn't supported, use ${dependencyName} >= ${versionExpected} instead.` + ) +} function isVersionOrAbove(versionActual: string, versionExpected: `${number}.${number}.${number}`): boolean { const p1 = parseVersion(versionActual)