Pull findRoot out into libZipOps

The findRoot function useful for managing mod zip files on the master.
This commit is contained in:
Hornwitser committed 2022-08-24 15:42:26 +02:00
1 parent 9a1c7b2ecd
commit a40fefc8d7
5 files changed
+92 -78

No files matched your search

+1 -37
View File
@@ -8,6 +8,7 @@ const path = require("path");
const semver = require("semver");
const libHash = require("../hash");
const { findRoot } = require("../zip_ops");
const knownScenarios = {
@@ -21,42 +22,6 @@ const knownScenarios = {
},
};
/**
* Returns the root folder in the zip file
*
* Returns the name of the folder that all files in the zip file is
* contained in. Throws an error if there are multiple such folders.
*
* @param {JSZip} zip - Zip to search through.
* @returns {string} name of the root folder.
* @memberof module:lib/factorio
* @private
* @inner
*/
function findRoot(zip) {
let root = null;
zip.forEach((relativePath, file) => {
let index = relativePath.indexOf("/");
if (index === -1) {
throw new Error(`Zip contains file '${relativePath}' in root dir`);
}
let pathRoot = relativePath.slice(0, index);
if (root === null) {
root = pathRoot;
} else if (root !== pathRoot) {
throw new Error("Zip contains multiple root folders");
}
});
if (root === null) {
throw new Error("Empty zip file");
}
return root;
}
/**
* Generates control.lua code for loading the Clusterio modules
*
@@ -295,7 +260,6 @@ module.exports = {
patch,
// For testing only
_findRoot: findRoot,
_generateLoader: generateLoader,
_reorderDependencies: reorderDependencies,
};
+2
View File
@@ -18,6 +18,7 @@ const libPrometheus = require("./prometheus");
const libSchema = require("./schema");
const libSharedCommands = require("./shared_commands");
const libUsers = require("./users");
const libZipOps = require("./zip_ops");
const ExponentialBackoff = require("./ExponentialBackoff");
const RateLimiter = require("./RateLimiter");
@@ -43,6 +44,7 @@ module.exports = {
libSchema,
libSharedCommands,
libUsers,
libZipOps,
ExponentialBackoff,
RateLimiter,
+40
View File
@@ -0,0 +1,40 @@
"use strict";
/**
* Returns the root folder in the zip file
*
* Returns the name of the folder that all files in the zip file is
* contained in. Throws an error if there are multiple such folders.
*
* @param {JSZip} zip - Zip to search through.
* @returns {string} name of the root folder.
* @memberof module:lib/factorio
* @private
* @inner
*/
function findRoot(zip) {
let root = null;
zip.forEach((relativePath, file) => {
let index = relativePath.indexOf("/");
if (index === -1) {
throw new Error(`Zip contains file '${relativePath}' in root dir`);
}
let pathRoot = relativePath.slice(0, index);
if (root === null) {
root = pathRoot;
} else if (root !== pathRoot) {
throw new Error("Zip contains multiple root folders");
}
});
if (root === null) {
throw new Error("Empty zip file");
}
return root;
}
module.exports = {
findRoot,
};