Skip to content

feat: add a start-up check for minimum node and npm versions #866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/state/InitState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*
*/

import semver from'semver';
import shell from 'shelljs';
import { configDotenv } from 'dotenv';
import { readFileSync, writeFileSync } from 'fs';
import path, { join } from 'path';
Expand Down Expand Up @@ -119,8 +121,9 @@ export class InitState implements IState{
const isCorrectDockerComposeVersion = await this.dockerService.isCorrectDockerComposeVersion();
const isDockerStarted = await this.dockerService.checkDocker();
const dockerHasEnoughResources = await this.dockerService.checkDockerResources(this.cliOptions.multiNode);
const areNodeAndNpmVersionsValid = this.checkNodeAndNpmVersions();

if (!(isCorrectDockerComposeVersion && isDockerStarted && dockerHasEnoughResources)) {
if (!(isCorrectDockerComposeVersion && isDockerStarted && dockerHasEnoughResources) && areNodeAndNpmVersionsValid) {
this.observer!.update(EventType.UnresolvableError);
return;
}
Expand All @@ -145,6 +148,27 @@ export class InitState implements IState{
this.observer!.update(EventType.Finish);
}

private checkNodeAndNpmVersions(): boolean {
const MIN_NODE_VERSION = '17.9.1';
const MIN_NPM_VERSION = '8.11.0';
const V_OR_NEW_LINE_REGEX = /v|\n/g;

const nodeVersion = shell.exec(`node -v `, { silent: true }).replace(V_OR_NEW_LINE_REGEX, '');
const npmVersion = shell.exec(`npm -v `, { silent: true }).replace(V_OR_NEW_LINE_REGEX, '');

const isNodeVersionValid = semver.gte(nodeVersion, MIN_NODE_VERSION);
const isNpmVersionValid = semver.gte(npmVersion, MIN_NPM_VERSION);

if (!isNodeVersionValid) {
this.logger.error(`Current node version is ${nodeVersion} but minimum required one is ${MIN_NODE_VERSION}`);
}
if (!isNpmVersionValid) {
this.logger.error(`Current npm version is ${npmVersion} but minimum required one is ${MIN_NPM_VERSION}`);
}

return isNodeVersionValid && isNpmVersionValid;
}

/**
* Prepares the work directory.
*
Expand Down
1 change: 1 addition & 0 deletions test/unit/states/InitState.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ describe('InitState tests', () => {
dockerService.isCorrectDockerComposeVersion.resolves(true)
dockerService.checkDocker.resolves(true)
dockerService.checkDockerResources.resolves(true)
testSandbox.stub(initState, 'checkNodeAndNpmVersions').returns(true);

await initState.onStart();

Expand Down
Loading