@knennigtri/merge-markdown
Version:
Prepare input files to merge together and optionally output html/pdf/word presentation
462 lines (442 loc) • 24 kB
JavaScript
;
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 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 manifestUtil = require("./manifest.js");
var path = require("path");
var fs = require("fs");
var mkdirp = require("mkdirp");
var concat = require("concat");
var markdownLinkCheck = require("markdown-link-check");
var doctoc = require("doctoc/lib/transform");
var validUrl = require("valid-url");
var debug = require("debug")("merge");
var debugRelLinks = require("debug")("rellinks");
var debugYaml = require("debug")("o:yaml");
var debugDoctoc = require("debug")("o:doctoc");
var debugReplace = require("debug")("o:replace");
var debugLinkcheck = require("debug")("linkcheck");
var debugLinkcheckDeep = require("debug")("linkcheck:deep");
var EXTS = {
"linkcheck": ".linkcheck.md",
"qa": ".qa.md"
};
exports.EXTS = EXTS;
exports.debbugOptions = {
"merge": "messages for merge process",
"rellinks": "relative links",
"o:yaml": "yaml removal",
"o:doctoc": "doctoc messages",
"o:replace": "regex replace messages",
"linkcheck": "linkcheck validation",
"linkcheck:deep": "deep linkcheck validation"
};
function start(manifestFileStr, qaMode, skip_linkcheck, maintainAssetPaths) {
var onlyQA = qaMode || false;
var skipLinkcheck = skip_linkcheck || false;
var keepAssetPaths = maintainAssetPaths || false;
//Set manifest variables
var manifest = manifestUtil.getJSON_withABSPaths(manifestFileStr, onlyQA);
var manifestOutputName = manifest.output.name;
var manifestInput = manifest.input;
var manifestQARegEx;
if (manifest.qa) manifestQARegEx = new RegExp(manifest.qa.exclude);
if (onlyQA) console.log("QA exclude regex: " + manifestQARegEx);
if (skipLinkcheck) console.log("Skipping linkcheck on all files");
//removes old .md files
deleteGeneratedFiles([manifestOutputName, replaceExtension(manifestOutputName, EXTS.linkcheck), replaceExtension(manifestOutputName, EXTS.qa)]);
//Iterate through all of the input files in manifest apply options
var fileArr = [];
var refFileArr = [];
Object.keys(manifestInput).forEach(function (inputKey) {
var inputFileName = path.basename(inputKey);
console.log("--" + inputFileName + "--");
if (onlyQA && manifestQARegEx.test(inputFileName)) {
console.log("Skipping " + inputFileName + " for QA");
return;
}
if (!fs.existsSync(inputKey)) {
console.log(inputFileName + " does not exist. Skipping.");
return;
}
var origContent = fs.readFileSync(inputKey, "utf-8");
//Force a new line at the end of the file to help with file merging
origContent += "\n";
origContent += "";
//updates all relative asset paths to the relative output location
var generatedContent = updateAssetRelPaths(origContent, path.dirname(inputKey), path.dirname(manifestOutputName), keepAssetPaths);
debug("--Apply file OPTIONS--");
//Content, local options, global options
generatedContent = applyContentOptions(generatedContent, manifestInput[inputKey], manifest);
var tempFile = inputKey + ".temp";
fs.writeFileSync(tempFile, generatedContent);
if (!skipLinkcheck) {
//checks for broken links within the content
debug("--Create/Update linkcheck file--");
linkcheck(tempFile, replaceExtension(manifestOutputName, EXTS.linkcheck));
}
//add the temp file to the list to merge together
fileArr.push(tempFile);
debug(path.basename(tempFile));
console.log("...added to merge list");
});
console.log("+++++++++++++");
//Merge lists and output single markdown file
var mergedFileArr = fileArr.concat(refFileArr);
console.log("Creating Merged Markdown:\n " + mergedFileArr.join("\n "));
var fileToWrite = manifestOutputName;
if (onlyQA) {
fileToWrite = replaceExtension(fileToWrite, EXTS.qa);
}
return createSingleFile(mergedFileArr, fileToWrite, manifest);
}
function createSingleFile(_x, _x2, _x3) {
return _createSingleFile.apply(this, arguments);
} //Content, local options, global options
function _createSingleFile() {
_createSingleFile = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(list, fileToWrite, manifestJSON) {
return _regeneratorRuntime().wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
debug("Creating single file");
if (!(!list || list.length === 0)) {
_context.next = 4;
break;
}
console.log("List to merge is not valid. Aborting..");
return _context.abrupt("return", Promise.reject(new Error("Invalid list")));
case 4:
return _context.abrupt("return", concat(list).then(function (resultContent) {
if (Object.prototype.hasOwnProperty.call(manifestJSON.output, "doctoc") && manifestJSON.output.doctoc) {
manifestJSON.output.doctoc;
var outDoctoc = optionBuildTOC(resultContent, manifestJSON.output.doctoc, manifestJSON.doctoc);
return safelyWriteFile(fileToWrite, outDoctoc);
} else {
return safelyWriteFile(fileToWrite, resultContent);
}
}).then(function (result) {
deleteGeneratedFiles(list); //cleanup
return result;
}));
case 5:
case "end":
return _context.stop();
}
}, _callee);
}));
return _createSingleFile.apply(this, arguments);
}
function applyContentOptions(origContent, fileOptions, globalOptions) {
var scrubbedContent = origContent;
// if(fileOptions == undefined || fileOptions == "") return scrubbedContent;
/* Apply noYAML */
//Apply local noYAML option
if (fileOptions && Object.prototype.hasOwnProperty.call(fileOptions, "noYAML")) {
if (fileOptions.noYAML) {
debug("Using [Local] noYAML...");
scrubbedContent = optionRemoveYAML(origContent);
}
} else
//Apply global noYAML option
if (Object.prototype.hasOwnProperty.call(globalOptions, "noYAML") && globalOptions.noYAML) {
debug("Using [Global] noYAML...");
scrubbedContent = optionRemoveYAML(origContent);
}
/* Apply find and replace */
//Apply local replace
if (fileOptions && Object.prototype.hasOwnProperty.call(fileOptions, "replace")) {
// merge global replace with local replace taking precedence
if (Object.prototype.hasOwnProperty.call(globalOptions, "replace")) {
for (var key in globalOptions.replace) {
if (!Object.prototype.hasOwnProperty.call(fileOptions.replace, key)) {
fileOptions.replace[key] = globalOptions.replace[key];
}
}
debug("Using [Local/Global] Find/Replace...");
scrubbedContent = optionReplaceStrings(scrubbedContent, fileOptions.replace);
} else {
debug("Using [Local] Find/Replace...");
scrubbedContent = optionReplaceStrings(scrubbedContent, fileOptions.replace);
}
} else
//Apply global replace
if (Object.prototype.hasOwnProperty.call(globalOptions, "replace")) {
debug("Using [Global] Find/Replace...");
scrubbedContent = optionReplaceStrings(scrubbedContent, globalOptions.replace);
}
//Add TOC
if (fileOptions && Object.prototype.hasOwnProperty.call(fileOptions, "doctoc")) {
if (fileOptions.doctoc) {
if (Object.prototype.hasOwnProperty.call(globalOptions, "doctoc")) {
debugDoctoc("Using [Local/Global] DocToc...");
scrubbedContent = optionBuildTOC(scrubbedContent, fileOptions.doctoc, globalOptions.doctoc);
} else {
debugDoctoc("Using [Local] DocToc...");
scrubbedContent = optionBuildTOC(scrubbedContent, fileOptions.doctoc);
}
}
} else if (Object.prototype.hasOwnProperty.call(globalOptions, "doctoc") && globalOptions.doctoc) {
debug("Using [Global] DocToc...");
scrubbedContent = optionBuildTOC(scrubbedContent, null, globalOptions.doctoc);
}
return scrubbedContent;
}
/** Searches for  or src="relPath" in a string and replaces the asset
* relPath with an absolute one
*/
function updateAssetRelPaths(fileContents, inputFilePath, mergedFilePath, keepAssetPaths) {
var resultContent = [];
var regex = /(!\[(.*?)\][(](.*?)[)])|(src=[""](.*?)[""])/g;
debug("Rewriting Relative Asset Paths");
debugRelLinks("inputFilePath: " + inputFilePath);
debugRelLinks("mergedFilePath: " + mergedFilePath);
//Go through each line that and
var lines = fileContents.split("\n");
var count = 0;
lines.forEach(function (line, lineNumber) {
var match = line.match(regex);
var origAssetPath = "";
//if found capture relPath
if (match != null) {
var origStr = match[0];
//Example: 
if (origStr.startsWith("![")) {
origAssetPath = origStr.substring(origStr.indexOf("(") + 1, origStr.indexOf(")"));
} else {
//Example: src="links/my-asset.jpg"
if (origStr.startsWith("src=")) {
origAssetPath = origStr.substring(origStr.indexOf("\"") + 1, origStr.lastIndexOf("\""));
}
}
//Check if path is a URL
if (!validUrl.isUri(origAssetPath)) {
var newAssetPath = "";
//patch for issue #51
if (path.isAbsolute(origAssetPath)) {
debugRelLinks("OrigPath");
newAssetPath = origAssetPath;
} else {
//resolve the asset path
newAssetPath = path.resolve(inputFilePath, origAssetPath);
if (keepAssetPaths) {
debugRelLinks("relPath");
newAssetPath = path.relative(mergedFilePath, newAssetPath);
}
}
count++;
debugRelLinks("[Line " + lineNumber + "]: " + origAssetPath);
debugRelLinks("Updated to: " + newAssetPath);
var newLine = line.replace(origAssetPath, newAssetPath);
resultContent.push(newLine);
} else {
resultContent.push(line);
}
} else {
resultContent.push(line);
}
});
debugRelLinks(count + " relative paths updated");
return resultContent.join("\n");
}
/** Removes the YAML at the top of a markdown file
* @param {*} fileContents String that might contain YAML
* @returns String that has YAML removed
*/
function optionRemoveYAML(fileContents) {
debug("[OPTION] Remove YAML...");
var resultContent = fileContents;
var lines = fileContents.split("\n");
var i = 0;
var startYAML = -1;
var endYAML = -1;
for (i = 0; i < lines.length; i++) {
var line = lines[i];
//Stop searching if there is no YAML
if (i == 0 && line != "---") {
break;
}
//Set start/end YAML indexes if they are available
if (i == 0 && startYAML == -1 && line == "---") {
startYAML = i;
} else if (startYAML != -1 && endYAML == -1 && line == "---") {
endYAML = i;
break;
} //TODO YAML is on line 2 or (\n\r before YAML..)
//Stop searching if a header # is reached
if (startYAML != -1 && line.startsWith("#")) {
break;
}
}
debugYaml("S(" + startYAML + ")E(" + endYAML + ")✓ d(" + i + ")T(" + lines.length + ")");
if (startYAML != -1 && endYAML != -1) {
//shows YAML being removed
debugYaml("Removing S(" + startYAML + ")->E(" + endYAML + ") YAML:");
var yaml = lines.splice(startYAML, 1 + endYAML - startYAML).join("\n");
debugYaml(yaml);
resultContent = lines.join("\n");
debugYaml("YAML removed");
} else {
debugYaml("No YAML found for removal");
}
return resultContent;
}
/** Replaces any value with another value.
* Regex values are allowed and will be wrapped with /str/g
* Debug=merge:replace
*/
function optionReplaceStrings(fileContents, replacements) {
debug("[OPTION] Find and Replace Strings...");
var replacedContent = fileContents;
Object.keys(replacements).forEach(function (replaceKey) {
var findRegex = "";
findRegex = new RegExp(replaceKey, "g");
var replaceStr = replacements[replaceKey];
debugReplace("Replacing: " + findRegex + " with: " + replaceStr);
replacedContent = replacedContent.replace(findRegex, replaceStr);
});
return replacedContent;
}
/*
*/
function optionBuildTOC(fileContents, doctocLocal, doctocGlobal) {
debug("[OPTION] Running doctoc...");
var includeTOC = false;
//TODO make this JSON available in DEBUG=defaults
var defaultDocToc = {
"mode": "github",
"maxlevel": 3,
"title": "",
"notitle": true,
"entryprefix": "",
"all": false,
"stdout": true,
"update-only": false
};
var finalDoctoc = defaultDocToc;
var obj = {
doctocGlobal: doctocGlobal,
doctocLocal: doctocLocal
};
for (var options in obj) {
if (options == "doctocGlobal") debugDoctoc("Apply Global Options: " + JSON.stringify(obj[options]));else debugDoctoc("Apply Local Options: " + JSON.stringify(obj[options]));
if (obj[options] != undefined) {
if (typeof obj[options] === "boolean") {
includeTOC = obj[options];
} else if (typeof obj[options] === "string") {
finalDoctoc.title = obj[options];
finalDoctoc.notitle = false;
includeTOC = true;
} else {
for (var key in finalDoctoc) {
if (Object.prototype.hasOwnProperty.call(obj[options], key)) {
finalDoctoc[key] = obj[options][key];
if (key == "title") finalDoctoc.notitle = false;
includeTOC = true;
}
}
}
}
}
if (includeTOC) {
debugDoctoc(JSON.stringify(finalDoctoc, null, 2));
var out = doctoc(fileContents, "github.com", finalDoctoc.maxlevel, finalDoctoc.title, finalDoctoc.notitle, finalDoctoc.entryprefix, finalDoctoc.all, finalDoctoc.stdout, finalDoctoc["update-only"]);
if (!out.transformed) {
debugDoctoc("No generated TOC based on document and maxlevel");
return fileContents;
}
if (out.data == null) return;
debugDoctoc("doctoc TOC generated");
return out.data;
}
return fileContents;
}
/** Function that uses markdown-link-check to validate all URLS and relative links to images
* Writes a file called outputFile.linkcheck.md with the results
* https://github.com/tcort/markdown-link-check
* DEBUG=merge:linkcheck
*/
function linkcheck(inputFileStr, fileToWrite) {
debugLinkcheck("LC start");
var inputFolder = path.dirname(inputFileStr);
var base = new URL(path.join("file:", path.resolve(inputFolder))); //TODO Might be failing on windows
var fileContents = fs.readFileSync(inputFileStr, "utf-8");
debugLinkcheck("Async linkcheck for " + inputFileStr + " added.");
markdownLinkCheck(fileContents, {
baseUrl: base,
ignorePatterns: [{
pattern: "^http://localhost"
}],
timeout: "2s"
}, function (err, results) {
if (err) {
console.error("markdownlinkcheck failed on: " + inputFileStr);
return;
}
var linkcheckResults = "FILE: " + inputFileStr;
debugLinkcheck(linkcheckResults);
linkcheckResults += " \n";
results.forEach(function (result) {
var icon = "";
switch (result.status) {
case "alive":
icon = "✓";
break;
case "dead":
icon = "x";
break;
case "ignored":
icon = "-";
break;
}
var statusStr = "[" + icon + "] " + result.link + " is " + result.status;
debugLinkcheckDeep(statusStr);
if (result.status != "alive") {
linkcheckResults += " " + statusStr + " \n";
}
});
debugLinkcheck("deep complete");
linkcheckResults += "\n " + results.length + " links checked. \n\n \n";
if (fs.existsSync(fileToWrite)) {
return safelyWriteFile(fileToWrite, linkcheckResults, true);
} else {
return safelyWriteFile(fileToWrite, linkcheckResults);
}
});
}
function replaceExtension(filePath, newExt) {
var dir = path.dirname(filePath);
var name = path.basename(filePath, path.extname(filePath));
var ext = newExt.startsWith(".") ? newExt : "." + newExt;
return path.join(dir, name + ext);
}
function deleteGeneratedFiles(fileArr) {
fileArr.forEach(function (element) {
if (fs.existsSync(element)) {
fs.unlinkSync(element);
}
});
}
function safelyWriteFile(filePath, contents, append) {
try {
//Create the output directory structure if it doesn't exist
mkdirp.sync(path.dirname(filePath));
if (append) {
fs.appendFileSync(filePath, contents, "utf-8");
} else {
fs.writeFileSync(filePath, contents, "utf-8");
}
return filePath;
} catch (err) {
console.error("Error writing file: " + filePath);
}
}
exports.start = start;