Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.

Commit ed10299

Browse files
committed
feat(install): check whether the nodecg-io version is compatible with the used NodeCG version
1 parent 8ff3530 commit ed10299

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

integration_test.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ function clean_test_directory {
2222
trap clean_test_directory EXIT
2323

2424
cd $dir
25-
git clone https://github.com/nodecg/nodecg.git --depth 1 .
25+
26+
# Download NodeCG
27+
if [ "$version" == "0.1" ] || [ "$version" == "0.2" ]; then
28+
# if version is 0.1 or 0.2 then switch back to NodeCG v1
29+
git clone https://github.com/nodecg/nodecg.git --depth 1 --branch legacy-1.x .
30+
else
31+
git clone https://github.com/nodecg/nodecg.git --depth 1 .
32+
fi
2633

2734
if [ "$version" == "development" ]; then
2835
nodecg-io install --nodecg-io-version $version --docs

src/install/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ async function install(opts: InstallCommandOptions): Promise<void> {
9191
requestedInstall,
9292
currentInstall && !currentInstall.dev ? currentInstall : undefined,
9393
nodecgIODir,
94+
nodecgDir,
9495
);
9596
}
9697

src/install/production.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ import { logger } from "../utils/log";
1414
import { promises as fs } from "fs";
1515
import path = require("path");
1616
import chalk = require("chalk");
17+
import { SemVer } from "semver";
18+
import { getNodeCGVersion } from "../utils/nodecgInstallation";
1719

1820
export async function createProductionInstall(
1921
requested: ProductionInstallation,
2022
current: ProductionInstallation | undefined,
2123
nodecgIODir: string,
24+
nodecgDir: string,
2225
): Promise<void> {
26+
const nodecgVersion = await getNodeCGVersion(nodecgDir);
27+
await checkNodeCGCompatibility(requested, nodecgVersion);
2328
await ensureDirectory(nodecgIODir);
2429

2530
if (current) {
@@ -41,6 +46,27 @@ export async function createProductionInstall(
4146
}
4247
}
4348

49+
/**
50+
* Checks whether the nodecg-io version that should be installed is compatible with the used NodeCG version-
51+
* @param requestedInstall the target nodecg-io install
52+
* @param nodecgDir the directory in which NodeCG is installed, used to get the NodeCG version
53+
*/
54+
export async function checkNodeCGCompatibility(requestedInstall: ProductionInstallation, nodecgVersion: SemVer) {
55+
const supportedNodeCGVersions = [1];
56+
if (requestedInstall.version !== "0.1" && requestedInstall.version !== "0.2") {
57+
// All versions above 0.2 support NodeCG v2 as well
58+
supportedNodeCGVersions.push(2);
59+
}
60+
61+
const nodeCGMajorVersion = nodecgVersion.major;
62+
if (!supportedNodeCGVersions.includes(nodeCGMajorVersion)) {
63+
throw new Error(
64+
`Your currently used NodeCG v${nodeCGMajorVersion} is not supported by nodecg-io ${requestedInstall.version}. ` +
65+
`Supported NodeCG major versions for this nodecg-io version are: ${supportedNodeCGVersions.join(", ")}`,
66+
);
67+
}
68+
}
69+
4470
/**
4571
* Finds the removed or added packages between two array of packages (current and requested install).
4672
* That way we only need to remove what is no longer requested and install what was not already present.

test/install/production.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ import { vol } from "memfs";
22
import * as path from "path";
33
import * as fs from "fs";
44
import { corePkg, dashboardPkg, nodecgIODir, twitchChatPkg, validProdInstall } from "../test.util";
5-
import { diffPackages, installPackages, removePackages, validateInstall } from "../../src/install/production";
5+
import {
6+
checkNodeCGCompatibility,
7+
diffPackages,
8+
installPackages,
9+
removePackages,
10+
validateInstall,
11+
} from "../../src/install/production";
612
import * as installation from "../../src/utils/installation";
713
import * as fsUtils from "../../src/utils/fs";
814
import * as npm from "../../src/utils/npm";
15+
import { SemVer } from "semver";
916

1017
jest.mock("fs", () => vol);
1118
beforeEach(() => vol.promises.mkdir(nodecgIODir));
@@ -20,6 +27,35 @@ const corePkg2 = {
2027
};
2128
const packages = [corePkg, twitchChatPkg];
2229

30+
describe("checkNodeCGCompatibility", () => {
31+
const testCases: [string, number, boolean][] = [
32+
["0.1", 0, false],
33+
["0.1", 1, true],
34+
["0.1", 2, false],
35+
["0.2", 1, true],
36+
["0.2", 2, false],
37+
["0.3", 1, true],
38+
["0.3", 2, true],
39+
["development", 1, true],
40+
["development", 2, true],
41+
["development", 3, false],
42+
];
43+
44+
testCases.forEach(([version, nodecgVersion, shouldWork]) => {
45+
test(`should ${shouldWork ? "allow" : "not allow"} installing ${version} with NodeCG v${nodecgVersion}`, () => {
46+
const promise = checkNodeCGCompatibility(
47+
{ ...validProdInstall, version },
48+
new SemVer(`v${nodecgVersion}.0.0`),
49+
);
50+
if (shouldWork) {
51+
return expect(promise).resolves.toBeUndefined();
52+
} else {
53+
return expect(promise).rejects.toThrowError();
54+
}
55+
});
56+
});
57+
});
58+
2359
describe("diffPackages", () => {
2460
test("should return not already installed package in pkgInstall", () => {
2561
const { pkgInstall, pkgRemove } = diffPackages([twitchChatPkg, corePkg], [twitchChatPkg]);

0 commit comments

Comments
 (0)