Fix CLI node version check (#5905)

This commit is contained in:
Bjorn Lu 2023-01-20 15:37:42 +08:00 committed by Matthew Phillips
parent f135b0d94b
commit b2ce69704b
2 changed files with 33 additions and 52 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix CLI node version check

View file

@ -4,7 +4,6 @@
// ISOMORPHIC FILE: NO TOP-LEVEL IMPORT/REQUIRE() ALLOWED // ISOMORPHIC FILE: NO TOP-LEVEL IMPORT/REQUIRE() ALLOWED
// This file has to run as both ESM and CJS on older Node.js versions // This file has to run as both ESM and CJS on older Node.js versions
// Assume ESM to start, and then call `require()` below once CJS is confirmed.
// Needed for Stackblitz: https://github.com/stackblitz/webcontainer-core/issues/281 // Needed for Stackblitz: https://github.com/stackblitz/webcontainer-core/issues/281
const CI_INSTRUCTIONS = { const CI_INSTRUCTIONS = {
@ -14,64 +13,41 @@ const CI_INSTRUCTIONS = {
VERCEL: 'https://vercel.com/docs/runtimes#official-runtimes/node-js/node-js-version', VERCEL: 'https://vercel.com/docs/runtimes#official-runtimes/node-js/node-js-version',
}; };
// Hardcode supported Node.js version so we don't have to read differently in CJS & ESM.
const engines = '>=16.12.0';
const skipSemverCheckIfAbove = 16;
/** `astro *` */ /** `astro *` */
async function main() { async function main() {
// Check for ESM support.
// Load the "supports-esm" package in an way that works in both ESM & CJS.
let supportsESM =
typeof require !== 'undefined'
? require('supports-esm')
: (await import('supports-esm')).default;
// Check for CJS->ESM named export support.
// "path-to-regexp" is a real-world package that we depend on, that only
// works in later versions of Node with advanced CJS->ESM support.
// If `import {compile} from 'path-to-regexp'` will fail, we need to know.
if (supportsESM) {
const testNamedExportsModule = await import('path-to-regexp');
supportsESM = !!testNamedExportsModule.compile;
}
// Preflight check complete. Enjoy! ✨
if (supportsESM) {
return import('./dist/cli/index.js')
.then(({ cli }) => cli(process.argv))
.catch((error) => {
console.error(error);
process.exit(1);
});
}
const version = process.versions.node; const version = process.versions.node;
// Fast-path for higher Node.js versions
// Not supported (incomplete ESM): It's very difficult (impossible?) to load the if ((parseInt(version) || 0) <= skipSemverCheckIfAbove) {
// dependencies below in an unknown module type. If `require` is undefined, then this file try {
// actually was run as ESM but one of the ESM preflight checks above failed. In that case, const semver = await import('semver');
// it's okay to hard-code the valid Node versions here since they will not change over time. if (!semver.satisfies(version, engines)) {
if (typeof require === 'undefined') { await errorNodeUnsupported();
console.error(`\nNode.js v${version} is not supported by Astro! return;
Please upgrade to a supported version of Node.js: ">=16.12.0"\n`); }
} catch {
await errorNodeUnsupported();
return;
}
} }
// Not supported: Report the most helpful error message possible. return import('./dist/cli/index.js')
const pkg = require('./package.json'); .then(({ cli }) => cli(process.argv))
const ci = require('ci-info'); .catch((error) => {
const semver = require('semver'); console.error(error);
const engines = pkg.engines.node; process.exit(1);
});
}
// TODO: Remove "semver" in Astro v1.0: This is mainly just to check our work. Once run in async function errorNodeUnsupported() {
// the wild for a bit without error, we can assume our engine range is correct and won't console.error(`\
// change over time. Node.js v${process.versions.node} is not supported by Astro!
const isSupported = semver.satisfies(version, engines);
if (isSupported) {
console.error(`\nNode.js v${version} is not supported by Astro!
Supported versions: ${engines}\n
Issue Detected! This Node.js version was expected to work, but failed a system check.
Please file an issue so that we can take a look: https://github.com/withastro/astro/issues/new\n`);
} else {
console.error(`\nNode.js v${version} is not supported by Astro!
Please upgrade Node.js to a supported version: "${engines}"\n`); Please upgrade Node.js to a supported version: "${engines}"\n`);
}
const ci = typeof require !== 'undefined' ? require('ci-info') : await import('ci-info');
// Special instructions for CI environments, which may have special steps needed. // Special instructions for CI environments, which may have special steps needed.
// This is a common issue that we can help users with proactively. // This is a common issue that we can help users with proactively.