Skip to content
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

Node Version Validation #1036

Merged
merged 1 commit into from
Jan 6, 2025
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
1 change: 1 addition & 0 deletions project/gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ const writeBuildDataToJSON = async (entryType) => {

const buildInfo = {};
buildInfo.entryType = entryType;
buildInfo.expectedNode = manifest?.engines?.node ?? "";
buildInfo.sptVersion = coreParsed.sptVersion;
buildInfo.commit = coreParsed.commit;
buildInfo.buildTime = coreParsed.buildTime;
Expand Down
15 changes: 15 additions & 0 deletions project/src/ProgramStatics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import buildInfo from "@spt/entry/build.json" assert { type: "json" };
import fs from "fs-extra";
import { EntryType } from "./models/enums/EntryType";

// biome-ignore lint/complexity/noStaticOnlyClass:
Expand All @@ -9,12 +10,23 @@ export class ProgramStatics {
private static _COMPILED: boolean;
private static _MODS: boolean;

private static _EXPECTED_NODE: string;
private static _SPT_VERSION: string;
private static _COMMIT: string;
private static _BUILD_TIME: number;

public static initialize(): void {
ProgramStatics._ENTRY_TYPE = buildInfo.entryType as EntryType;

// If running the local entry, the expected node version can be fetched from the package.json file. In built
// entries, the expected node version is set at build and can be fetched from the build.json file.
if (ProgramStatics._ENTRY_TYPE === EntryType.LOCAL) {
const packageInfo = JSON.parse(fs.readFileSync("./package.json", "utf8"));
ProgramStatics._EXPECTED_NODE = packageInfo?.engines?.node ?? "";
} else {
ProgramStatics._EXPECTED_NODE = buildInfo.expectedNode;
}

ProgramStatics._SPT_VERSION = buildInfo.sptVersion ?? "";
ProgramStatics._COMMIT = buildInfo.commit ?? "";
ProgramStatics._BUILD_TIME = buildInfo.buildTime ?? 0;
Expand Down Expand Up @@ -57,6 +69,9 @@ export class ProgramStatics {
public static get MODS(): boolean {
return ProgramStatics._MODS;
}
public static get EXPECTED_NODE(): string {
return ProgramStatics._EXPECTED_NODE;
}
public static get SPT_VERSION(): string {
return ProgramStatics._SPT_VERSION;
}
Expand Down
1 change: 1 addition & 0 deletions project/src/entry/build.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"entryType": "LOCAL",
"expectedNode": "",
"sptVersion": "",
"commit": "",
"buildTime": 0
Expand Down
9 changes: 9 additions & 0 deletions project/src/utils/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ export class App {
this.logger.debug(`PATH: ${this.encodingUtil.toBase64(process.execPath)}`);
this.logger.debug(`Server: ${ProgramStatics.SPT_VERSION || this.coreConfig.sptVersion}`);

const nodeVersion = process.version.replace(/^v/, "");
if (ProgramStatics.EXPECTED_NODE && nodeVersion !== ProgramStatics.EXPECTED_NODE) {
this.logger.error(
`Node version mismatch. Required: ${ProgramStatics.EXPECTED_NODE} | Current: ${nodeVersion}`,
);
process.exit(1);
}
this.logger.debug(`Node: ${nodeVersion}`);

if (ProgramStatics.BUILD_TIME) {
this.logger.debug(`Date: ${ProgramStatics.BUILD_TIME}`);
}
Expand Down
Loading