Skip to content

Commit

Permalink
feat: show a message to upgrade node when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
LiKang6688 committed Nov 8, 2022
1 parent 3d9e77b commit 6286686
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ If the proposal includes new designs or bigger changes, please be prepared to di

### Prerequisites

- [Node.js](https://nodejs.org/) 10+ and [yarn](https://yarnpkg.com) 1.9.4 installed
- [Node.js](https://nodejs.org/) 18+ and [yarn](https://yarnpkg.com) 1+ installed

### Project structure

Expand Down
7 changes: 3 additions & 4 deletions commands/cli/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#!/usr/bin/env node
const yargs = require('yargs');
const importCwd = require('import-cwd');
const checkNodeVersion = require('../utils/checkNodeVersion');
const pkg = require('../package.json');

// const build = require('@nebula.js/cli-build/command');
// const create = require('@nebula.js/cli-create/command');
// const serve = require('@nebula.js/cli-serve/command');
// const sense = require('@nebula.js/cli-sense/command');
checkNodeVersion(pkg);

yargs.usage('nebula <command> [options]');

Expand Down
3 changes: 3 additions & 0 deletions commands/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"files": [
"lib"
],
"engines": {
"node": ">=18.0.0"
},
"bin": {
"nebula": "lib/index.js"
},
Expand Down
28 changes: 28 additions & 0 deletions commands/cli/utils/checkNodeVersion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function checkNodeVersion(pkg) {
if (pkg.engines && pkg.engines.node) {
const minVersion = pkg.engines.node.replace('>=', '');
const [minMajor, minMinor, minPatch] = minVersion.split('.').map(Number);
const currentVersion = process.versions.node;
const [currentMajor, currentMinor, currentPatch] = currentVersion.split('.').map(Number);

let validVersion = true;
if (currentMajor < minMajor) {
validVersion = false;
} else if (currentMajor === minMajor) {
if (currentMinor < minMinor) {
validVersion = false;
}
if (currentMinor === minMinor && currentPatch < minPatch) {
validVersion = false;
}
}

if (!validVersion) {
// eslint-disable-next-line no-console
console.error(`${pkg.name} requires NodeJS >= ${minVersion}, but you are using NodeJS ${currentVersion}.`);
process.exit(1);
}
}
}

module.exports = checkNodeVersion;
3 changes: 3 additions & 0 deletions commands/serve/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"dist",
"lib"
],
"engines": {
"node": ">=18.0.0"
},
"scripts": {
"build": "cross-env NODE_ENV=production DEFAULTS=true webpack --config ./lib/webpack.build.js",
"build:dev": "cross-env NODE_ENV=development DEFAULTS=true webpack --config ./lib/webpack.build.js",
Expand Down

0 comments on commit 6286686

Please sign in to comment.