Flatten lib namespace

Move all functions and classes currently available through lib/name and
libName to be under a single flat namespace in lib.  This make
reorganising the lib namespace much simpler as the location of symbols
can move without breaking the consumers of lib.

The multi-entrypoint design was already problematic for the web plugin
system which couldn't effectively use it due to how webpack's shared
module system is awkward to set up with multiple entrypoints per package.
This commit is contained in:
Hornwitser committed 2023-07-28 16:03:29 +02:00
1 parent bb58670597
commit bfe119eaa3
143 files changed
+1798 -1918

No files matched your search

+21 -21
View File
@@ -4,23 +4,23 @@ const events = require("events");
const fs = require("fs-extra");
const path = require("path");
const libFactorio = require("@clusterio/lib/factorio");
const { wait } = require("@clusterio/lib/helpers");
const lib = require("@clusterio/lib");
const { wait } = lib;
const { testLines } = require("./lines");
describe("lib/factorio/server", function() {
describe("_getVersion()", function() {
it("should get the version from a changelog", async function() {
let version = await libFactorio._getVersion(path.join("test", "file", "changelog-test.txt"));
let version = await lib._getVersion(path.join("test", "file", "changelog-test.txt"));
assert.equal(version, "0.1.1");
});
it("should return null if unable to find the version", async function() {
let version = await libFactorio._getVersion(path.join("test", "file", "changelog-bad.txt"));
let version = await lib._getVersion(path.join("test", "file", "changelog-bad.txt"));
assert.equal(version, null);
});
it("should return null if file does not exist", async function() {
let version = await libFactorio._getVersion(path.join("test", "file", "does-not-exist.txt"));
let version = await lib._getVersion(path.join("test", "file", "does-not-exist.txt"));
assert.equal(version, null);
});
});
@@ -28,7 +28,7 @@ describe("lib/factorio/server", function() {
describe("_versionOrder()", function() {
it("should sort an array of versions", function() {
let versions = ["1.2.3", "0.1.4", "0.1.2", "1.2.3", "0.1.5", "1.10.2"];
versions.sort(libFactorio._versionOrder);
versions.sort(lib._versionOrder);
assert.deepEqual(
versions,
["1.10.2", "1.2.3", "1.2.3", "0.1.5", "0.1.4", "0.1.2"]
@@ -39,46 +39,46 @@ describe("lib/factorio/server", function() {
describe("_findVersion()", function() {
it("should find a given install dir with latest as target", async function() {
let installDir = path.join("test", "file", "factorio");
let [dir, version] = await libFactorio._findVersion(installDir, "latest");
let [dir, version] = await lib._findVersion(installDir, "latest");
assert.equal(dir, path.join(installDir, "data"));
assert.equal(version, "0.1.1");
});
it("should find a given install dir with correct version as target", async function() {
let installDir = path.join("test", "file", "factorio");
let [dir, version] = await libFactorio._findVersion(installDir, "0.1.1");
let [dir, version] = await lib._findVersion(installDir, "0.1.1");
assert.equal(dir, path.join(installDir, "data"));
assert.equal(version, "0.1.1");
});
it("should reject if the install dir version does not match target version", async function() {
let installDir = path.join("test", "file", "factorio");
await assert.rejects(
libFactorio._findVersion(installDir, "0.1.2"),
lib._findVersion(installDir, "0.1.2"),
new Error("Factorio version 0.1.2 was requested, but install directory contains 0.1.1")
);
});
it("should search given directory for latest Factorio install", async function() {
let installDir = path.join("test", "file");
let [dir, version] = await libFactorio._findVersion(installDir, "latest");
let [dir, version] = await lib._findVersion(installDir, "latest");
assert.equal(dir, path.join(installDir, "factorio", "data"));
assert.equal(version, "0.1.1");
});
it("should search given directory for given Factorio install", async function() {
let installDir = path.join("test", "file");
let [dir, version] = await libFactorio._findVersion(installDir, "0.1.1");
let [dir, version] = await lib._findVersion(installDir, "0.1.1");
assert.equal(dir, path.join(installDir, "factorio", "data"));
assert.equal(version, "0.1.1");
});
it("should reject if no factorio install with the given version was found", async function() {
let installDir = path.join("test", "file");
await assert.rejects(
libFactorio._findVersion(installDir, "0.1.2"),
lib._findVersion(installDir, "0.1.2"),
new Error("Unable to find Factorio version 0.1.2")
);
});
it("should reject if no factorio install was found", async function() {
let installDir = path.join("test", "file", "instances");
await assert.rejects(
libFactorio._findVersion(installDir, "latest"),
lib._findVersion(installDir, "latest"),
new Error(`Unable to find any Factorio install in ${installDir}`)
);
});
@@ -86,7 +86,7 @@ describe("lib/factorio/server", function() {
describe("randomDynamicPort()", function() {
it("should return a port number", function() {
let port = libFactorio._randomDynamicPort();
let port = lib._randomDynamicPort();
assert.equal(typeof port, "number");
assert(Number.isInteger(port));
assert(0 <= port && port < 2**16);
@@ -97,24 +97,24 @@ describe("lib/factorio/server", function() {
return (49152 <= port && port <= 65535);
}
for (let i=0; i < 20; i++) {
assert(validate(libFactorio._randomDynamicPort()));
assert(validate(lib._randomDynamicPort()));
}
});
});
describe("generatePassword()", function() {
it("should return a string", async function() {
let password = await libFactorio._generatePassword(1);
let password = await lib._generatePassword(1);
assert.equal(typeof password, "string");
});
it("should return a string of the given length", async function() {
let password = await libFactorio._generatePassword(10);
let password = await lib._generatePassword(10);
assert.equal(password.length, 10);
});
it("should contain only a-z, A-Z, 0-9", async function() {
let password = await libFactorio._generatePassword(10);
let password = await lib._generatePassword(10);
assert(/^[a-zA-Z0-9]+$/.test(password), `${password} failed test`);
});
});
@@ -123,7 +123,7 @@ describe("lib/factorio/server", function() {
it("should parse the test lines", function() {
for (let [line, reference] of testLines) {
reference.source = "test";
let output = libFactorio._parseOutput(line, "test");
let output = lib._parseOutput(line, "test");
assert.deepEqual(output, reference);
}
});
@@ -131,12 +131,12 @@ describe("lib/factorio/server", function() {
describe("class FactorioServer", function() {
let writePath = path.join("temp", "test", "server");
let server = new libFactorio.FactorioServer(path.join("test", "file", "factorio"), writePath, {});
let server = new lib.FactorioServer(path.join("test", "file", "factorio"), writePath, {});
describe("constructor()", function() {
it("should handle dashes in write path with strapPaths enabled", function() {
// eslint-disable-next-line no-new
new libFactorio.FactorioServer(
new lib.FactorioServer(
path.join("test", "file", "factorio"),
path.join("temp", "test", "server-1"),
{ stripPaths: true }