UNPKG

@knennigtri/merge-markdown

Version:

Prepare input files to merge together and optionally output html/pdf/word presentation

607 lines (589 loc) 32.3 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _regeneratorRuntime() { "use strict"; var r = _regenerator(), e = r.m(_regeneratorRuntime), t = (Object.getPrototypeOf ? Object.getPrototypeOf(e) : e.__proto__).constructor; function n(r) { var e = "function" == typeof r && r.constructor; return !!e && (e === t || "GeneratorFunction" === (e.displayName || e.name)); } var o = { "throw": 1, "return": 2, "break": 3, "continue": 3 }; function a(r) { var e, t; return function (n) { e || (e = { stop: function stop() { return t(n.a, 2); }, "catch": function _catch() { return n.v; }, abrupt: function abrupt(r, e) { return t(n.a, o[r], e); }, delegateYield: function delegateYield(r, o, a) { return e.resultName = o, t(n.d, _regeneratorValues(r), a); }, finish: function finish(r) { return t(n.f, r); } }, t = function t(r, _t, o) { n.p = e.prev, n.n = e.next; try { return r(_t, o); } finally { e.next = n.n; } }), e.resultName && (e[e.resultName] = n.v, e.resultName = void 0), e.sent = n.v, e.next = n.n; try { return r.call(this, e); } finally { n.p = e.prev, n.n = e.next; } }; } return (_regeneratorRuntime = function _regeneratorRuntime() { return { wrap: function wrap(e, t, n, o) { return r.w(a(e), t, n, o && o.reverse()); }, isGeneratorFunction: n, mark: r.m, awrap: function awrap(r, e) { return new _OverloadYield(r, e); }, AsyncIterator: _regeneratorAsyncIterator, async: function async(r, e, t, o, u) { return (n(e) ? _regeneratorAsyncGen : _regeneratorAsync)(a(r), e, t, o, u); }, keys: _regeneratorKeys, values: _regeneratorValues }; })(); } function _regeneratorValues(e) { if (null != e) { var t = e["function" == typeof Symbol && Symbol.iterator || "@@iterator"], r = 0; if (t) return t.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) return { next: function next() { return e && r >= e.length && (e = void 0), { value: e && e[r++], done: !e }; } }; } throw new TypeError(_typeof(e) + " is not iterable"); } function _regeneratorKeys(e) { var n = Object(e), r = []; for (var t in n) r.unshift(t); return function e() { for (; r.length;) if ((t = r.pop()) in n) return e.value = t, e.done = !1, e; return e.done = !0, e; }; } function _regeneratorAsync(n, e, r, t, o) { var a = _regeneratorAsyncGen(n, e, r, t, o); return a.next().then(function (n) { return n.done ? n.value : a.next(); }); } function _regeneratorAsyncGen(r, e, t, o, n) { return new _regeneratorAsyncIterator(_regenerator().w(r, e, t, o), n || Promise); } function _regeneratorAsyncIterator(t, e) { function n(r, o, i, f) { try { var c = t[r](o), u = c.value; return u instanceof _OverloadYield ? e.resolve(u.v).then(function (t) { n("next", t, i, f); }, function (t) { n("throw", t, i, f); }) : e.resolve(u).then(function (t) { c.value = t, i(c); }, function (t) { return n("throw", t, i, f); }); } catch (t) { f(t); } } var r; this.next || (_regeneratorDefine2(_regeneratorAsyncIterator.prototype), _regeneratorDefine2(_regeneratorAsyncIterator.prototype, "function" == typeof Symbol && Symbol.asyncIterator || "@asyncIterator", function () { return this; })), _regeneratorDefine2(this, "_invoke", function (t, o, i) { function f() { return new e(function (e, r) { n(t, i, e, r); }); } return r = r ? r.then(f, f) : f(); }, !0); } function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); } function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { if (r) i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n;else { var o = function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); }; o("next", 0), o("throw", 1), o("return", 2); } }, _regeneratorDefine2(e, r, n, t); } function _OverloadYield(e, d) { this.v = e, this.k = d; } function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); } function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } var packageInfo = require("../package.json"); var dockerode = require("dockerode"); var fs = require("fs"); var path = require("path"); var tar = require("tar"); var manifestUtil = require("./manifest.js"); var presentationUtil = require("./presentation.js"); var mergeUtil = require("./merge.js"); var _require = require("child_process"), exec = _require.exec; var debug = require("debug"); var debugDocker = debug("docker"); var debugDockerPaths = debug("docker:paths"); exports.debbugOptions = { docker: "docker", dockerPaths: "docker:paths" }; var docker = new dockerode(); var IMAGE_NAME = "node"; var IMAGE_TAG = "mergemarkdown"; var CONTAINER_NAME = "mergemarkdown"; var WORKING_DIR = "/home/runner/workspace/cli"; var TAR_NAME = "archive.tar.gz"; var EXCLUDE_PATHS = [/node_modules/, // Matches any path containing 'node_modules' /quickstart.md/, /merged/, // Matches any path containing 'merged' /target/, // Matches any path containing 'target' ".git/", ".vscode/", "archive.tar.gz"]; function runMergeMarkdownInDocker(_x, _x2) { return _runMergeMarkdownInDocker.apply(this, arguments); } /* Starts a docker container or returns the running container */ function _runMergeMarkdownInDocker() { _runMergeMarkdownInDocker = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(manifestFileStr, cmdArgs) { var _manifest$docker; var manifest, manifestOutputName, excludePaths, manifestRelDir, dockerRunning, imageExists, needsRebuild, hasCompatibleNodeVersion, command; return _regeneratorRuntime().wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: manifest = manifestUtil.getJSON_withABSPaths(manifestFileStr); manifestOutputName = manifest.output.name; excludePaths = [].concat(EXCLUDE_PATHS, _toConsumableArray(Array.isArray((_manifest$docker = manifest.docker) === null || _manifest$docker === void 0 ? void 0 : _manifest$docker.excludePaths) ? manifest.docker.excludePaths : [])); debugDocker("excludePaths:\n".concat(excludePaths.map(function (regex) { return " ".concat(regex.toString()); }).join("\n"))); // relDir given in -m dir/manifest.yml to help build docker upload/download paths manifestRelDir = path.parse(manifestFileStr).dir || "./"; debugDocker("manifestDir: ".concat(manifestRelDir)); _context.prev = 6; _context.next = 9; return isDockerRunning(); case 9: dockerRunning = _context.sent; if (dockerRunning) { _context.next = 13; break; } console.error("Docker is not running. Start the Docker engine and retry."); return _context.abrupt("return"); case 13: _context.next = 15; return dockerImageExists("".concat(IMAGE_NAME, ":").concat(IMAGE_TAG)); case 15: imageExists = _context.sent; needsRebuild = false; if (imageExists) { _context.next = 22; break; } console.log("Docker Image DNE. Creating..."); needsRebuild = true; _context.next = 26; break; case 22: _context.next = 24; return checkDockerImageNodeVersion("".concat(IMAGE_NAME, ":").concat(IMAGE_TAG)); case 24: hasCompatibleNodeVersion = _context.sent; if (!hasCompatibleNodeVersion) { console.log("Docker Image exists but has incompatible Node.js version. Rebuilding with Node.js 20+..."); needsRebuild = true; } else { debugDocker("Docker Image exists and has compatible Node.js version"); } case 26: if (!needsRebuild) { _context.next = 31; break; } command = "docker compose -f ".concat(path.join(__dirname, "../docker/docker-compose.yml"), " up -d --build"); console.log(command); _context.next = 31; return runExecCommands(command, manifestRelDir).then(function (output) { console.log("Success.", output); })["catch"](function (error) { console.error("Error executing:", error); }); case 31: console.log("--------Docker Container START--------"); getContainer().then(function (container) { return container.inspect().then(function (info) { if (info.State.Running == true) return container;else return startContainer(container); }); }).then(function (resultContainer) { console.log("Cleaning the docker working directory."); var command = "rm -rf ".concat(WORKING_DIR, "/*"); debugDocker(command); return execContainer(resultContainer, command); }).then(function (resultContainer) { console.log("Copying this project to the docker container."); console.log("Exclude Copying These Paths:\n".concat(excludePaths.map(function (regex) { return " ".concat(regex.toString()); }).join("\n"))); return createTarArchive("./", TAR_NAME, excludePaths).then(function () { console.log("Tar archive created successfully"); return copyIntoContainer(resultContainer, TAR_NAME, WORKING_DIR); }); }).then(function (resultContainer) { console.log("Extracting files into the container."); var extractCommand = "tar xzf ".concat(WORKING_DIR, "/").concat(TAR_NAME, " -C ").concat(WORKING_DIR); debugDocker(extractCommand); return execContainer(resultContainer, extractCommand); }).then(function (resultContainer) { debugDocker("Running container is ".concat(resultContainer.id)); var npmModuleName = packageInfo.name.replace(/@[^/]+\//, ""); var requestedCMD = "".concat(npmModuleName, " ").concat(cmdArgs); requestedCMD = requestedCMD.replace("--docker", ""); var commands = ["cd ".concat(WORKING_DIR), requestedCMD].join(" && "); var dockerCMD = ["/bin/sh", "-c", commands]; debugDocker(dockerCMD); debugDocker("Merging in Docker"); return execContainer(resultContainer, dockerCMD, true); }).then(function (resultContainer) { debugDocker("Downloading locally"); debugDockerPaths("WORKING_DIR: ".concat(WORKING_DIR)); debugDockerPaths("manifestOutputName: ".concat(manifestOutputName)); debugDockerPaths("manifestRelDir: ".concat(manifestRelDir)); debugDockerPaths("process.cwd(): ".concat(process.cwd())); //ABS path where the npm command was executed var relManifestOutputName = path.relative(process.cwd(), manifestOutputName); var absManifestOutputName_inDocker = path.join(WORKING_DIR, relManifestOutputName); var absManifestOutputName_parsed = path.parse(absManifestOutputName_inDocker); var absManifestOutputName_name = absManifestOutputName_parsed.name; // Get the base filename without extension var absManifestOutputName_dir = absManifestOutputName_parsed.dir; // Get the docker directory of output debugDockerPaths("Downloading merged Files from: ".concat(absManifestOutputName_dir)); var outputPaths = [absManifestOutputName_inDocker].concat(_toConsumableArray(Object.values(presentationUtil.EXTS).map(function (ext) { return path.join(absManifestOutputName_dir, "".concat(absManifestOutputName_name).concat(ext)); })), _toConsumableArray(Object.values(mergeUtil.EXTS).map(function (ext) { return path.join(absManifestOutputName_dir, "".concat(absManifestOutputName_name).concat(ext)); })), [path.join(absManifestOutputName_dir, "temp.html")]); debugDockerPaths("Downloading files if they exist:\n".concat(outputPaths.map(function (p) { return " ".concat(p); }).join("\n"))); var destPath = path.parse(manifestOutputName).dir; debugDockerPaths("Downloading to ".concat(destPath)); // Ensure clean destination directory - delete and recreate if (fs.existsSync(destPath)) { fs.rmSync(destPath, { recursive: true, force: true }); debugDockerPaths("Deleted existing directory: ".concat(destPath)); } fs.mkdirSync(destPath, { recursive: true }); debugDockerPaths("Created clean directory: ".concat(destPath)); return downloadFromContainer(resultContainer, outputPaths, destPath); }).then(function () { if (debug.enabled("docker")) { debugDocker("Skipping ".concat(TAR_NAME, " file deletion in debug mode")); } else { var fileToDel = path.join("./", TAR_NAME); fs.unlink(fileToDel, function (err) { if (err) { console.error("Could not delete ".concat(fileToDel)); } }); } console.log("--------Docker Container END--------"); }); _context.next = 39; break; case 35: _context.prev = 35; _context.t0 = _context["catch"](6); console.error("Error getting or creating container:", _context.t0); throw _context.t0; case 39: case "end": return _context.stop(); } }, _callee, null, [[6, 35]]); })); return _runMergeMarkdownInDocker.apply(this, arguments); } function startContainer(container) { return new Promise(function (resolve, reject) { debugDocker("Starting container: ".concat(container.id)); container.start(function (err) { if (err) { if (err.statusCode === 304) { debugDocker("304. Container already running."); resolve(container); } else { console.error("Error starting container:", err); reject(err); } } else { debugDocker("Container started successfully"); resolve(container); } }); }); } /* Gets the running container or starts and returns it */ function getContainer(containerName) { var name = containerName || CONTAINER_NAME; debugDocker("Getting containter: ".concat(name)); return new Promise(function (resolve, reject) { docker.getContainer(name).inspect(function (err, data) { if (err) { if (err.statusCode === 404) { debugDocker("404. Container doesn't exist. Creating..."); resolve(createContainer()); } else reject(err); } else { debugDocker("Found container ".concat(name, " with ID: ").concat(data.Id)); resolve(docker.getContainer(data.Id)); //returns a container } }); }); } /* Function to create a container based on an existing image */ function createContainer(containerName, imageName) { var name = containerName || CONTAINER_NAME; var img = imageName || "".concat(IMAGE_NAME, ":").concat(IMAGE_TAG); debugDocker("Creating container ".concat(name, " from image ").concat(img)); return new Promise(function (resolve, reject) { docker.createContainer({ Image: img, Tty: true, name: name }, function (err, container) { if (err) reject(err); // Failed to create container debugDocker("Container created: ".concat(container.id)); resolve(startContainer(container)); }); }); } /* execute commands within the running container */ function execContainer(_x3, _x4, _x5) { return _execContainer.apply(this, arguments); } function _execContainer() { _execContainer = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(container, command, attachStd) { var execOptions; return _regeneratorRuntime().wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: execOptions = { Cmd: typeof command === "string" ? command.split(" ") : Array.isArray(command) ? command : [], AttachStdout: attachStd, AttachStderr: attachStd }; return _context2.abrupt("return", new Promise(function (resolve, reject) { container.exec(execOptions, function (err, exec) { console.log("Executing command: ", execOptions.Cmd.join(" ")); var dockerConsole = " "; var finalOutput = ""; if (err) { console.error("Error creating exec instance:", err); reject(err); } else { exec.start(function (err, stream) { if (err) { console.error("Error starting exec instance:", err); reject(err); } else { stream.on("data", function (chunk) { finalOutput = finalOutput + dockerConsole + chunk; }); exec.inspect(function (err, data) { if (err) { console.error("Error inspecting exec instance:", err); } else { console.log(dockerConsole + "Running: ", data.ProcessConfig.entrypoint); } }); stream.on("end", function () { console.log(finalOutput); resolve(container); }); } }); } }); })); case 2: case "end": return _context2.stop(); } }, _callee2); })); return _execContainer.apply(this, arguments); } function dockerImageExists(imageName) { var img = imageName || "".concat(IMAGE_NAME, ":").concat(IMAGE_TAG); return new Promise(function (resolve, reject) { docker.listImages({ filters: { reference: [img] } }, function (err, images) { if (images.length > 0) { resolve(true); } else { resolve(false); } }); }); } /* Check if the Docker image has a compatible Node.js version */ function checkDockerImageNodeVersion(_x6) { return _checkDockerImageNodeVersion.apply(this, arguments); } function _checkDockerImageNodeVersion() { _checkDockerImageNodeVersion = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(imageName) { var img, container, stream, output, versionMatch, majorVersion; return _regeneratorRuntime().wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: img = imageName || "".concat(IMAGE_NAME, ":").concat(IMAGE_TAG); debugDocker("Checking Node.js version in image: ".concat(img)); _context3.prev = 2; _context3.next = 5; return docker.createContainer({ Image: img, Cmd: ["node", "--version"], AttachStdout: true, AttachStderr: true }); case 5: container = _context3.sent; _context3.next = 8; return container.attach({ stream: true, stdout: true, stderr: true }); case 8: stream = _context3.sent; output = ""; stream.on("data", function (chunk) { output += chunk.toString(); }); _context3.next = 13; return container.start(); case 13: _context3.next = 15; return container.wait(); case 15: _context3.next = 17; return container.remove(); case 17: // Extract version number (e.g., "v20.1.0" -> 20) versionMatch = output.match(/v(\d+)\./); if (!versionMatch) { _context3.next = 22; break; } majorVersion = parseInt(versionMatch[1]); debugDocker("Found Node.js version: ".concat(majorVersion)); // Require Node.js 20 or higher for proper Web API support return _context3.abrupt("return", majorVersion >= 20); case 22: debugDocker("Could not parse Node.js version from: ".concat(output)); return _context3.abrupt("return", false); case 26: _context3.prev = 26; _context3.t0 = _context3["catch"](2); debugDocker("Error checking Node.js version: ".concat(_context3.t0.message)); // If we can't check the version, assume it's outdated and needs rebuild return _context3.abrupt("return", false); case 30: case "end": return _context3.stop(); } }, _callee3, null, [[2, 26]]); })); return _checkDockerImageNodeVersion.apply(this, arguments); } function isDockerRunning() { return new Promise(function (resolve, reject) { docker.ping(function (err) { if (err) { resolve(false); // Docker is not running or not accessible } else { resolve(true); // Docker is running } }); }); } /* Run command line arguments with exec */ function runExecCommands(command, cwd) { return new Promise(function (resolve, reject) { exec(command, { cwd: cwd }, function (error, stdout, stderr) { if (error) { reject(error); return; } if (stderr) { reject(stderr); return; } resolve(stdout); }); }); } /* Copy the local tarFilePath into the destDir in the container */ function copyIntoContainer(container, tarFilePath, destDir) { debugDocker("Copying ".concat(tarFilePath, " to ").concat(destDir)); return new Promise(function (resolve, reject) { var tarStream = fs.createReadStream(tarFilePath); container.putArchive(tarStream, { path: destDir }, function (err) { if (err) { reject(err); return; } resolve(container); }); }); } /* Takes the sourceDir and compresses it into a tar at tarFilePath, excluding specified paths using regex */ function createTarArchive(sourceDir, tarFilePath) { var excludedPaths = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; debugDocker("".concat(sourceDir, " to tarball ").concat(tarFilePath)); return new Promise(function (resolve, reject) { var includedCount = 0; var tarStream = tar.c({ gzip: true, cwd: sourceDir, filter: function filter(filePath) { var relativePath = path.relative(sourceDir, filePath); // Normalize path separators to forward slashes for consistent regex matching var normalizedPath = relativePath.replace(/\\/g, "/"); // Check if this path or any parent path should be excluded var shouldExclude = excludedPaths.some(function (excludedPath) { var regex = typeof excludedPath === "string" ? new RegExp(excludedPath) : excludedPath; // Check if the current path matches if (regex.test(normalizedPath)) { debugDockerPaths("Excluding: ".concat(normalizedPath, " (matched: ").concat(regex.toString(), ")")); return true; } // Check if this path is inside an excluded directory var pathParts = normalizedPath.split("/"); for (var i = 0; i < pathParts.length; i++) { var partialPath = pathParts.slice(0, i + 1).join("/"); if (regex.test(partialPath)) { debugDockerPaths("Excluding: ".concat(normalizedPath, " (parent matched: ").concat(regex.toString(), ")")); return true; } } return false; }); if (!shouldExclude) { includedCount++; debugDockerPaths("Including: ".concat(normalizedPath)); } return !shouldExclude; } }, ["."]); var fileStream = fs.createWriteStream(tarFilePath); tarStream.pipe(fileStream); tarStream.on("end", function () { debugDocker("Archive created with ".concat(includedCount, " files")); if (includedCount === 0) { console.warn("Warning: No files were included in the archive. Check exclude patterns."); } resolve(); }); tarStream.on("error", function (err) { console.error("Error creating tar archive:", err); reject(err); }); fileStream.on("error", function (err) { console.error("Error writing tar file:", err); reject(err); }); }); } /* Downloads the srcPath in the container to the local destPath */ function downloadFromContainer(_x7, _x8, _x9) { return _downloadFromContainer.apply(this, arguments); } function _downloadFromContainer() { _downloadFromContainer = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(container, srcPaths, destPath) { var downloadPromises; return _regeneratorRuntime().wrap(function _callee5$(_context5) { while (1) switch (_context5.prev = _context5.next) { case 0: if (!Array.isArray(srcPaths)) { srcPaths = [srcPaths]; } if (!destPath) destPath = "./"; // Ensure the destination directory exists if (!fs.existsSync(destPath)) { fs.mkdirSync(destPath, { recursive: true }); } console.log("Downloading files to ".concat(destPath)); downloadPromises = srcPaths.map(/*#__PURE__*/function () { var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(srcPath) { var stream; return _regeneratorRuntime().wrap(function _callee4$(_context4) { while (1) switch (_context4.prev = _context4.next) { case 0: _context4.prev = 0; _context4.next = 3; return container.getArchive({ path: srcPath }); case 3: stream = _context4.sent; return _context4.abrupt("return", new Promise(function (resolve, reject) { stream.pipe(tar.extract({ cwd: destPath })); stream.on("end", function () { debugDocker("Output ".concat(srcPath.replace(WORKING_DIR, "."), " downloading..")); resolve(); }); stream.on("error", function (err) { reject(err); }); })); case 7: _context4.prev = 7; _context4.t0 = _context4["catch"](0); debugDocker("File ".concat(srcPath, " not found, skipping")); return _context4.abrupt("return", null); case 11: case "end": return _context4.stop(); } }, _callee4, null, [[0, 7]]); })); return function (_x10) { return _ref.apply(this, arguments); }; }()); _context5.next = 7; return Promise.all(downloadPromises); case 7: return _context5.abrupt("return", container); case 8: case "end": return _context5.stop(); } }, _callee5); })); return _downloadFromContainer.apply(this, arguments); } exports.runMergeMarkdownInDocker = runMergeMarkdownInDocker;