diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 00000000..444b47c8 --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +packages/master/static diff --git a/.gitignore b/.gitignore index 32870a46..57a52e6f 100644 --- a/.gitignore +++ b/.gitignore @@ -28,7 +28,7 @@ dist/ # Autogenerated docs output /docs/api -node_modules/* +node_modules Factorio* /factorio @@ -41,7 +41,7 @@ Factorio* package-lock.json mod_cache.json static/export -static/external +packages/master/static/external saves /temp/ diff --git a/.travis.yml b/.travis.yml index 01d17891..d46fbc66 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,8 @@ before_install: - tar -xf factorio.tar.gz - npm install -g codecov script: - - node build-mod --bump-patch --factorio-version 0.18 --output-dir sharedMods + - npx lerna bootstrap --no-ci + - node packages/lib/build_mod --bump-patch --factorio-version 0.18 --output-dir sharedMods - curl -o sharedMods/subspace_storage_1.99.1.zip -L https://github.com/clusterio/factorioClusterioMod/releases/download/1.99.1/subspace_storage_1.99.1.zip - npm run-script ci-cover - cat temp/test/integration/log.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index e99a2e73..aae40add 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ Version 2.0.0 - Daemonized slaves. Slaves now have the ability to run multiple Factorio instances, starting and stopping them individually. To manage this the local command line interface has been replaced with a remote interface on - the master server that can be accessed through the clusterctl cli tool. + the master server that can be accessed through the clusterioctl cli tool. - Rewritten the communication between slaves and the master from scratch. The new system is based on a WebSocket connection between the slaves and the master server and provides efficient validated bi-directonal communication. diff --git a/README.md b/README.md index f4fedff6..ae3e136e 100644 --- a/README.md +++ b/README.md @@ -105,12 +105,13 @@ supported on EOL Ubuntu releases so make sure you're on a recent release of Ubun Master and all slaves: wget -qO - https://deb.nodesource.com/setup_12.x | sudo -E bash - - sudo apt install -y nodejs python-dev git build-essential - git clone -b master https://github.com/clusterio/factorioClusterio.git - cd factorioClusterio + sudo apt install -y nodejs + mkdir clusterio + cd clusterio + npm init -y + npm install @clusterio/master @clusterio/slave @clusterio/ctl wget -O factorio.tar.gz https://www.factorio.com/get-download/latest/headless/linux64 tar -xf factorio.tar.gz - npm install --only=production downloads and installs nodejs, git and clusterio. To specify a version, change "latest" in the link to a version number like 0.14.21. @@ -136,30 +137,19 @@ The -v flag is used to specify the instance directory. Your instances (save file **Warning**: These instructions are for the unstable master version and is not recommended for use, see [the 1.2.x branch][1.2.x] for how to install the stable version. -Clusterio is built up of multiple parts. Here is a quick guide: - -Master = master.js - -Server host (Slave) = slave.js + factorio server - -Game Client = The people connecting to the server - **Requirements** download and install nodeJS 12 from http://nodejs.org. Clusterio runs on Node.js v12 and up, v11.13.0+ and v10.16.0+. -download and install git from https://git-scm.com/ - -reboot when you are done, then proceed to the next steps. *reboots matter* - **Master** 1. Open PowerShell or Command prompt in the directory you want to install to and run the following commands. - git clone -b master https://github.com/clusterio/factorioClusterio - cd factorioClusterio - npm install --only=production + mkdir clusterio + cd clusterio + npm init -y + npm install @clusterio/master @clusterio/slave @clusterio/ctl 2. Obtain Factorio by either of these two methods: @@ -198,12 +188,12 @@ Before you can manage the cluster you need to bootstrap an admin account for it. Replace `` with your Factorio username (do not make up a new username here). - node master bootstrap create-admin - node master bootstrap create-ctl-config + npx clusteriomaster bootstrap create-admin + npx clusteriomaster bootstrap create-ctl-config The first command creates a user account with the given name and promotes it to a cluster admin. The second one sets up a -`config-control.json` config for clusterctl to connect to the master +`config-control.json` config for clusterioctl to connect to the master server under the given user account. @@ -213,7 +203,7 @@ It's necessary to run the master server in order for anything to work. Once you've completed the setup run the following command to start it up: - node master run + npx clusteriomaster run ### Slaves @@ -223,7 +213,7 @@ master server. In order for slaves to connect to the master server they need a valid authentication token, you can create a slave config with a valid token with the following command. - node clusterctl slave create-config --name Local --generate-token + npx clusterioctl slave create-config --name Local --generate-token This will write a new `config-slave.json` file in the current directory (you can change the location with the `--output` option) with the name, @@ -232,30 +222,30 @@ the config for a remote slave you will need to have set the `master.external_address` option to the URL the master server can be reached on. -You can list the config of a slave on the slave itself with the `node -slave config list` command. Use `node slave config --help` for more -information. +You can list the config of a slave on the slave itself with the `npx +clusterioslave config list` command. Use `npx clusterioslave config +--help` for more information. Once the config is set up run the slave with - node slave run + npx clusterioslave run ### Instances Instances are created, managed and started from the master server. For -now the only interface available is the `clusterctl` command line tool +now the only interface available is the `clusterioctl` command line tool included in Clusterio. You can run this tool from any slave, or the master server without having to set up a config, if you want to manage the cluster from somewhere else you will need to set the `control.master_url` and `control.master_token` options with the `node -clusterctl control-config set` command: +clusterioctl control-config set` command: The basic operations to start a new instance is the following - node clusterctl instance create "My instance" - node clusterctl instance assign "My instance" "Local" - node clusterctl instance start "My instance" + npx clusterioctl instance create "My instance" + npx clusterioctl instance assign "My instance" "Local" + npx clusterioctl instance start "My instance" The first line creates the instance configuration on the master server. The second assigns the instance to a slave which creates the instance @@ -263,8 +253,8 @@ directory and files needed to run the instance on the given slave. The third line starts the instance, which creates a new save if there are no save games present. -There are many more commands available with clusterctl. See -`node clusterctl --help` for a full list of them. +There are many more commands available with clusterioctl. See +`npx clusterioctl --help` for a full list of them. ## Plugins @@ -298,7 +288,3 @@ TLDR: the tested fix is: - select TCP and select specific local ports and type in the ports that you want to open (comma separated) and click next > 3 times - give the rule a name (like 'web server' or something), give it a description (optionally) and click finish - -### Other fixes for other potential problems: - -Sometimes the install fails. Try `node ./lib/npmPostinstall` to complete it. diff --git a/docs/communication.md b/docs/communication.md index 010a2cba..bf56f4b8 100644 --- a/docs/communication.md +++ b/docs/communication.md @@ -49,9 +49,10 @@ Message There's two types of messages currently implemented apart from the Link builtin close message and that's Request and Event. The builtin -messages are defined in [lib/link/messages.js](../lib/link/messages.js) -and are attached to links via the attachAllMessages function, which is -usally called in the Link subclass constructor. +messages are defined in +[packages/lib/link/messages.js](/packages/lib/link/messages.js) and are +attached to links via the attachAllMessages function, which is usally +called in the Link subclass constructor. The messages define which links they are valid on and only attaches handlers for those links. There's also a forwarding mechanism for when diff --git a/docs/config-system.md b/docs/config-system.md index e4f82268..058dba45 100644 --- a/docs/config-system.md +++ b/docs/config-system.md @@ -6,16 +6,16 @@ configuration system in Clusterio, and unless you're developing Clusterio it's probably not going to be very useful for you. Clusterio uses a group based configuration system to manage settings for -the master server, instances, slaves, and the clusterctl utility. +the master server, instances, slaves, and the clusterioctl utility. Plugins get their own config group for the master server and instances which they can add their own fields to. The fields are defined early in the startup, and the groups and their fields for disabled plugins will still be created. The built-in config groups and their fields are defined in -[/lib/config/definitions.js](lib/config/definitions.js) while plugins -can add their own config groups to the `MasterConfigGroup` and -`InstanceConfigGroup` properties of the `info.js` export using +[packages/lib/config/definitions.js](/packages/lib/config/definitions.js) +while plugins can add their own config groups to the `MasterConfigGroup` +and `InstanceConfigGroup` properties of the `info.js` export using subclasses of `PluginConfigGroup`. The plugin group has the enabled field pre-defined on it, and it's used to enable/disable plugins. diff --git a/docs/contributing.md b/docs/contributing.md index 9370d70d..a7c909f3 100644 --- a/docs/contributing.md +++ b/docs/contributing.md @@ -11,6 +11,7 @@ Contents - [Workflow](#workflow) - [Starting a Feature Branch](#starting-a-feature-branch) + - [Setting up the Development Environment](#setting-up-the-development-environment) - [Checking your code for errors](#checking-your-code-for-errors) - [Submitting a Pull Request](#submitting-a-pull-request) - [Editing your Feature Branch](#editing-your-feature-branch) @@ -56,6 +57,19 @@ from the main repository. Do not run `git pull upstream` it will mess thing up! +### Setting up the Development Environment + +The project is organized into several packages that are all managed +through Lerna. In order to get the development environment up and +running you will need to run the following commands: + + npm install + npx lerna bootstrap + +This installs dependencies needed by tests and links the packages up so +they work from the git work tree. + + ### Starting a Feature Branch Development is done exclusively in feature branches. This means the diff --git a/docs/jsdoc.json b/docs/jsdoc.json index 19005b30..8752694b 100644 --- a/docs/jsdoc.json +++ b/docs/jsdoc.json @@ -11,12 +11,12 @@ }, "source": { "include": [ - "master.js", - "slave.js", - "clusterctl.js", - "lib/", - "plugins/", - "routes.js" + "packages/master/src/", + "packages/master/master.js", + "packages/slave/slave.js", + "packages/ctl/ctl.js", + "packages/lib/", + "plugins/" ], "excludePattern": "node_modules" } diff --git a/docs/maniging-a-cluster.md b/docs/maniging-a-cluster.md index 4db36968..d8e45881 100644 --- a/docs/maniging-a-cluster.md +++ b/docs/maniging-a-cluster.md @@ -2,17 +2,17 @@ Managing a Cluster ================== Clusterio clusers are managed through the master server by using the -`clusterctl` command line interface which is invoked by running -`node clusterctl ` in the clusterio directory. This document -uses the shorthand `ctl> foo` to indicate `node clusterctl foo` should +`clusterioctl` command line interface which is invoked by running +`npx clusterioctl ` in the clusterio directory. This document +uses the shorthand `ctl> foo` to indicate `npx clusterioctl foo` should be executed. Mandatory parameters are shown in `` and optional pameters are in `[square brackets]`. -Before `clusterctl` can be used it needs to be configured for the +Before `clusterioctl` can be used it needs to be configured for the cluster it will connect to. The easiest way to do this is to run -`node master bootstrap create-ctl-config ` on the master -server, which creates the necessary `config-control.json` for managing -the cluster as the given user. +`npx clusteriomaster bootstrap create-ctl-config ` on the +master server, which creates the necessary `config-control.json` for +managing the cluster as the given user. Slaves diff --git a/docs/socket.md b/docs/socket.md index 99bd49ef..8e28705d 100644 --- a/docs/socket.md +++ b/docs/socket.md @@ -230,8 +230,8 @@ there's [Requests](#requests). By convention message types ending in `*_event` are events. The data payload of an event consists solely of event specific properties. -See [lib/link/messages.js](/lib/link/messages.js) for the recognized -events and their contents. +See [packages/lib/link/messages.js](/packages/lib/link/messages.js) for +the recognized events and their contents. Requests @@ -245,8 +245,8 @@ replied to with a corresponding `*_response` message. If an error occured while processing the request an error response containing an error property in the data payload is sent instead. -See [lib/link/messages.js](/lib/link/messages.js) for the recognized -requests and their contents. +See [packages/lib/link/messages.js](/packages/lib/link/messages.js) for +the recognized requests and their contents. ### `*_response` diff --git a/docs/writing-plugins.md b/docs/writing-plugins.md index b1ba341d..60f6ecec 100644 --- a/docs/writing-plugins.md +++ b/docs/writing-plugins.md @@ -19,7 +19,7 @@ Contents - [Sending Link Messages](#sending-link-messages) - [Handling connection events](#handling-connection-events) - [Collecting Statistics](#collecting-statistics) -- [Adding Custom Commands to clusterctl](#adding-custom-commands-to-clusterctl) +- [Adding Custom Commands to clusterioctl](#adding-custom-commands-to-clusterioctl) Plugin Structure @@ -42,7 +42,7 @@ The basic file structure of a plugin is the following. The most important file is the `info.js` file. Without it the plugin will not recognized by Clusterio. Here's an example of it: - const link = require('lib/link'); // For messages + const link = require('@clusterio/lib/link'); // For messages module.exports = { name: "foo_frobber", @@ -100,7 +100,7 @@ The following properties are recognized: Path to a Node.js module relative to the plugin directory which contains the ControlPlugin class definition for this plugin. This is an optional paramater. A plugin can be made that only runs on - the clusterctl side. + the clusterioctl side. **messages**: Object with link messages definitions for this plugin. See guide @@ -129,7 +129,7 @@ The plugin class should derive from its respective base class defined in `lib/plugin`. For example, to define a MasterPlugin class the following code can be used: - const plugin = require('lib/plugin'); + const plugin = require('@clusterio/lib/plugin'); class MasterPlugin extends plugin.BaseMasterPlugin { async init() { @@ -145,9 +145,9 @@ code can be used: } For the instance plugin it's exactly the same except "Master" is -replaced with "Instance", and for the clusterctl plugin "Control" is +replaced with "Instance", and for the clusterioctl plugin "Control" is used. The available hooks that you can override are documented in the -base class [in lib/plugin.js](../lib/plugin.js). +base class [in lib/plugin.js](/packages/lib/plugin.js). It's best to avoid defining a constructor, but if you insist on defining one, forward all arguments to the base class. E.g.: @@ -173,7 +173,7 @@ take advantage of it by subclassing `PluginConfigGroup`, setting the and passing it as either `MasterConfigGroup` or `InstanceConfigGroup` in the `info.js` export. For example in info.js: - const config = require("lib/config"); + const config = require("@clusterio/lib/config"); class MasterConfigGroup extends config.PluginConfigGroup { } MasterConfigGroup.groupName = "foo_frobber"; @@ -550,7 +550,7 @@ from plugins optionally loaded at runtime on different computers. In its simplest form collecting data from plugins consists of defining the metric and updating it somewhere in the plugin code. For example: - const { Counter } = require('lib/prometheus'); + const { Counter } = require('@clusterio/lib/prometheus'); const fooMetric = new Counter( 'clusterio_foo_frobber_foo_metric', "Measures the level of foo", @@ -567,7 +567,7 @@ naming scheme. For metrics that are per-instance, you must define an `instance_id` label and set it accordingly, for example: - const { Counter } = require('lib/prometheus'); + const { Counter } = require('@clusterio/lib/prometheus'); const barMetric = new Gauge( 'clusterio_foo_frobber_bar_metric', "Bar instance level", @@ -591,14 +591,14 @@ hook on both master and instance plugins that is run before the metrics in the default registry are collected. -Adding Custom Commands to clusterctl ------------------------------------- +Adding Custom Commands to clusterioctl +-------------------------------------- The control entrypoint for plugins allows you to extend clustectl with your own commands. The creation of custom commands typically starts with defining a command tree for the plugin: - const { Command, CommandTree } = require("lib/command"); + const { Command, CommandTree } = require("@clusterio/lib/command"); const fooFrobberCommands = new CommandTree({ name: "foo-frobber", description: "Foo Frobber Plugin commands" }); @@ -630,18 +630,18 @@ and for setting up positional and optional arguments to commands). The `handler` is an async function that's invoked when the command is executed and it's passed the parsed command line arguments and a -reference to the `Control` class of clusterctl. It's possible to optain +reference to the `Control` class of clusterioctl. It's possible to optain a reference to the plugin class with `control.plugins.get(info.name)`. -Note that messages sent from clusterctl needs to have +Note that messages sent from clusterioctl needs to have `"control-master"` as a part of the links array for it to be accepted by the master server, see [Defining Link Messages](#defining-link-messages) for how to define the messages that can be sent to the master. -To have the command tree become part of clusterctl it needs to be added +To have the command tree become part of clusterioctl it needs to be added to the rootCommand tree in `addCommands` callback of the Control plugin: - const plugin = require("lib/plugin"); + const plugin = require("@clusterio/lib/plugin"); class ControlPlugin extends plugin.BaseControlPlugin { async addCommands(rootCommand) { diff --git a/lerna.json b/lerna.json new file mode 100644 index 00000000..8732d9e9 --- /dev/null +++ b/lerna.json @@ -0,0 +1,6 @@ +{ + "packages": [ + "packages/*" + ], + "version": "2.0.0-alpha0" +} diff --git a/lib/manager/modManager.js b/lib/manager/modManager.js deleted file mode 100644 index 19fb28ac..00000000 --- a/lib/manager/modManager.js +++ /dev/null @@ -1,299 +0,0 @@ -// This file will be rewritten from scratch -/* eslint-disable */ -/** -Provides tools to manage mods in a clusterio install -@module -*/ - -"use strict"; -const asTable = require("as-table").configure({delimiter: " | "}); -const moment = require("moment"); -const syncRequest = require("sync-request"); -const fs = require("fs-extra"); -const https = require("follow-redirects").https; -const factorioAPI = require("node-factorio-api"); -const sLog = require("single-line-log").stdout; -const query = require("cli-interact").getYesNo; -const path = require("path"); - -const fileOps = require("lib/fileOps"); - - -function ellipse(str, max){ - return str.length > (max - 3) ? str.substring(0,max-3) + "..." : str; -} - -// https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript -function formatBytes(size, decimals) { - if (bytes === 0) return "0 Bytes"; - - const k = 1024; - const dm = decimals < 0 ? 0 : decimals; - const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; - - const i = Math.floor(Math.log(bytes) / Math.log(k)); - - return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i]; -} - - -/** -Lists mods for an instance as a table. Warning, handles printing internally. -@param {Object} config - Obsolete config -@param {Instance} instance - Instance object, see slave.js - -@example -var modManager = require("modManager"); -modManager.listMods(config, instance); -// console.logs table of mods installed on instance -// if instance.name is shared: -// console.logs mods located in /sharedMods as a nice table -*/ -async function listMods(config, instance) { - return new Promise((resolve, reject) => { - if (true) { - factorioAPI.init(false, instance.path("instanceMods"), instance.path("saves"), instance.server.version); - factorioAPI.authenticate({ - username: config.username, - token: config.token, - require_ownership: false, - }).then(token => { - console.log("* Currently installed:"); - factorioAPI.readModZips().then(list => { - console.log(asTable(list.map(mod => { - return { - name:(mod.title || mod.name), - version:mod.version, - date:mod.date, - factorio_ver:mod.factorio_version, - author:mod.author, - }; - }))); - }); - // Had issues earlier due to bug in node-factorio-api, see https://github.com/Danacus/node-factorio-api/issues/2 - // bug is resolved, thanks to @Danacus and @brotherpot - factorioAPI.getModsFromSaves().then(list => { - console.log("* Mods used in save:"); - console.log(asTable(list[0].mods)); - }); - }).catch(reason => { - console.log("Failed to authenticate to factorio.com! Please check your connection and the username+token used for authentication in config.json"); - resolve(); - }); - } else { - // list shared mods in /sharedMods/ - factorioAPI.init(false, "sharedMods/", "", instance.server.version); - factorioAPI.authenticate({ - username: config.username, - token: config.token, - require_ownership: false, - }).then(() => { - console.log("\r\n"); - console.log("* Mods shared by all instances:"); - factorioAPI.getInstalledMods().then(mods => { - console.log(asTable(mods)); - resolve(); - }); - }).catch(e => { - reject(new Error(e)); - }); - } - - // get JSON data about GH releases - /*let res = syncRequest('GET', 'https://api.github.com/repos/Danielv123/factorioClusterioMod/releases', {"headers":{"User-Agent":"factorioClusterio"}}); - let url = JSON.parse(res.getBody())[0].assets[0].browser_download_url; - let name = JSON.parse(res.getBody())[0].assets[0].name; - console.log(JSON.parse(res.getBody())) - */ - }); -} - -/** -Searches mods.factorio.com for mods matching a search term and prints -the results to console as a neat looking table - -@param {Object} config - Obsolete config -@param {string} searchTerm What to search for -*/ -async function findMods(config, searchTerm) { - return new Promise((resolve, reject) => { - factorioAPI.init(false); - factorioAPI.authenticate({ - username: config.username, - token: config.token, - require_ownership: false, - }).then(token => { - factorioAPI.searchMods(searchTerm).then(mods => { - let result = asTable(mods.reverse().map(mod => { - return { - title:ellipse(mod.title, 50), - name:ellipse(mod.name, 30), - downloads:mod.downloads_count, - updated: moment(mod.updated_at).format("DD.MM.YY"), - latest_version: mod.latest_release.version, - owner:mod.owner, - //license: mod.license_name, - }; - })); - resolve(result); - // console.log(result); - }); - }).catch(e => { - if (e.contains("Insufficient information")) console.log("We are missing some parameters required for logging in! Make sure your factorio username and token are specified in config.json."); - reject(new Error(e)); - }); - }); -} - -async function addMod(config, term, instance) { - return new Promise((resolve, reject) => { - if (instance.name == "shared") { - var modDirectory = "sharedMods"; - } else { - var modDirectory = instance.path("instanceMods"); - } - if (term && term.length > 0) { - if (term.includes("mods.factorio.com/mods/")) { // https://mods.factorio.com/mods/Danielv123/ChooChoo - // treat it as a link download - factorioAPI.init(false, modDirectory, "", instance.server.version); - factorioAPI.authenticate({ - username: config.username, - token: config.token, - require_ownership: false, - }).then(() => { - factorioAPI.searchMods(term.split("/")[term.split("/").length-1]).then(body => { - console.log("Im gonna download: "+body[0].name + " in version: " + body[0].latest_release.version); - factorioAPI.downloadMod( {name: body[0].name, version: body[0].latest_release.version}).then(()=>{ - console.log("Mod downloaded"); - resolve(); - }); - }); - }); - } else if (term.includes("github.com")) { // https://github.com/Danielv123/factorioClusterioMod - // treat it as a github releases download - let apiURL = "https://api.github.com/repos/" + term.split("/")[3] + "/" + term.split("/")[4] + "/releases"; // https://api.github.com/repos/Danielv123/factorioClusterioMod/releases - - let res = syncRequest("GET", apiURL, {"headers":{"User-Agent":"factorioClusterio"}}); - let url = JSON.parse(res.getBody())[0].assets[0].browser_download_url; - let name = JSON.parse(res.getBody())[0].assets[0].name; - if (url) { - console.log("Downloading " + url); - let file = fs.createWriteStream(path.join(modDirectory, name)); - let bytesDownloaded = 0; - let downloadStartTime = Date.now(); - https.get(url, function(response) { - let bytesToDownload = response.headers["content-length"]; - response.pipe(file); - response.on("data", chunk => { - bytesDownloaded += chunk.length; - let downloadTimeElapsed = Date.now() - downloadStartTime; - let bytesPerSecond = bytesDownloaded / (downloadTimeElapsed/1000); - let timeToComplete = Math.floor((bytesToDownload - bytesDownloaded)/bytesPerSecond); - sLog("Downloading "+name+" "+(bytesDownloaded/bytesToDownload*100).toPrecision(3)+"% ("+formatBytes(bytesDownloaded)+" / "+formatBytes(bytesToDownload)+")\r\n"+ - "about "+timeToComplete+" seconds remaining ("+formatBytes(bytesPerSecond)+"/s)"); - }).on("end", x => { - sLog.clear(); - console.log(" Downloaded in "+moment.utc(moment.duration(Date.now()-downloadStartTime).as("milliseconds")).format("mm:ss")); // because sLog.clear() doesn't seem to do the job of ending the line properly - resolve(); - }); - }); - } - } else { - // treat it as a search and download the top result - factorioAPI.init(false, modDirectory, "", instance.server.version); - factorioAPI.authenticate({ - username: config.username, - token: config.token, - require_ownership: false, - }).then(() => { - factorioAPI.searchMods(term).then(body => { - console.log(body); - console.log("Im gonna download: "+body[0].name + " in version: " + body[0].latest_release.version); - factorioAPI.downloadMod( {name: body[0].name, version: body[0].latest_release.version}).then(()=>{ - console.log("Mod downloaded"); - resolve(); - }); - }); - }); - } - } else { - reject(new Error("Invalid search term")); - } - }); -} - -async function removeMod(config, term, instance, force = false) { - return new Promise((resolve, reject) => { - if (instance.name == "shared") { - var modDirectory = "sharedMods"; - } else { - var modDirectory = instance.path("instanceMods"); - } - if (term && typeof term == "string" && term.length > 0) { - let mods = fs.readdirSync(modDirectory); - let modsMatchingFilter = mods.filter(mod => mod.toLowerCase().includes(term.toLowerCase())); - if (modsMatchingFilter.length < 1) { - console.log("No mods found matching search"); - } else if (modsMatchingFilter.length == 1) { - console.log("Deleting "+modsMatchingFilter[0]); - fs.unlinkSync(path.join(modDirectory, modsMatchingFilter[0])); - console.log("Deleted."); - } else if (modsMatchingFilter.length > 1) { - console.log("Found multiple mods"); - modsMatchingFilter.forEach(mod => console.log(mod)); - if (query("Delete all mods matching filter?")) { - let modsToDelete = modsMatchingFilter.length; - for (let i = 0; i < modsToDelete; i++) { - fs.unlinkSync(path.join(modDirectory, modsMatchingFilter[i])); - console.log("Deleted "+modsMatchingFilter[i]); - } - } else console.log("Did not delete mods."); - } - resolve(); - } else { - reject(new Error("Invalid search term")); - } - }); -} - -// 'https://api.github.com/repos/Danielv123/factorioClusterioMod/releases' -/** -Download mod from github repo by looking for the first binary download in -the latest release, saving it to /sharedMods - -@param {string} url a github repo URL, for example https://api.github.com/repos/Danielv123/factorioClusterioMod/releases. Must link to /releases -*/ -function downloadFromGitURL(url) { - console.log("Downloading mod from "+url); - // get JSON data about releases - let res = syncRequest("GET", url, {"headers":{"User-Agent":"factorioClusterio"}}); - let downloadUrl = JSON.parse(res.getBody())[0].assets[0].browser_download_url; - let name = JSON.parse(res.getBody())[0].assets[0].name; - console.log(JSON.parse(res.getBody())); - if (downloadUrl) { - console.log(downloadUrl); - let file = fs.createWriteStream("sharedMods/"+name); - https.get(downloadUrl, function(response) { - response.pipe(file); - console.log("Downloaded "+name); - }); - } -} - -/* -function findUpdates() { - console.log("Downloading mods..."); - // get JSON data about releases - let res = syncRequest('GET', 'https://api.github.com/repos/Danielv123/factorioClusterioMod/releases', {"headers":{"User-Agent":"factorioClusterio"}}); - let url = JSON.parse(res.getBody())[0].assets[0].browser_download_url; - let name = JSON.parse(res.getBody())[0].assets[0].name; - console.log(JSON.parse(res.getBody())) -}*/ - -module.exports = { - listMods, - findMods, - addMod, - removeMod, - downloadFromGitURL, -}; diff --git a/lib/package.json b/lib/package.json deleted file mode 100644 index 269ff388..00000000 --- a/lib/package.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "name": "lib", - "description": "Internal library for factorioClusterio" -} - diff --git a/package.json b/package.json index cfd5f8af..5eb377f6 100644 --- a/package.json +++ b/package.json @@ -1,18 +1,14 @@ { - "name": "clusterio", - "version": "2.0.0-alpha", - "description": "Interserver communication for factorio", - "repository": "https://github.com/clusterio/factorioClusterio", - "main": "master.js", + "name": "root", + "private": true, "scripts": { "test": "mocha --exit --check-leaks -R spec --recursive test plugins/*/test", "fast-test": "FAST_TEST=y npm test", "cover": "nyc npm test", "fast-cover": "FAST_TEST=y nyc npm test", "ci-cover": "nyc --reporter=lcovonly npm run-script test", - "lint": "eslint \"*.js\" lib plugins test", + "lint": "eslint \"*.js\" packages plugins test", "docs": "jsdoc --configure docs/jsdoc.json", - "install": "node lib/npmPostinstall", "bundle-dependencies": "bundle-dependencies" }, "nyc": { @@ -21,63 +17,24 @@ "plugins/*/test/**" ] }, - "keywords": [ - "factorio" - ], - "author": "Danielv123", - "license": "MIT", "engines": { "node": ">=10.16 <11 || >= 11.13" }, - "dependencies": { - "as-table": "^1.0.31", - "base64url": "^3.0.0", - "body-parser": "^1.18.2", - "chalk": "^3.0.0", - "cli-interact": "^0.1.9", - "compression": "^1.7.2", - "cookie-parser": "^1.4.3", - "deepmerge": "^4.0.0", - "ejs": "^2.5.7", - "express": "^4.16.2", - "express-fileupload": "1.2.0", - "follow-redirects": "^1.5.0", - "fs-extra": "^8.1.0", - "ini": "^1.3.5", - "jimp": "^0.12.1", - "jsonwebtoken": "^8.2.1", - "jszip": "^3.2.2", - "lib": "file:./lib", - "luamin": "^1.0.4", - "minimist": "^1.2.0", - "moment": "^2.22.2", - "needle": "^2.4.0", - "node-factorio-api": "^0.3.8", - "node-forge": "^0.10.0", - "phin": "^3.4.1", - "pidusage": "^2.0.18", - "rcon-client": "^4.2.0", - "request": "^2.83.0", - "sanitizer": "^0.1.3", - "semver": "^7.1.1", - "set-blocking": "^2.0.0", - "simple-git": "^1.96.0", - "single-line-log": "^1.1.2", - "sync-request": "^4.0.1", - "typescript-collections": "^1.3.2", - "ws": "^7.2.3", - "yargs": "^14.2.0" - }, "devDependencies": { + "@clusterio/ctl": "file:./packages/ctl", + "@clusterio/lib": "file:./packages/lib", + "@clusterio/master": "file:packages/master", + "@clusterio/slave": "file:./packages/slave", "eslint": "^7.2.0", + "fs-extra": "^8.1.0", "html5-validator": "^1.2.1", "jsdoc": "^3.6.3", - "klaw": "^3.0.0", + "jsonwebtoken": "^8.2.1", + "jszip": "^3.2.2", + "lerna": "^3.22.1", "mocha": "^6.2.0", - "mocha-sinon": "^2.1.0", "mocha.parallel": "^0.15.5", - "nock": "^11.3.0", "nyc": "^14.1.1", - "sinon": "^7.4.1" + "phin": "^3.4.1" } } diff --git a/clusterctl.js b/packages/ctl/ctl.js old mode 100644 new mode 100755 similarity index 96% rename from clusterctl.js rename to packages/ctl/ctl.js index 310d5265..42a56469 --- a/clusterctl.js +++ b/packages/ctl/ctl.js @@ -1,21 +1,23 @@ +#!/usr/bin/env node + /** * Command line interface for controlling a Clusterio cluster - * @module + * @module ctl/ctl */ "use strict"; -const jwt = require("jsonwebtoken"); const fs = require("fs-extra"); const yargs = require("yargs"); const version = require("./package").version; const asTable = require("as-table").configure({ delimiter: " | " }); const chalk = require("chalk"); const events = require("events"); +const path = require("path"); -const link = require("lib/link"); -const errors = require("lib/errors"); -const config = require("lib/config"); -const plugin = require("lib/plugin"); -const command = require("lib/command"); +const link = require("@clusterio/lib/link"); +const errors = require("@clusterio/lib/errors"); +const config = require("@clusterio/lib/config"); +const plugin = require("@clusterio/lib/plugin"); +const command = require("@clusterio/lib/command"); /** @@ -520,7 +522,7 @@ class ControlConnector extends link.WebSocketClientConnector { console.log("SOCKET | registering control"); this.sendHandshake("register_control", { token: this._token, - agent: "clusterctl", + agent: "clusterioctl", version: version, }); } @@ -575,7 +577,7 @@ class Control extends link.Link { async function startControl() { yargs - .scriptName("clusterctl") + .scriptName("clusterioctl") .usage("$0 [options]") .option("config", { nargs: 1, @@ -589,7 +591,7 @@ async function startControl() { .strict() ; - const rootCommands = new command.CommandTree({ name: "clusterctl", description: "Manage cluster" }); + const rootCommands = new command.CommandTree({ name: "clusterioctl", description: "Manage cluster" }); rootCommands.add(slaveCommands); rootCommands.add(instanceCommands); rootCommands.add(permissionCommands); @@ -608,7 +610,9 @@ async function startControl() { continue; } - let { ControlPlugin } = require(`./plugins/${pluginInfo.name}/${pluginInfo.controlEntrypoint}`); + // XXX Does not work on Windows + let pluginPath = path.relative(__dirname, path.join("plugins", pluginInfo.name)); + let { ControlPlugin } = require(path.join(pluginPath, pluginInfo.controlEntrypoint)); let controlPlugin = new ControlPlugin(pluginInfo); controlPlugins.set(pluginInfo.name, controlPlugin); await controlPlugin.init(); @@ -737,9 +741,9 @@ I version of clusterio. Expect things to break. I ); } else { console.error(` -+-------------------------------+ -| Unable to to start clusterctl | -+-------------------------------+` ++---------------------------------+ +| Unable to to start clusterioctl | ++---------------------------------+` ); } diff --git a/packages/ctl/package.json b/packages/ctl/package.json new file mode 100644 index 00000000..a74d5fb2 --- /dev/null +++ b/packages/ctl/package.json @@ -0,0 +1,27 @@ +{ + "name": "@clusterio/ctl", + "description": "Clusterio command line management tool", + "author": "Hornwitser", + "license": "MIT", + "version": "2.0.0-alpha0", + "main": "ctl.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "bin": { + "clusterioctl": "./ctl.js" + }, + "keywords": [ + "factorio" + ], + "engines": { + "node": ">=10.16 <11 || >= 11.13" + }, + "dependencies": { + "@clusterio/lib": "=2.0.0-alpha0", + "as-table": "^1.0.31", + "chalk": "^3.0.0", + "fs-extra": "^8.1.0", + "yargs": "^14.2.0" + } +} diff --git a/build-mod.js b/packages/lib/build_mod.js similarity index 100% rename from build-mod.js rename to packages/lib/build_mod.js diff --git a/lib/command.js b/packages/lib/command.js similarity index 94% rename from lib/command.js rename to packages/lib/command.js index 691d99f9..7e06b7a8 100644 --- a/lib/command.js +++ b/packages/lib/command.js @@ -1,17 +1,17 @@ /** * Command library * - * Data types and utilites for commands to clusterctl's CLI. + * Data types and utilites for commands to clusterioctl's CLI. * * @author Hornwitser - * @module + * @module lib/command */ "use strict"; -const link = require("lib/link"); -const errors = require("lib/errors"); +const link = require("@clusterio/lib/link"); +const errors = require("@clusterio/lib/errors"); /** - * Represents a command that can be runned by clusterctl + * Represents a command that can be runned by clusterioctl * @static */ class Command { @@ -25,7 +25,7 @@ class Command { * @param {function(args, control)} handler - * Async function invoked when the command is executed. Is given * the parsed args and a reference to the {@link - * module:clusterctl.Control} instance. + * module:ctl/ctl.Control} instance. */ constructor({ definition, handler }) { if (definition[0] instanceof Array) { @@ -52,7 +52,7 @@ class Command { * A node in the command tree that can hold commands and sub trees * * Container which can contain {@link module:lib/command.Command}s and other - * CommandTrees. This is used by clusterctl to hold the full tree of + * CommandTrees. This is used by clusterioctl to hold the full tree of * available commands, you may extend this tree by using a control plugin, * see {@link module:lib/plugin.BaseControlPlugin#addCommands} * diff --git a/lib/config/classes.js b/packages/lib/config/classes.js similarity index 99% rename from lib/config/classes.js rename to packages/lib/config/classes.js index ca369270..101b8037 100644 --- a/lib/config/classes.js +++ b/packages/lib/config/classes.js @@ -4,7 +4,7 @@ const events = require("events"); const util = require("util"); -const { basicType } = require("lib/helpers"); +const { basicType } = require("@clusterio/lib/helpers"); /** diff --git a/lib/config/definitions.js b/packages/lib/config/definitions.js similarity index 100% rename from lib/config/definitions.js rename to packages/lib/config/definitions.js diff --git a/lib/config/index.js b/packages/lib/config/index.js similarity index 100% rename from lib/config/index.js rename to packages/lib/config/index.js diff --git a/lib/database.js b/packages/lib/database.js similarity index 98% rename from lib/database.js rename to packages/lib/database.js index 74b59853..7dce0451 100644 --- a/lib/database.js +++ b/packages/lib/database.js @@ -1,14 +1,14 @@ /** * Implementation of master databases * - * @module + * @module lib/database * @author Hornwitser */ "use strict"; const fs = require("fs-extra"); -const { basicType } = require("lib/helpers"); +const { basicType } = require("@clusterio/lib/helpers"); /** diff --git a/lib/errors.js b/packages/lib/errors.js similarity index 98% rename from lib/errors.js rename to packages/lib/errors.js index 7ec888ea..40de8d80 100644 --- a/lib/errors.js +++ b/packages/lib/errors.js @@ -1,6 +1,6 @@ /** * Errors thrown in Clusterio - * @module + * @module lib/errors */ "use strict"; diff --git a/lib/factorio/export.js b/packages/lib/factorio/export.js similarity index 99% rename from lib/factorio/export.js rename to packages/lib/factorio/export.js index c1ac3da5..9a19617b 100644 --- a/lib/factorio/export.js +++ b/packages/lib/factorio/export.js @@ -6,7 +6,7 @@ const ini = require("ini"); const Jimp = require("jimp"); const JSZip = require("jszip"); -const buildMod = require("../../build-mod"); +const buildMod = require("@clusterio/lib/build_mod"); /** * Generate the export mod needed for exportData diff --git a/lib/factorio/index.js b/packages/lib/factorio/index.js similarity index 58% rename from lib/factorio/index.js rename to packages/lib/factorio/index.js index f6104a77..8624b8c4 100644 --- a/lib/factorio/index.js +++ b/packages/lib/factorio/index.js @@ -5,7 +5,7 @@ */ "use strict"; module.exports = { - ...require("lib/factorio/export"), - ...require("lib/factorio/patch"), - ...require("lib/factorio/server"), + ...require("./export"), + ...require("./patch"), + ...require("./server"), }; diff --git a/lib/factorio/patch.js b/packages/lib/factorio/patch.js similarity index 99% rename from lib/factorio/patch.js rename to packages/lib/factorio/patch.js index 6742a21f..8021e58c 100644 --- a/lib/factorio/patch.js +++ b/packages/lib/factorio/patch.js @@ -7,7 +7,7 @@ const jszip = require("jszip"); const path = require("path"); const semver = require("semver"); -const hash = require("lib/hash"); +const hash = require("@clusterio/lib/hash"); const knownScenarios = { diff --git a/lib/factorio/server.js b/packages/lib/factorio/server.js similarity index 99% rename from lib/factorio/server.js rename to packages/lib/factorio/server.js index 77f0cc70..04c88e87 100644 --- a/lib/factorio/server.js +++ b/packages/lib/factorio/server.js @@ -9,7 +9,7 @@ const crypto = require("crypto"); const ini = require("ini"); const rconClient = require("rcon-client"); -const errors = require("lib/errors"); +const errors = require("@clusterio/lib/errors"); /** diff --git a/lib/fileOps/fileOps.js b/packages/lib/fileOps/fileOps.js similarity index 100% rename from lib/fileOps/fileOps.js rename to packages/lib/fileOps/fileOps.js diff --git a/lib/fileOps/index.js b/packages/lib/fileOps/index.js similarity index 100% rename from lib/fileOps/index.js rename to packages/lib/fileOps/index.js diff --git a/lib/hash.js b/packages/lib/hash.js similarity index 98% rename from lib/hash.js rename to packages/lib/hash.js index 147ebb50..5aa9d1ab 100644 --- a/lib/hash.js +++ b/packages/lib/hash.js @@ -1,6 +1,6 @@ /** * Hashing functions - * @module + * @module lib/hash */ "use strict"; const crypto = require("crypto"); diff --git a/lib/helpers.js b/packages/lib/helpers.js similarity index 95% rename from lib/helpers.js rename to packages/lib/helpers.js index 8fa63587..a31322ba 100644 --- a/lib/helpers.js +++ b/packages/lib/helpers.js @@ -1,6 +1,6 @@ /** * Collection of small utilites that are useful in multiple places. - * @module + * @module lib/helpers */ "use strict"; diff --git a/lib/link/connectors.js b/packages/lib/link/connectors.js similarity index 99% rename from lib/link/connectors.js rename to packages/lib/link/connectors.js index a7997f0a..b18f9844 100644 --- a/lib/link/connectors.js +++ b/packages/lib/link/connectors.js @@ -3,8 +3,8 @@ const events = require("events"); const WebSocket = require("ws"); -const schema = require("lib/schema"); -const errors = require("lib/errors"); +const schema = require("@clusterio/lib/schema"); +const errors = require("@clusterio/lib/errors"); /** diff --git a/lib/link/index.js b/packages/lib/link/index.js similarity index 58% rename from lib/link/index.js rename to packages/lib/link/index.js index 2690e686..28f7aa3a 100644 --- a/lib/link/index.js +++ b/packages/lib/link/index.js @@ -5,7 +5,7 @@ */ "use strict"; module.exports = { - ...require("lib/link/link"), - ...require("lib/link/messages"), - ...require("lib/link/connectors"), + ...require("./link"), + ...require("./messages"), + ...require("./connectors"), }; diff --git a/lib/link/link.js b/packages/lib/link/link.js similarity index 98% rename from lib/link/link.js rename to packages/lib/link/link.js index d05cfb53..1a9d0822 100644 --- a/lib/link/link.js +++ b/packages/lib/link/link.js @@ -1,9 +1,9 @@ // Implementation of Link class "use strict"; -const schema = require("lib/schema"); -const errors = require("lib/errors"); -const messages = require("lib/link/messages"); +const schema = require("@clusterio/lib/schema"); +const errors = require("@clusterio/lib/errors"); +const messages = require("./messages"); // Some definitions for the terminology used here: // link: Either side of a master - client connection diff --git a/lib/link/messages.js b/packages/lib/link/messages.js similarity index 99% rename from lib/link/messages.js rename to packages/lib/link/messages.js index 648aef20..fe16e7e4 100644 --- a/lib/link/messages.js +++ b/packages/lib/link/messages.js @@ -1,7 +1,7 @@ // Message definitions for links "use strict"; -const schema = require("lib/schema"); -const errors = require("lib/errors"); +const schema = require("@clusterio/lib/schema"); +const errors = require("@clusterio/lib/errors"); /** * Represents a message that can be sent over the link diff --git a/lib/luaTools.js b/packages/lib/luaTools.js similarity index 94% rename from lib/luaTools.js rename to packages/lib/luaTools.js index 48ad79dd..ac086b65 100644 --- a/lib/luaTools.js +++ b/packages/lib/luaTools.js @@ -1,6 +1,6 @@ /** * Utilities for dealing with Lua - * @module + * @module lib/luaTools */ "use strict"; diff --git a/lib/objectOps.js b/packages/lib/objectOps.js similarity index 97% rename from lib/objectOps.js rename to packages/lib/objectOps.js index 1fe093cc..47b9a4fa 100644 --- a/lib/objectOps.js +++ b/packages/lib/objectOps.js @@ -1,6 +1,6 @@ /** * Helper functions related to objects - * @module + * @module lib/objectOps */ "use strict"; diff --git a/packages/lib/package.json b/packages/lib/package.json new file mode 100644 index 00000000..6936c27c --- /dev/null +++ b/packages/lib/package.json @@ -0,0 +1,26 @@ +{ + "name": "@clusterio/lib", + "description": "Shared library for Clusterio", + "version": "2.0.0-alpha0", + "repository": "https://github.com/clusterio/factorioClusterio", + "license": "MIT", + "keywords": [ + "factorio" + ], + "engines": { + "node": ">=10.16 <11 || >= 11.13" + }, + "dependencies": { + "fs-extra": "^8.1.0", + "ini": "^1.3.5", + "jimp": "^0.12.1", + "jsonwebtoken": "^8.2.1", + "jszip": "^3.2.2", + "klaw": "^3.0.0", + "node-forge": "^0.10.0", + "rcon-client": "^4.2.0", + "semver": "^7.1.1", + "ws": "^7.2.3", + "yargs": "^14.2.0" + } +} diff --git a/lib/plugin.js b/packages/lib/plugin.js similarity index 94% rename from lib/plugin.js rename to packages/lib/plugin.js index 84e4cbd5..58f7a657 100644 --- a/lib/plugin.js +++ b/packages/lib/plugin.js @@ -1,12 +1,12 @@ /** * Plugin interfaces and utilities. - * @module + * @module lib/plugin */ "use strict"; const fs = require("fs-extra"); const path = require("path"); -const errors = require("lib/errors"); +const errors = require("@clusterio/lib/errors"); /** @@ -42,7 +42,7 @@ class BaseInstancePlugin { /** * Instance the plugin started for - * @type {module:slave~Instance} + * @type {module:slave/slave~Instance} */ this.instance = instance; @@ -52,7 +52,7 @@ class BaseInstancePlugin { * With the exepction of accessing the slave's config you should * avoid ineracting with the slave object directly. * - * @type {module:slave~Slave} + * @type {module:slave/slave~Slave} */ this.slave = slave; } @@ -181,7 +181,7 @@ class BaseInstancePlugin { * Plugins must stop sending messages to the master, or are forwarded * via the master server after the prepare disconnect has been handled. * - * @param {module:master~SlaveConnection} connection - + * @param {module:master/master~SlaveConnection} connection - * The connection to the slave preparing to disconnect. */ async onPrepareMasterDisconnect(connection) { } @@ -283,7 +283,7 @@ class BaseMasterPlugin { * * Invoked when the connection to the slave has been closed. * - * @param {module:master~SlaveConnection} connection - + * @param {module:master/master~SlaveConnection} connection - * The connection the event occured on. * @param {string} event - one of connect, drop, and close */ @@ -298,7 +298,7 @@ class BaseMasterPlugin { * Plugins must stop sending messages to the slave in question after the * prepare disconnect has been handled. * - * @param {module:master~SlaveConnection} connection - + * @param {module:master/master~SlaveConnection} connection - * The connection to the slave preparing to disconnect. */ async onPrepareSlaveDisconnect(connection) { } @@ -339,10 +339,10 @@ class BaseMasterPlugin { } /** - * Base class for clusterctl plugins + * Base class for clusterioctl plugins * * Control plugins are subclasses of this class which get instantiated by - * clusterctl in order to extend its functionallity. To be discovered the + * clusterioctl in order to extend its functionallity. To be discovered the * class must be exported under the name `ControlPlugin` in the module * specified by the `controlEntrypoint` in the plugin's info.js file. * @@ -364,12 +364,12 @@ class BaseControlPlugin { /** * Called to add commands to the command line interface. * - * Invoked by clusterctl to let plugins add commands. `rootCommand` is + * Invoked by clusterioctl to let plugins add commands. `rootCommand` is * the top level command node which the plugin should add its own {@link * module:lib/command.CommandTree} to. * * @param {module:lib/command.CommandTree} rootCommand - - * Root of the clusterctl command tree. + * Root of the clusterioctl command tree. */ async addCommands(rootCommand) { } } @@ -392,7 +392,9 @@ async function loadPluginInfos(baseDir) { try { // Note: Require path is relative to this module - pluginInfo = require(path.posix.join("..", baseDir, pluginDir, "info")); + // XXX Does not work on Windows + let pluginPath = path.relative(__dirname, path.join(baseDir, pluginDir)); + pluginInfo = require(path.join(pluginPath, "info")); } catch (err) { if (err.code === "MODULE_NOT_FOUND") { diff --git a/lib/prometheus.js b/packages/lib/prometheus.js similarity index 100% rename from lib/prometheus.js rename to packages/lib/prometheus.js diff --git a/lib/schema.js b/packages/lib/schema.js similarity index 99% rename from lib/schema.js rename to packages/lib/schema.js index 66f038bd..4e781812 100644 --- a/lib/schema.js +++ b/packages/lib/schema.js @@ -1,6 +1,6 @@ /** * JSON schemas used for validating link messages - * @module + * @module lib/schema */ "use strict"; const ajv = new (require("ajv"))({ diff --git a/lib/users.js b/packages/lib/users.js similarity index 99% rename from lib/users.js rename to packages/lib/users.js index aa2803fb..7f69c390 100644 --- a/lib/users.js +++ b/packages/lib/users.js @@ -4,7 +4,7 @@ * Defines data types for users, roles and permissions. * * @author Hornwitser - * @module + * @module lib/users */ "use strict"; const jwt = require("jsonwebtoken"); diff --git a/master.js b/packages/master/master.js old mode 100644 new mode 100755 similarity index 98% rename from master.js rename to packages/master/master.js index b5f8d427..26628e5e --- a/master.js +++ b/packages/master/master.js @@ -1,15 +1,17 @@ +#!/usr/bin/env node + /** * Clusterio master server * * Facilitates communication between slaves and control of the cluster * through WebSocet connections, and hosts a webserver for browser * interfaces and Prometheus statistics export. It is remotely controlled - * by {@link module:clusterctl}. + * by {@link module:ctl/ctl}. * - * @module + * @module master/master * @author Danielv123, Hornwitser * @example - * node master run + * npx clusteriomaster run */ // Attempt updating @@ -17,7 +19,6 @@ // updater.update().then(console.log); "use strict"; -const deepmerge = require("deepmerge"); const path = require("path"); const fs = require("fs-extra"); const jwt = require("jsonwebtoken"); @@ -39,15 +40,15 @@ let debugEvents = new events.EventEmitter(); let pluginList = {}; // homebrew modules -const generateSSLcert = require("lib/generateSSLcert"); -const database = require("lib/database"); -const schema = require("lib/schema"); -const link = require("lib/link"); -const errors = require("lib/errors"); -const plugin = require("lib/plugin"); -const prometheus = require("lib/prometheus"); -const config = require("lib/config"); -const users = require("lib/users"); +const generateSSLcert = require("./src/generate_ssl_cert"); +const database = require("@clusterio/lib/database"); +const schema = require("@clusterio/lib/schema"); +const link = require("@clusterio/lib/link"); +const errors = require("@clusterio/lib/errors"); +const plugin = require("@clusterio/lib/plugin"); +const prometheus = require("@clusterio/lib/prometheus"); +const config = require("@clusterio/lib/config"); +const users = require("@clusterio/lib/users"); const express = require("express"); const compression = require("compression"); @@ -87,9 +88,10 @@ app.use(function(req, res, next){ next(); }); -require("./routes")(app); +require("./src/routes")(app); // Set folder to serve static content from (the website) -app.use(express.static("static")); +app.use(express.static(path.join(__dirname, "static"))); +app.use(express.static("static")); // Used for data export files const endpointHitCounter = new prometheus.Counter( "clusterio_master_http_endpoint_hits_total", @@ -1482,7 +1484,9 @@ async function loadPlugins(pluginInfos) { let MasterPlugin = plugin.BaseMasterPlugin; try { if (pluginInfo.masterEntrypoint) { - ({ MasterPlugin } = require(`./plugins/${pluginInfo.name}/${pluginInfo.masterEntrypoint}`)); + // XXX Does not work on Windows + let pluginPath = path.relative(__dirname, path.join("plugins", pluginInfo.name)); + ({ MasterPlugin } = require(path.join(pluginPath, pluginInfo.masterEntrypoint))); } let masterPlugin = new MasterPlugin( @@ -1578,7 +1582,7 @@ async function startServer() { .command("bootstrap", "Bootstrap access to cluster", yargs => { yargs .command("create-admin ", "Create a cluster admin") - .command("create-ctl-config ", "Create clusterctl config for the given user", yargs => { + .command("create-ctl-config ", "Create clusterioctl config for the given user", yargs => { yargs.option("output", { describe: "Path to output config (- for stdout)", type: "string", nargs: 1, default: "config-control.json", diff --git a/lib/npmPostinstall.js b/packages/master/npm_post_install.js similarity index 100% rename from lib/npmPostinstall.js rename to packages/master/npm_post_install.js diff --git a/packages/master/package.json b/packages/master/package.json new file mode 100644 index 00000000..a2062d7f --- /dev/null +++ b/packages/master/package.json @@ -0,0 +1,40 @@ +{ + "name": "@clusterio/master", + "description": "Implementation of Clusterio master server", + "author": "Hornwitser", + "license": "MIT", + "version": "2.0.0-alpha0", + "main": "master.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "install": "node npm_post_install" + }, + "bin": { + "clusteriomaster": "./master.js" + }, + "keywords": [ + "factorio" + ], + "engines": { + "node": ">=10.16 <11 || >= 11.13" + }, + "dependencies": { + "@clusterio/lib": "=2.0.0-alpha0", + "body-parser": "^1.18.2", + "compression": "^1.7.2", + "cookie-parser": "^1.4.3", + "ejs": "^2.5.7", + "express": "^4.16.2", + "express-fileupload": "1.2.0", + "fs-extra": "^8.1.0", + "jsonwebtoken": "^8.2.1", + "jszip": "^3.2.2", + "moment": "^2.22.2", + "node-factorio-api": "^0.3.8", + "node-forge": "^0.10.0", + "request": "^2.83.0", + "set-blocking": "^2.0.0", + "ws": "^7.2.3", + "yargs": "^14.2.0" + } +} diff --git a/lib/generateSSLcert.js b/packages/master/src/generate_ssl_cert.js similarity index 95% rename from lib/generateSSLcert.js rename to packages/master/src/generate_ssl_cert.js index fee1b50b..0e966b8b 100644 --- a/lib/generateSSLcert.js +++ b/packages/master/src/generate_ssl_cert.js @@ -13,7 +13,7 @@ module.exports = async function({ doLogging = false, } = {}){ function log(t) { - if(doLogging) console.log("lib/generateSSLcert.js | "+t); + if(doLogging) console.log("src/generate_ssl_cert.js | "+t); } log("Generating SSL certificate with "+bits+" bits at "+sslCertPath); diff --git a/routes.js b/packages/master/src/routes.js similarity index 100% rename from routes.js rename to packages/master/src/routes.js diff --git a/static/bootstrap.min.css b/packages/master/static/bootstrap.min.css similarity index 100% rename from static/bootstrap.min.css rename to packages/master/static/bootstrap.min.css diff --git a/static/browserconfig.xml b/packages/master/static/browserconfig.xml similarity index 100% rename from static/browserconfig.xml rename to packages/master/static/browserconfig.xml diff --git a/static/favicon.ico b/packages/master/static/favicon.ico similarity index 100% rename from static/favicon.ico rename to packages/master/static/favicon.ico diff --git a/static/favicon/android-icon-144x144.png b/packages/master/static/favicon/android-icon-144x144.png similarity index 100% rename from static/favicon/android-icon-144x144.png rename to packages/master/static/favicon/android-icon-144x144.png diff --git a/static/favicon/android-icon-192x192.png b/packages/master/static/favicon/android-icon-192x192.png similarity index 100% rename from static/favicon/android-icon-192x192.png rename to packages/master/static/favicon/android-icon-192x192.png diff --git a/static/favicon/android-icon-36x36.png b/packages/master/static/favicon/android-icon-36x36.png similarity index 100% rename from static/favicon/android-icon-36x36.png rename to packages/master/static/favicon/android-icon-36x36.png diff --git a/static/favicon/android-icon-48x48.png b/packages/master/static/favicon/android-icon-48x48.png similarity index 100% rename from static/favicon/android-icon-48x48.png rename to packages/master/static/favicon/android-icon-48x48.png diff --git a/static/favicon/android-icon-72x72.png b/packages/master/static/favicon/android-icon-72x72.png similarity index 100% rename from static/favicon/android-icon-72x72.png rename to packages/master/static/favicon/android-icon-72x72.png diff --git a/static/favicon/android-icon-96x96.png b/packages/master/static/favicon/android-icon-96x96.png similarity index 100% rename from static/favicon/android-icon-96x96.png rename to packages/master/static/favicon/android-icon-96x96.png diff --git a/static/favicon/apple-icon-114x114.png b/packages/master/static/favicon/apple-icon-114x114.png similarity index 100% rename from static/favicon/apple-icon-114x114.png rename to packages/master/static/favicon/apple-icon-114x114.png diff --git a/static/favicon/apple-icon-120x120.png b/packages/master/static/favicon/apple-icon-120x120.png similarity index 100% rename from static/favicon/apple-icon-120x120.png rename to packages/master/static/favicon/apple-icon-120x120.png diff --git a/static/favicon/apple-icon-144x144.png b/packages/master/static/favicon/apple-icon-144x144.png similarity index 100% rename from static/favicon/apple-icon-144x144.png rename to packages/master/static/favicon/apple-icon-144x144.png diff --git a/static/favicon/apple-icon-152x152.png b/packages/master/static/favicon/apple-icon-152x152.png similarity index 100% rename from static/favicon/apple-icon-152x152.png rename to packages/master/static/favicon/apple-icon-152x152.png diff --git a/static/favicon/apple-icon-180x180.png b/packages/master/static/favicon/apple-icon-180x180.png similarity index 100% rename from static/favicon/apple-icon-180x180.png rename to packages/master/static/favicon/apple-icon-180x180.png diff --git a/static/favicon/apple-icon-57x57.png b/packages/master/static/favicon/apple-icon-57x57.png similarity index 100% rename from static/favicon/apple-icon-57x57.png rename to packages/master/static/favicon/apple-icon-57x57.png diff --git a/static/favicon/apple-icon-60x60.png b/packages/master/static/favicon/apple-icon-60x60.png similarity index 100% rename from static/favicon/apple-icon-60x60.png rename to packages/master/static/favicon/apple-icon-60x60.png diff --git a/static/favicon/apple-icon-72x72.png b/packages/master/static/favicon/apple-icon-72x72.png similarity index 100% rename from static/favicon/apple-icon-72x72.png rename to packages/master/static/favicon/apple-icon-72x72.png diff --git a/static/favicon/apple-icon-76x76.png b/packages/master/static/favicon/apple-icon-76x76.png similarity index 100% rename from static/favicon/apple-icon-76x76.png rename to packages/master/static/favicon/apple-icon-76x76.png diff --git a/static/favicon/apple-icon-precomposed.png b/packages/master/static/favicon/apple-icon-precomposed.png similarity index 100% rename from static/favicon/apple-icon-precomposed.png rename to packages/master/static/favicon/apple-icon-precomposed.png diff --git a/static/favicon/apple-icon.png b/packages/master/static/favicon/apple-icon.png similarity index 100% rename from static/favicon/apple-icon.png rename to packages/master/static/favicon/apple-icon.png diff --git a/static/favicon/browserconfig.xml b/packages/master/static/favicon/browserconfig.xml similarity index 100% rename from static/favicon/browserconfig.xml rename to packages/master/static/favicon/browserconfig.xml diff --git a/static/favicon/favicon-16x16.png b/packages/master/static/favicon/favicon-16x16.png similarity index 100% rename from static/favicon/favicon-16x16.png rename to packages/master/static/favicon/favicon-16x16.png diff --git a/static/favicon/favicon-32x32.png b/packages/master/static/favicon/favicon-32x32.png similarity index 100% rename from static/favicon/favicon-32x32.png rename to packages/master/static/favicon/favicon-32x32.png diff --git a/static/favicon/favicon-96x96.png b/packages/master/static/favicon/favicon-96x96.png similarity index 100% rename from static/favicon/favicon-96x96.png rename to packages/master/static/favicon/favicon-96x96.png diff --git a/static/favicon/favicon.ico b/packages/master/static/favicon/favicon.ico similarity index 100% rename from static/favicon/favicon.ico rename to packages/master/static/favicon/favicon.ico diff --git a/static/favicon/manifest.json b/packages/master/static/favicon/manifest.json similarity index 100% rename from static/favicon/manifest.json rename to packages/master/static/favicon/manifest.json diff --git a/static/favicon/ms-icon-144x144.png b/packages/master/static/favicon/ms-icon-144x144.png similarity index 100% rename from static/favicon/ms-icon-144x144.png rename to packages/master/static/favicon/ms-icon-144x144.png diff --git a/static/favicon/ms-icon-150x150.png b/packages/master/static/favicon/ms-icon-150x150.png similarity index 100% rename from static/favicon/ms-icon-150x150.png rename to packages/master/static/favicon/ms-icon-150x150.png diff --git a/static/favicon/ms-icon-310x310.png b/packages/master/static/favicon/ms-icon-310x310.png similarity index 100% rename from static/favicon/ms-icon-310x310.png rename to packages/master/static/favicon/ms-icon-310x310.png diff --git a/static/favicon/ms-icon-70x70.png b/packages/master/static/favicon/ms-icon-70x70.png similarity index 100% rename from static/favicon/ms-icon-70x70.png rename to packages/master/static/favicon/ms-icon-70x70.png diff --git a/static/lib/terminal.js b/packages/master/static/lib/terminal.js similarity index 100% rename from static/lib/terminal.js rename to packages/master/static/lib/terminal.js diff --git a/static/lib/utility.js b/packages/master/static/lib/utility.js similarity index 100% rename from static/lib/utility.js rename to packages/master/static/lib/utility.js diff --git a/static/main.js b/packages/master/static/main.js similarity index 100% rename from static/main.js rename to packages/master/static/main.js diff --git a/static/manifest.json b/packages/master/static/manifest.json similarity index 100% rename from static/manifest.json rename to packages/master/static/manifest.json diff --git a/static/nodeDetails.css b/packages/master/static/nodeDetails.css similarity index 100% rename from static/nodeDetails.css rename to packages/master/static/nodeDetails.css diff --git a/static/nodeDetails.js b/packages/master/static/nodeDetails.js similarity index 100% rename from static/nodeDetails.js rename to packages/master/static/nodeDetails.js diff --git a/static/pictures/clusterio_icon.png b/packages/master/static/pictures/clusterio_icon.png similarity index 100% rename from static/pictures/clusterio_icon.png rename to packages/master/static/pictures/clusterio_icon.png diff --git a/static/pictures/logo.png b/packages/master/static/pictures/logo.png similarity index 100% rename from static/pictures/logo.png rename to packages/master/static/pictures/logo.png diff --git a/static/pictures/unknown-item.png b/packages/master/static/pictures/unknown-item.png similarity index 100% rename from static/pictures/unknown-item.png rename to packages/master/static/pictures/unknown-item.png diff --git a/static/settings.js b/packages/master/static/settings.js similarity index 100% rename from static/settings.js rename to packages/master/static/settings.js diff --git a/static/style.css b/packages/master/static/style.css similarity index 100% rename from static/style.css rename to packages/master/static/style.css diff --git a/packages/slave/package.json b/packages/slave/package.json new file mode 100644 index 00000000..c2fba4a6 --- /dev/null +++ b/packages/slave/package.json @@ -0,0 +1,28 @@ +{ + "name": "@clusterio/slave", + "description": "Implementation of Clusterio slave server", + "author": "Hornwitser", + "license": "MIT", + "version": "2.0.0-alpha0", + "main": "slave.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "bin": { + "clusterioslave": "./slave.js" + }, + "keywords": [ + "factorio" + ], + "engines": { + "node": ">=10.16 <11 || >= 11.13" + }, + "dependencies": { + "@clusterio/lib": "=2.0.0-alpha0", + "fs-extra": "^8.1.0", + "phin": "^3.4.1", + "pidusage": "^2.0.18", + "set-blocking": "^2.0.0", + "yargs": "^14.2.0" + } +} diff --git a/slave.js b/packages/slave/slave.js old mode 100644 new mode 100755 similarity index 97% rename from slave.js rename to packages/slave/slave.js index 7852aa65..bb5d609c --- a/slave.js +++ b/packages/slave/slave.js @@ -1,14 +1,16 @@ +#!/usr/bin/env node + /** * Clusterio slave * * Connects to the master server and hosts Factorio servers that can * communicate with the cluster. It is remotely controlled by {@link - * module:master}. + * module:master/master}. * - * @module + * @module slave/slave * @author Danielv123, Hornwitser * @example - * node slave run + * npx clusterioslave run */ "use strict"; const fs = require("fs-extra"); @@ -22,14 +24,14 @@ const util = require("util"); const version = require("./package").version; // internal libraries -const fileOps = require("lib/fileOps"); -const factorio = require("lib/factorio"); -const link = require("lib/link"); -const plugin = require("lib/plugin"); -const errors = require("lib/errors"); -const prometheus = require("lib/prometheus"); -const luaTools = require("lib/luaTools"); -const config = require("lib/config"); +const fileOps = require("@clusterio/lib/fileOps"); +const factorio = require("@clusterio/lib/factorio"); +const link = require("@clusterio/lib/link"); +const plugin = require("@clusterio/lib/plugin"); +const errors = require("@clusterio/lib/errors"); +const prometheus = require("@clusterio/lib/prometheus"); +const luaTools = require("@clusterio/lib/luaTools"); +const config = require("@clusterio/lib/config"); const instanceRconCommandsCounter = new prometheus.Counter( @@ -198,7 +200,9 @@ class Instance extends link.Link{ async _loadPlugin(pluginInfo, slave) { let pluginLoadStarted = Date.now(); - let { InstancePlugin } = require(`./plugins/${pluginInfo.name}/${pluginInfo.instanceEntrypoint}`); + // XXX Does not work on Windows + let pluginPath = path.relative(__dirname, path.join("plugins", pluginInfo.name)); + let { InstancePlugin } = require(path.join(pluginPath, pluginInfo.instanceEntrypoint)); let instancePlugin = new InstancePlugin(pluginInfo, this, slave); this.plugins.set(pluginInfo.name, instancePlugin); await instancePlugin.init(); @@ -1110,7 +1114,7 @@ class Slave extends link.Link { * Initialize and connect an unloaded instance * * @param {number} instanceId - ID of instance to initialize. - * @returns {module:slave~InstanceConnection} connection to instance. + * @returns {module:slave/slave~InstanceConnection} connection to instance. */ async _connectInstance(instanceId) { let instanceInfo = this.instanceInfos.get(instanceId); @@ -1423,9 +1427,9 @@ async function startSlave() { console.error("ERROR invalid config!"); console.error( "Master server requires an access token for socket operations. As clusterio\n"+ - "slaves depends upon this, please set your token using the command node slave\n"+ - "config set slave.master_token . You can generate an auth token using\n"+ - "using node clusterctl generate-slave-token." + "slaves depends upon this, please set your token using the command npx\n"+ + "clusterioslave config set slave.master_token . You can generate an\n"+ + "auth token using npx clusterioctl generate-slave-token." ); process.exitCode = 1; return; diff --git a/plugins/global_chat/control.js b/plugins/global_chat/control.js index acba4340..4f810d7a 100644 --- a/plugins/global_chat/control.js +++ b/plugins/global_chat/control.js @@ -1,7 +1,7 @@ "use strict"; -const plugin = require("lib/plugin"); +const plugin = require("@clusterio/lib/plugin"); const info = require("./info"); -const command = require("lib/command"); +const command = require("@clusterio/lib/command"); const globalChatCommands = new command.CommandTree({ name: "global-chat", description: "Global Chat plugin commands" }); diff --git a/plugins/global_chat/info.js b/plugins/global_chat/info.js index 7231ce8d..bb4dece1 100644 --- a/plugins/global_chat/info.js +++ b/plugins/global_chat/info.js @@ -1,6 +1,6 @@ "use strict"; -let link = require("lib/link"); -let plugin = require("lib/plugin"); +let link = require("@clusterio/lib/link"); +let plugin = require("@clusterio/lib/plugin"); module.exports = { name: "global_chat", diff --git a/plugins/global_chat/instance.js b/plugins/global_chat/instance.js index 5cb2dadb..7cbea0dd 100644 --- a/plugins/global_chat/instance.js +++ b/plugins/global_chat/instance.js @@ -2,8 +2,8 @@ * @module */ "use strict"; -const plugin = require("lib/plugin"); -const luaTools = require("lib/luaTools"); +const plugin = require("@clusterio/lib/plugin"); +const luaTools = require("@clusterio/lib/luaTools"); /** diff --git a/plugins/research_sync/info.js b/plugins/research_sync/info.js index c9d85316..e28ef732 100644 --- a/plugins/research_sync/info.js +++ b/plugins/research_sync/info.js @@ -1,5 +1,5 @@ "use strict"; -const link = require("lib/link"); +const link = require("@clusterio/lib/link"); // schema used for syncing technologies to and from the master diff --git a/plugins/research_sync/instance.js b/plugins/research_sync/instance.js index 4a9f56c0..e924a526 100644 --- a/plugins/research_sync/instance.js +++ b/plugins/research_sync/instance.js @@ -1,8 +1,6 @@ "use strict"; -const needle = require("needle"); - -const plugin = require("lib/plugin"); -const luaTools = require("lib/luaTools"); +const plugin = require("@clusterio/lib/plugin"); +const luaTools = require("@clusterio/lib/luaTools"); function unexpectedError(err) { diff --git a/plugins/research_sync/master.js b/plugins/research_sync/master.js index a6f0cbd4..14d4104d 100644 --- a/plugins/research_sync/master.js +++ b/plugins/research_sync/master.js @@ -2,7 +2,7 @@ const fs = require("fs-extra"); const path = require("path"); -const plugin = require("lib/plugin"); +const plugin = require("@clusterio/lib/plugin"); class MasterPlugin extends plugin.BaseMasterPlugin { diff --git a/plugins/statistics_exporter/info.js b/plugins/statistics_exporter/info.js index ca83b9b5..a0dabdd4 100644 --- a/plugins/statistics_exporter/info.js +++ b/plugins/statistics_exporter/info.js @@ -1,5 +1,5 @@ "use strict"; -let link = require("lib/link"); +let link = require("@clusterio/lib/link"); module.exports = { name: "statistics_exporter", diff --git a/plugins/statistics_exporter/instance.js b/plugins/statistics_exporter/instance.js index 2e179d1f..e5793b24 100644 --- a/plugins/statistics_exporter/instance.js +++ b/plugins/statistics_exporter/instance.js @@ -1,6 +1,6 @@ "use strict"; -const plugin = require("lib/plugin"); -const { Gauge } = require("lib/prometheus"); +const plugin = require("@clusterio/lib/plugin"); +const { Gauge } = require("@clusterio/lib/prometheus"); const instancePlayerCount = new Gauge( diff --git a/plugins/subspace_storage/dole.js b/plugins/subspace_storage/dole.js index fe06bb3a..3f49a27a 100644 --- a/plugins/subspace_storage/dole.js +++ b/plugins/subspace_storage/dole.js @@ -1,7 +1,7 @@ "use strict"; const doleNN = require("./dole_nn_base"); -const prometheus = require("lib/prometheus"); +const prometheus = require("@clusterio/lib/prometheus"); const prometheusNNDoleGauge = new prometheus.Gauge( diff --git a/plugins/subspace_storage/info.js b/plugins/subspace_storage/info.js index 028162e8..cc0e9831 100644 --- a/plugins/subspace_storage/info.js +++ b/plugins/subspace_storage/info.js @@ -1,6 +1,6 @@ "use strict"; -const link = require("lib/link"); -const config = require("lib/config"); +const link = require("@clusterio/lib/link"); +const config = require("@clusterio/lib/config"); class MasterConfigGroup extends config.PluginConfigGroup {} MasterConfigGroup.groupName = "subspace_storage"; diff --git a/plugins/subspace_storage/instance.js b/plugins/subspace_storage/instance.js index 53ca3571..da4ebea4 100644 --- a/plugins/subspace_storage/instance.js +++ b/plugins/subspace_storage/instance.js @@ -1,8 +1,8 @@ "use strict"; const fs = require("fs-extra"); -const plugin = require("lib/plugin"); -const luaTools = require("lib/luaTools"); +const plugin = require("@clusterio/lib/plugin"); +const luaTools = require("@clusterio/lib/luaTools"); function unexpectedError(err) { console.log("Unexpected error in subspace_storage"); diff --git a/plugins/subspace_storage/master.js b/plugins/subspace_storage/master.js index 9a6bdab5..00d1d290 100644 --- a/plugins/subspace_storage/master.js +++ b/plugins/subspace_storage/master.js @@ -2,9 +2,9 @@ const fs = require("fs-extra"); const path = require("path"); -const database = require("lib/database"); -const plugin = require("lib/plugin"); -const prometheus = require("lib/prometheus"); +const database = require("@clusterio/lib/database"); +const plugin = require("@clusterio/lib/plugin"); +const prometheus = require("@clusterio/lib/prometheus"); const routes = require("./routes"); const dole = require("./dole"); diff --git a/plugins/subspace_storage/routes.js b/plugins/subspace_storage/routes.js index cffa5c54..32d49933 100644 --- a/plugins/subspace_storage/routes.js +++ b/plugins/subspace_storage/routes.js @@ -2,7 +2,7 @@ const express = require("express"); const path = require("path"); -const prometheus = require("lib/prometheus"); +const prometheus = require("@clusterio/lib/prometheus"); function addWebRoutes(app) { diff --git a/sharedPlugins/inventoryImports/interfacingFunctions.js b/sharedPlugins/inventoryImports/interfacingFunctions.js index a7d78b87..cca4d6c7 100644 --- a/sharedPlugins/inventoryImports/interfacingFunctions.js +++ b/sharedPlugins/inventoryImports/interfacingFunctions.js @@ -1,4 +1,4 @@ -const objectOps = require("lib/objectOps"); +const objectOps = require("@clusterio/lib/objectOps"); const needle = require("needle"); const pluginConfig = require("./config") const isJson = require("./isJson") diff --git a/sharedPlugins/inventoryImports/test/interfacingFunctions.js b/sharedPlugins/inventoryImports/test/interfacingFunctions.js index 8624ec0b..33544a14 100644 --- a/sharedPlugins/inventoryImports/test/interfacingFunctions.js +++ b/sharedPlugins/inventoryImports/test/interfacingFunctions.js @@ -2,7 +2,7 @@ const assert = require("assert"); const child_process = require("child_process"); const functions = require("../interfacingFunctions"); const luamin = require("luamin"); -const isFactorioCommand = require("lib/isFactorioCommand"); +const isFactorioCommand = require("@clusterio/lib/isFactorioCommand"); const nock = require("nock"); // sinon does weird prototype manipulation and so doen't need to be saved to anything. diff --git a/test/clusterctl.js b/test/ctl.js similarity index 52% rename from test/clusterctl.js rename to test/ctl.js index ade97724..e293d294 100644 --- a/test/clusterctl.js +++ b/test/ctl.js @@ -1,24 +1,19 @@ "use strict"; const assert = require("assert").strict; -const chalk = require("chalk"); const { testLines } = require("./lib/factorio/lines"); -const clusterctl = require("../clusterctl.js"); +const clusterioctl = require("@clusterio/ctl/ctl.js"); -describe("clusterctl", function() { +describe("clusterioctl", function() { describe("formatOutputColored()", function() { it("should pass the test lines", function() { - // Ensure tests get uncoloured output. - let old = chalk.level; - chalk.level = 0; - for (let [reference, output] of testLines) { - let line = clusterctl._formatOutputColored(output); + let line = clusterioctl._formatOutputColored(output); + // Strip colours + line = line.replace(/\x1B\[\d+m/g, ""); assert.deepEqual(line, reference); } - - chalk.level = old; }); }); }); diff --git a/test/integration/clusterio.js b/test/integration/clusterio.js index e2c260bf..6daabafa 100644 --- a/test/integration/clusterio.js +++ b/test/integration/clusterio.js @@ -5,7 +5,7 @@ const path = require("path"); const validateHTML = require("html5-validator"); const parallel = require("mocha.parallel"); -const link = require("lib/link"); +const link = require("@clusterio/lib/link"); const { slowTest, get, execCtl, sendRcon, getControl, instancesDir } = require("./index"); @@ -18,7 +18,7 @@ describe("Integration of Clusterio", function() { for (let path of paths) { it(`sends some HTML when accessing ${path}`, async function() { let res = await get(path); - let validation = await validateHTML(res.body); + let validation = await validateHTML(res.body.toString()); let filtered = validation.messages.filter(msg => msg.type !== "info"); assert( filtered.length === 0, @@ -29,7 +29,7 @@ describe("Integration of Clusterio", function() { }); - describe("clusterctl", function() { + describe("clusterioctl", function() { describe("slave list", function() { it("runs", async function() { await execCtl("slave list"); @@ -197,6 +197,7 @@ describe("Integration of Clusterio", function() { } }); it("should not create the user if not instructed to", async function() { + slowTest(this); for (let list of lists) { try { await execCtl(`user set-${list} no_create_test`); @@ -206,6 +207,7 @@ describe("Integration of Clusterio", function() { assert.equal(user, undefined, "user was unexpectedly created"); }); it("should create the user if instructed to", async function() { + slowTest(this); for (let list of lists) { await execCtl(`user set-${list} --create test_create_${list}`); let user = (await getUsers()).get(`test_create_${list}`); diff --git a/test/integration/export.js b/test/integration/export.js index 6c6fc233..f50370d3 100644 --- a/test/integration/export.js +++ b/test/integration/export.js @@ -2,7 +2,7 @@ const assert = require("assert").strict; const path = require("path"); -const factorio = require("lib/factorio"); +const factorio = require("@clusterio/lib/factorio"); describe("Integration of lib/factorio/export", function() { diff --git a/test/integration/index.js b/test/integration/index.js index 21230fcf..ed05ce5c 100644 --- a/test/integration/index.js +++ b/test/integration/index.js @@ -3,12 +3,12 @@ const path = require("path"); const fs = require("fs-extra"); const child_process = require("child_process"); const jwt = require("jsonwebtoken"); -const needle = require("needle"); +const phin = require("phin"); const util = require("util"); const events = require("events"); -const link = require("lib/link"); -const server = require("lib/factorio/server"); +const link = require("@clusterio/lib/link"); +const server = require("@clusterio/lib/factorio/server"); class TestControl extends link.Link { @@ -29,7 +29,7 @@ class TestControl extends link.Link { class TestControlConnector extends link.WebSocketClientConnector { register() { - this.sendHandshake("register_control", { token: this.token, agent: "clusterctl", version: "test" }); + this.sendHandshake("register_control", { token: this.token, agent: "clusterioctl", version: "test" }); } } @@ -45,7 +45,11 @@ function slowTest(test) { } async function get(path) { - let res = await needle("get", `https://localhost:4443${path}`, { rejectUnauthorized: false }); + let res = await phin({ + method: "GET", + url: `https://localhost:4443${path}`, + core: { rejectUnauthorized: false }, + }); if (res.statusCode != 200) { throw new Error(`Got response code ${res.statusCode}, content: ${res.body}`); } @@ -70,7 +74,7 @@ async function exec(...args) { } async function execCtl(...args) { - args[0] = `node clusterctl --config ${controlConfigPath} ${args[0]}`; + args[0] = `node packages/ctl --config ${controlConfigPath} ${args[0]}`; return await exec(...args); } @@ -113,26 +117,26 @@ before(async function() { await fs.remove(slaveConfigPath); await fs.remove(controlConfigPath); - await exec(`node master --config ${masterConfigPath} config set master.database_directory ${databaseDir}`); - await exec(`node master --config ${masterConfigPath} config set master.auth_secret TestSecretDoNotUse`); - await exec(`node master --config ${masterConfigPath} config set master.http_port 8880`); - await exec(`node master --config ${masterConfigPath} config set master.https_port 4443`); - await exec(`node master --config ${masterConfigPath} config set master.tls_certificate ${path.join(databaseDir, "cert.crt")}`); - await exec(`node master --config ${masterConfigPath} config set master.tls_private_key ${path.join(databaseDir, "cert.key")}`); - await exec(`node master --config ${masterConfigPath} config set master.tls_bits 1024`); - await exec(`node master --config ${masterConfigPath} config set master.heartbeat_interval 0.25`); - await exec(`node master --config ${masterConfigPath} config set master.connector_shutdown_timeout 2`); + await exec(`node packages/master --config ${masterConfigPath} config set master.database_directory ${databaseDir}`); + await exec(`node packages/master --config ${masterConfigPath} config set master.auth_secret TestSecretDoNotUse`); + await exec(`node packages/master --config ${masterConfigPath} config set master.http_port 8880`); + await exec(`node packages/master --config ${masterConfigPath} config set master.https_port 4443`); + await exec(`node packages/master --config ${masterConfigPath} config set master.tls_certificate ${path.join(databaseDir, "cert.crt")}`); + await exec(`node packages/master --config ${masterConfigPath} config set master.tls_private_key ${path.join(databaseDir, "cert.key")}`); + await exec(`node packages/master --config ${masterConfigPath} config set master.tls_bits 1024`); + await exec(`node packages/master --config ${masterConfigPath} config set master.heartbeat_interval 0.25`); + await exec(`node packages/master --config ${masterConfigPath} config set master.connector_shutdown_timeout 2`); - await exec(`node master --config ${masterConfigPath} bootstrap create-admin test`); - await exec(`node master --config ${masterConfigPath} bootstrap create-ctl-config test --output ${controlConfigPath}`); + await exec(`node packages/master --config ${masterConfigPath} bootstrap create-admin test`); + await exec(`node packages/master --config ${masterConfigPath} bootstrap create-ctl-config test --output ${controlConfigPath}`); - masterProcess = await spawn("master:", `node master --config ${masterConfigPath} run`, "All plugins loaded"); + masterProcess = await spawn("master:", `node packages/master --config ${masterConfigPath} run`, "All plugins loaded"); let createArgs = `--id 4 --name slave --generate-token --output ${slaveConfigPath}`; await execCtl(`slave create-config ${createArgs}`); - await exec(`node slave --config ${slaveConfigPath} config set slave.instances_directory ${instancesDir}`); + await exec(`node packages/slave --config ${slaveConfigPath} config set slave.instances_directory ${instancesDir}`); - slaveProcess = await spawn("slave:", `node slave --config ${slaveConfigPath} run`, "SOCKET | received ready from master"); + slaveProcess = await spawn("slave:", `node packages/slave --config ${slaveConfigPath} run`, "SOCKET | received ready from master"); let controlConnector = new TestControlConnector(url, 2); controlConnector.token = controlToken; diff --git a/test/integration/patch.js b/test/integration/patch.js index 1baf6b1b..452ad712 100644 --- a/test/integration/patch.js +++ b/test/integration/patch.js @@ -4,7 +4,7 @@ const fs = require("fs-extra"); const jszip = require("jszip"); const path = require("path"); -const factorio = require("lib/factorio"); +const factorio = require("@clusterio/lib/factorio"); const { slowTest } = require("./index"); diff --git a/test/integration/server.js b/test/integration/server.js index 810570c1..0fe8f915 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -4,8 +4,8 @@ const events = require("events"); const fs = require("fs-extra"); const path = require("path"); -const factorio = require("lib/factorio"); -const errors = require("lib/errors"); +const factorio = require("@clusterio/lib/factorio"); +const errors = require("@clusterio/lib/errors"); const { slowTest } = require("./index"); diff --git a/test/lib/command.js b/test/lib/command.js index bc612782..f09db22e 100644 --- a/test/lib/command.js +++ b/test/lib/command.js @@ -1,9 +1,9 @@ "use strict"; const assert = require("assert").strict; -const command = require("lib/command"); -const errors = require("lib/errors"); -const link = require("lib/link"); +const command = require("@clusterio/lib/command"); +const errors = require("@clusterio/lib/errors"); +const link = require("@clusterio/lib/link"); const mock = require("../mock"); diff --git a/test/lib/config/classes.js b/test/lib/config/classes.js index 802a8860..d423f3e4 100644 --- a/test/lib/config/classes.js +++ b/test/lib/config/classes.js @@ -1,5 +1,5 @@ "use strict"; -const classes = require("lib/config/classes"); +const classes = require("@clusterio/lib/config/classes"); const assert = require("assert").strict; diff --git a/test/lib/database.js b/test/lib/database.js index 2b4670bc..1047e695 100644 --- a/test/lib/database.js +++ b/test/lib/database.js @@ -3,7 +3,7 @@ const assert = require("assert").strict; const fs = require("fs-extra"); const path = require("path"); -const database = require("lib/database"); +const database = require("@clusterio/lib/database"); describe("lib/database", function() { describe("mapToObject()", function() { diff --git a/test/lib/factorio/export.js b/test/lib/factorio/export.js index d4ce5691..a639a2da 100644 --- a/test/lib/factorio/export.js +++ b/test/lib/factorio/export.js @@ -2,7 +2,7 @@ const assert = require("assert").strict; const path = require("path"); -const factorio = require("lib/factorio"); +const factorio = require("@clusterio/lib/factorio"); describe("lib/factorio/export", function() { diff --git a/test/lib/factorio/patch.js b/test/lib/factorio/patch.js index 02a2527b..54d5109f 100644 --- a/test/lib/factorio/patch.js +++ b/test/lib/factorio/patch.js @@ -4,7 +4,7 @@ const fs = require("fs-extra"); const jszip = require("jszip"); const path = require("path"); -const factorio = require("lib/factorio"); +const factorio = require("@clusterio/lib/factorio"); describe("lib/factorio", function() { diff --git a/test/lib/factorio/server.js b/test/lib/factorio/server.js index f42438e0..32d21802 100644 --- a/test/lib/factorio/server.js +++ b/test/lib/factorio/server.js @@ -4,7 +4,7 @@ const events = require("events"); const fs = require("fs-extra"); const path = require("path"); -const factorio = require("lib/factorio"); +const factorio = require("@clusterio/lib/factorio"); const { testLines } = require("./lines"); diff --git a/test/lib/fileOps/fileOps.js b/test/lib/fileOps/fileOps.js index 30744860..9fb8d76a 100644 --- a/test/lib/fileOps/fileOps.js +++ b/test/lib/fileOps/fileOps.js @@ -3,7 +3,7 @@ const assert = require("assert").strict; const fs = require("fs-extra"); const path = require("path"); -const fileOps = require("lib/fileOps"); +const fileOps = require("@clusterio/lib/fileOps"); describe("fileOps.js", function(){ let baseDir = path.join("temp", "test", "fileOps"); diff --git a/test/lib/generateSSLcert.js b/test/lib/generateSSLcert.js index e64e6b43..a8fac889 100644 --- a/test/lib/generateSSLcert.js +++ b/test/lib/generateSSLcert.js @@ -3,7 +3,7 @@ const assert = require("assert"); const fs = require("fs-extra"); const path = require("path"); -const generateSSLcert = require("lib/generateSSLcert"); +const generateSSLcert = require("@clusterio/master/src/generate_ssl_cert"); describe("generateSSLcert.js(options)", function() { let testDir = path.join("temp", "test", "certTest"); diff --git a/test/lib/hash.js b/test/lib/hash.js index b12045d9..e349376a 100644 --- a/test/lib/hash.js +++ b/test/lib/hash.js @@ -4,7 +4,7 @@ const fs = require("fs"); const path = require("path"); const stream = require("stream"); -const hash = require("lib/hash"); +const hash = require("@clusterio/lib/hash"); describe("lib/hash", function() { diff --git a/test/lib/helpers.js b/test/lib/helpers.js index cdad02ad..30f0863c 100644 --- a/test/lib/helpers.js +++ b/test/lib/helpers.js @@ -1,7 +1,7 @@ "use strict"; const assert = require("assert").strict; -const helpers = require("lib/helpers"); +const helpers = require("@clusterio/lib/helpers"); describe("lib/helpers", function() { diff --git a/test/lib/link/connector.js b/test/lib/link/connector.js index 4d650d67..76783419 100644 --- a/test/lib/link/connector.js +++ b/test/lib/link/connector.js @@ -3,7 +3,7 @@ const assert = require("assert").strict; const events = require("events"); const mock = require("../../mock"); -const link = require("lib/link"); +const link = require("@clusterio/lib/link"); describe("lib/link/connectors", function() { diff --git a/test/lib/link/link.js b/test/lib/link/link.js index 9b860673..441600e1 100644 --- a/test/lib/link/link.js +++ b/test/lib/link/link.js @@ -2,9 +2,9 @@ const assert = require("assert").strict; const events = require("events"); -const link = require("lib/link"); -const errors = require("lib/errors"); -const schema = require("lib/schema"); +const link = require("@clusterio/lib/link"); +const errors = require("@clusterio/lib/errors"); +const schema = require("@clusterio/lib/schema"); const mock = require("../../mock"); diff --git a/test/lib/link/messages.js b/test/lib/link/messages.js index 25817638..74ba7f85 100644 --- a/test/lib/link/messages.js +++ b/test/lib/link/messages.js @@ -1,8 +1,8 @@ "use strict"; const assert = require("assert").strict; -const link = require("lib/link"); -const errors = require("lib/errors"); +const link = require("@clusterio/lib/link"); +const errors = require("@clusterio/lib/errors"); const mock = require("../../mock"); diff --git a/test/lib/luaTools.js b/test/lib/luaTools.js index c2958db1..55d5c408 100644 --- a/test/lib/luaTools.js +++ b/test/lib/luaTools.js @@ -1,7 +1,7 @@ "use strict"; const assert = require("assert").strict; -const luaTools = require("lib/luaTools"); +const luaTools = require("@clusterio/lib/luaTools"); describe("lib/luaTools", function() { diff --git a/test/lib/objectOps.js b/test/lib/objectOps.js index 8ce2a769..f975f280 100644 --- a/test/lib/objectOps.js +++ b/test/lib/objectOps.js @@ -1,6 +1,6 @@ "use strict"; var assert = require("assert"); -var objectOps = require("lib/objectOps"); +var objectOps = require("@clusterio/lib/objectOps"); describe("objectOps.js", function(){ describe("objectOps.deepclone()", function(){ diff --git a/test/lib/plugin.js b/test/lib/plugin.js index f1122edc..879e225f 100644 --- a/test/lib/plugin.js +++ b/test/lib/plugin.js @@ -4,9 +4,9 @@ const fs = require("fs-extra"); const path = require("path"); const mock = require("../mock"); -const link = require("lib/link"); -const plugin = require("lib/plugin"); -const errors = require("lib/errors"); +const link = require("@clusterio/lib/link"); +const plugin = require("@clusterio/lib/plugin"); +const errors = require("@clusterio/lib/errors"); describe("lib/plugin", function() { diff --git a/test/lib/prometheus.js b/test/lib/prometheus.js index d1602c47..f693c183 100644 --- a/test/lib/prometheus.js +++ b/test/lib/prometheus.js @@ -1,7 +1,7 @@ "use strict"; const assert = require("assert").strict; -const prometheus = require("lib/prometheus"); +const prometheus = require("@clusterio/lib/prometheus"); describe("lib/prometheus", function() { diff --git a/test/lib/users.js b/test/lib/users.js index 35788d1d..1dcf1d46 100644 --- a/test/lib/users.js +++ b/test/lib/users.js @@ -1,6 +1,6 @@ "use strict"; const assert = require("assert").strict; -const users = require("lib/users"); +const users = require("@clusterio/lib/users"); describe("lib/users", function() { diff --git a/test/master.js b/test/master.js index 128f106e..50510de8 100644 --- a/test/master.js +++ b/test/master.js @@ -2,7 +2,7 @@ const assert = require("assert").strict; const mock = require("./mock"); -const master = require("../master"); +const master = require("@clusterio/master/master"); describe("Master testing", function() { diff --git a/test/missing.js b/test/missing.js index 4cdbc87c..1c0ba611 100644 --- a/test/missing.js +++ b/test/missing.js @@ -2,5 +2,4 @@ // required here in order to track coverage for them. "use strict"; -require("lib/manager/modManager"); -require("lib/npmPostinstall.js"); +require("@clusterio/master/npm_post_install.js"); diff --git a/test/mock.js b/test/mock.js index 6fd759ec..03e851f9 100644 --- a/test/mock.js +++ b/test/mock.js @@ -1,7 +1,7 @@ "use strict"; const events = require("events"); -const link = require("lib/link"); +const link = require("@clusterio/lib/link"); class MockSocket { diff --git a/test/slave.js b/test/slave.js index 24749e2a..bb10399e 100644 --- a/test/slave.js +++ b/test/slave.js @@ -3,9 +3,9 @@ const assert = require("assert").strict; const path = require("path"); const fs = require("fs-extra"); -const link = require("lib/link"); -const config = require("lib/config"); -const slave = require("../slave"); +const link = require("@clusterio/lib/link"); +const config = require("@clusterio/lib/config"); +const slave = require("@clusterio/slave/slave"); describe("Slave testing", function() { before(function() {