Skip to content

Commit

Permalink
fix: Ensure CLI works out-of-the-box on Node 16+ (#1774)
Browse files Browse the repository at this point in the history
* fix: Ensure CLI works out-of-the-box on Node 16+

* fix: I can't spell

* docs: Adding changeset

* test: Fixing test images (unrelated)
  • Loading branch information
rschristian committed Jan 14, 2023
1 parent 783bb01 commit 05ba4b2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/fair-teachers-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-cli': patch
---

Injects necessary Node options config into new projects if user's Node version is 16+
10 changes: 10 additions & 0 deletions packages/cli/src/commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const {
warn,
dirExists,
normalizeTemplatesResponse,
isNodeVersionGreater,
} = require('../util');
const {
CUSTOM_TEMPLATE,
Expand Down Expand Up @@ -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]));
Expand Down
10 changes: 3 additions & 7 deletions packages/cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
);
}

Expand Down
10 changes: 10 additions & 0 deletions packages/cli/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
14 changes: 7 additions & 7 deletions packages/cli/tests/images/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down

0 comments on commit 05ba4b2

Please sign in to comment.