{"version":3,"sources":["files.ts"],"sourcesContent":["/**\n * Checks if a file exists, querying the (cached)\n * directory index file. Returns true if no directory\n * index was found\n */\n\nimport { Path } from \"datex-core-legacy/utils/path.ts\";\n\nconst directoryIndexFiles = new Map<string, object>();\n\nexport async function fileExists(url: URL|string, {cached, fallbackRequest}: {cached?: boolean, fallbackRequest?: boolean} = {cached: true, fallbackRequest: true}) {\n\tconst urlPath = url instanceof Path ? url : new Path(url);\n\n\tif (!urlPath.is_web) {\n\t\tconst existsDefault = urlPath.fs_exists;\n\t\t// normal file exists\n\t\tif (existsDefault) return true;\n\t\t// original (untranspiled) file exists\n\t\telse {\n\t\t\tif (urlPath.ext == \"css\") return urlPath.getWithFileExtension(\"scss\").fs_exists\n\t\t\tif (urlPath.ext == \"js\" || urlPath.ext == \"jsx\") return urlPath.getWithFileExtension(\"ts\").fs_exists || urlPath.getWithFileExtension(\"tsx\").fs_exists\n\t\t}\n\t}\n\n\tlet parent = urlPath.parent_dir;\n\tconst treeNames = [urlPath.name];\n\twhile ((!directoryIndexFiles.has(parent.toString()) || !cached) && parent.pathname) {\n\t\tif (parent.parent_dir.pathname == \"/\" || parent.parent_dir.pathname == \"/@uix/src/\") break;\n\t\ttreeNames.push(parent.name)\n\t\tparent = parent.parent_dir;\n\t}\n\n\ttry {\n\t\tlet fileTree:any;\n\t\t// get index file from cache\n\t\tif (cached && directoryIndexFiles.has(parent.toString())) fileTree = directoryIndexFiles.get(parent.toString())\n\t\t// fetch index file\n\t\telse {\n\t\t\tconst indexResponse = await fetch(parent)\n\t\t\tif (!indexResponse.ok) throw \"error\";\n\t\t\t// save in cache\n\t\t\tfileTree = await indexResponse.json();\n\t\t\tdirectoryIndexFiles.set(parent.toString(), fileTree);\n\t\t}\n\n\t\tlet name;\n\t\tlet subTree = fileTree;\n\n\t\twhile ((name = treeNames.pop())) {\n\t\t\t// find subdirectory\n\t\t\tfor (const entry of subTree) {\n\t\t\t\tif (treeNames.length == 0 && typeof entry == \"string\") {\n\t\t\t\t\tif (entry === name) return true; // cannot go deeper, but matches\n\t\t\t\t\telse {\n\t\t\t\t\t\tif (entry === name.replace(/\\.css$/, '.scss')) return true;\n\t\t\t\t\t\tif (entry === name.replace(/\\.jsx?$/, '.ts')) return true;\n\t\t\t\t\t\tif (entry === name.replace(/\\.jsx?$/, '.tsx')) return true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (entry.name == name) {\n\t\t\t\t\tif (treeNames.length == 0) return true; // cannot go deeper, but matches\n\t\t\t\t\tsubTree = entry.children;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// when this point is reached, the file does not exist\n\t\treturn false;\n\t}\n\tcatch {\n\t\tif (fallbackRequest) {\n\t\t\ttry {\n\t\t\t\treturn (await fetch(url)).ok\n\t\t\t}\n\t\t\tcatch (e) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t}\n\n\t//  fetch(path)).ok\n\treturn true;\n}"],"names":["Path","directoryIndexFiles","Map","fileExists","url","cached","fallbackRequest","urlPath","is_web","fs_exists","ext","getWithFileExtension","parent","parent_dir","treeNames","name","has","toString","pathname","push","fileTree","get","indexResponse","fetch","ok","json","set","subTree","pop","entry","length","replace","children","e"],"mappings":"AAMA,SAASA,IAAI,QAAQ,kCAAkC;AAEvD,IAAMC,sBAAsB,IAAIC;AAEhC,OAAO,eAAeC,WAAWC,GAAe,EAAE,EAACC,MAAM,EAAEC,eAAe,EAAgD,GAAG;IAACD,QAAQ,CAAA;IAAMC,iBAAiB,CAAA;AAAI,CAAC;IACjK,IAAMC,UAAUH,eAAeJ,OAAOI,MAAM,IAAIJ,KAAKI;IAErD,IAAI,CAACG,QAAQC,MAAM,EAAE;QAGpB,IAFsBD,QAAQE,SAAS,EAEpB,OAAO,CAAA;QAGzB,IAAIF,AAAe,SAAfA,QAAQG,GAAG,EAAW,OAAOH,QAAQI,oBAAoB,CAAC,QAAQF,SAAS;QAC/E,IAAIF,AAAe,QAA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