UNPKG

@tts-tools/savefile

Version:

Module to extract a savefile from Tabletop Simulator into multiple files.

50 lines 1.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.bundleObject = exports.bundleSave = void 0; const xmlbundle_1 = require("@tts-tools/xmlbundle"); const lodash_1 = require("lodash"); const luabundle_1 = require("luabundle"); const path_1 = require("path"); /** * Creates a copy of the given save file and bundles all Lua/XML scripts with the given options. */ const bundleSave = (saveFile, options) => { return (0, lodash_1.cloneDeepWith)(saveFile, bundler(options)); }; exports.bundleSave = bundleSave; /** * Create a copy of the given object and bundles its own and contained Lua/XML scripts with the given options. */ const bundleObject = (object, options) => { return (0, lodash_1.cloneDeepWith)(object, bundler(options)); }; exports.bundleObject = bundleObject; const bundler = (options) => { return (value, key, obj) => { if (key === "LuaScript" && value) { return luaBundle(obj.LuaScript, options.includePath); } else if (key == "XmlUI" && value) { return (0, xmlbundle_1.bundle)(value, options.includePath); } return undefined; }; }; /** * Bundles the given Lua `script` by resolving `require()` calls using the given `includePath`. * * @param script The script content. * @param includePaths The path to look for additional includes. * @returns The bundled script. */ const luaBundle = (script, includePaths) => { if (typeof includePaths === "string") { includePaths = [includePaths]; } const patterns = ["?.lua", "?.ttslua"]; const paths = includePaths.flatMap((path) => patterns.map((pattern) => (0, path_1.join)(path, pattern))); paths.push(...patterns); const bundled = (0, luabundle_1.bundleString)(script, { paths }); return bundled.startsWith("-- Bundled") ? bundled + "\n" : bundled; }; //# sourceMappingURL=bundle.js.map