mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-13 23:14:53 +09:00
34 lines
833 B
TypeScript
34 lines
833 B
TypeScript
import type JSZip from "jszip";
|
|
|
|
/**
|
|
* 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 zip - Zip to search through.
|
|
* @returns name of the root folder.
|
|
*/
|
|
export function findRoot(zip: JSZip) {
|
|
let root: undefined | string;
|
|
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 === undefined) {
|
|
root = pathRoot;
|
|
} else if (root !== pathRoot) {
|
|
throw new Error("Zip contains multiple root folders");
|
|
}
|
|
});
|
|
|
|
if (root === undefined) {
|
|
throw new Error("Empty zip file");
|
|
}
|
|
|
|
return root;
|
|
}
|