From 05ba4b2ea5247877475bffb262c996d28fa7da9b Mon Sep 17 00:00:00 2001 From: Ryan Christian <33403762+rschristian@users.noreply.github.com> Date: Sat, 14 Jan 2023 13:14:14 -0600 Subject: [PATCH] fix: Ensure CLI works out-of-the-box on Node 16+ (#1774) * fix: Ensure CLI works out-of-the-box on Node 16+ * fix: I can't spell * docs: Adding changeset * test: Fixing test images (unrelated) --- .changeset/fair-teachers-marry.md | 5 +++++ packages/cli/src/commands/create.js | 10 ++++++++++ packages/cli/src/index.js | 10 +++------- packages/cli/src/util.js | 10 ++++++++++ packages/cli/tests/images/build.js | 14 +++++++------- 5 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 .changeset/fair-teachers-marry.md diff --git a/.changeset/fair-teachers-marry.md b/.changeset/fair-teachers-marry.md new file mode 100644 index 000000000..e6b894e7d --- /dev/null +++ b/.changeset/fair-teachers-marry.md @@ -0,0 +1,5 @@ +--- +'preact-cli': patch +--- + +Injects necessary Node options config into new projects if user's Node version is 16+ diff --git a/packages/cli/src/commands/create.js b/packages/cli/src/commands/create.js index eccb6e6f3..3b6f781a4 100644 --- a/packages/cli/src/commands/create.js +++ b/packages/cli/src/commands/create.js @@ -19,6 +19,7 @@ const { warn, dirExists, normalizeTemplatesResponse, + isNodeVersionGreater, } = require('../util'); const { CUSTOM_TEMPLATE, @@ -313,6 +314,15 @@ exports.create = async function createCommand(repo, dest, argv) { spinner.text = 'Updating `name` within `package.json` file'; pkgData.name = argv.name.toLowerCase().replace(/\s+/g, '_'); } + + if (repo.startsWith(ORG) && isNodeVersionGreater('16.0.0')) { + pkgData.scripts.build = + 'cross-env NODE_OPTIONS=--openssl-legacy-provider preact build'; + pkgData.scripts.dev = + 'cross-env NODE_OPTIONS=--openssl-legacy-provider preact watch'; + pkgData.devDependencies['cross-env'] = '^7.0.3'; + } + // Find a `manifest.json`; use the first match, if any let files = await glob(target + '/**/manifest.json'); let manifest = files[0] && JSON.parse(await readFile(files[0])); diff --git a/packages/cli/src/index.js b/packages/cli/src/index.js index 9773a06ba..e6e7f4863 100755 --- a/packages/cli/src/index.js +++ b/packages/cli/src/index.js @@ -4,16 +4,12 @@ const sade = require('sade'); const notifier = require('update-notifier'); const { error } = require('./util'); const pkg = require('../package.json'); +const { isNodeVersionGreater } = require('./util'); -const ver = process.version; const min = pkg.engines.node; -if ( - ver - .substring(1) - .localeCompare(min.match(/\d+/g).join('.'), 'en', { numeric: true }) === -1 -) { +if (!isNodeVersionGreater(min)) { error( - `You are using Node ${ver} but preact-cli requires Node ${min}. Please upgrade Node to continue!\n` + `You are using Node ${process.version} but preact-cli requires Node ${min}. Please upgrade Node to continue!\n` ); } diff --git a/packages/cli/src/util.js b/packages/cli/src/util.js index 7260bfeca..dc26b45ce 100644 --- a/packages/cli/src/util.js +++ b/packages/cli/src/util.js @@ -56,6 +56,16 @@ exports.toBool = function (val) { return val !== void 0 && val !== false && !/false|0/.test(val); }; +exports.isNodeVersionGreater = function (minimum) { + return ( + process.version + .substring(1) + .localeCompare(minimum.match(/\d+/g).join('.'), 'en', { + numeric: true, + }) === 1 + ); +}; + exports.esmImport = require('esm')(module); /** diff --git a/packages/cli/tests/images/build.js b/packages/cli/tests/images/build.js index d3b4e335b..431c8f244 100644 --- a/packages/cli/tests/images/build.js +++ b/packages/cli/tests/images/build.js @@ -13,23 +13,23 @@ exports.default = Object.assign({}, common, { 'assets/preact-logo.svg': 645, 'assets/preact-logo-inverse.svg': 649, 'assets/favicon.ico': 15086, - 'ssr-build/ssr-bundle.2b941.css': 2601, - 'ssr-build/ssr-bundle.2b941.css.map': 3708, + 'ssr-build/ssr-bundle.82489.css': 2601, + 'ssr-build/ssr-bundle.82489.css.map': 3708, 'ssr-build/ssr-bundle.js': 28830, 'ssr-build/ssr-bundle.js.map': 52686, 'ssr-build/asset-manifest.json': 178, 'bundle.4c6ac.css': 1276, - 'bundle.fbf1b.js': 22529, - 'bundle.fbf1b.js.map': 111801, + 'bundle.c4895.js': 22529, + 'bundle.c4895.js.map': 111801, 'favicon.ico': 15086, 'index.html': 4127, 'manifest.json': 455, 'preact_prerender_data.json': 11, 'push-manifest.json': 388, 'asset-manifest.json': 1142, - 'route-home.chunk.c490f.css': 834, - 'route-home.chunk.50bd0.js': 1149, - 'route-home.chunk.50bd0.js.map': 1961, + 'route-home.chunk.9bcab.css': 834, + 'route-home.chunk.5d28d.js': 1149, + 'route-home.chunk.5d28d.js.map': 1961, 'route-profile.chunk.4c330.js': 3490, 'route-profile.chunk.4c330.js.map': 15002, });