|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const path = require("path"); |
| 4 | + |
| 5 | +/** |
| 6 | + * @param {string} path path |
| 7 | + * @returns {string} subPath |
| 8 | + */ |
| 9 | +const getSubPath = (path) => { |
| 10 | + let subPath = ""; |
| 11 | + const lastSlash = path.lastIndexOf("/"); |
| 12 | + let firstSlash = path.indexOf("/"); |
| 13 | + if (lastSlash !== -1 && firstSlash !== lastSlash) { |
| 14 | + if (firstSlash !== -1) { |
| 15 | + let next = path.indexOf("/", firstSlash + 1); |
| 16 | + let dir = path.slice(firstSlash + 1, next); |
| 17 | + |
| 18 | + while (dir === ".") { |
| 19 | + firstSlash = next; |
| 20 | + next = path.indexOf("/", firstSlash + 1); |
| 21 | + dir = path.slice(firstSlash + 1, next); |
| 22 | + } |
| 23 | + } |
| 24 | + subPath = path.slice(firstSlash + 1, lastSlash + 1); |
| 25 | + } |
| 26 | + return subPath; |
| 27 | +}; |
| 28 | + |
| 29 | +/** |
| 30 | + * @param {string} path path |
| 31 | + * @returns {boolean} whether path is a relative path |
| 32 | + */ |
| 33 | +const isRelativePath = (path) => /^\.\.?\//.test(path); |
| 34 | + |
| 35 | +/** |
| 36 | + * @param {string} url url |
| 37 | + * @param {string} outputDirectory outputDirectory |
| 38 | + * @returns {string} absolute path |
| 39 | + */ |
| 40 | +const urlToPath = (url, outputDirectory) => { |
| 41 | + if (url.startsWith("https://test.cases/path/")) url = url.slice(24); |
| 42 | + else if (url.startsWith("https://test.cases/")) url = url.slice(19); |
| 43 | + return path.resolve(outputDirectory, `./${url}`); |
| 44 | +}; |
| 45 | + |
| 46 | +/** |
| 47 | + * @param {string} url url |
| 48 | + * @returns {string} relative path |
| 49 | + */ |
| 50 | +const urlToRelativePath = (url) => { |
| 51 | + if (url.startsWith("https://test.cases/path/")) url = url.slice(24); |
| 52 | + else if (url.startsWith("https://test.cases/")) url = url.slice(19); |
| 53 | + return `./${url}`; |
| 54 | +}; |
| 55 | + |
| 56 | +/** |
| 57 | + * @returns {number[]} version arr |
| 58 | + */ |
| 59 | +const getNodeVersion = () => process.versions.node.split(".").map(Number); |
| 60 | + |
| 61 | +const ESModuleStatus = Object.freeze({ |
| 62 | + Unlinked: "unlinked", |
| 63 | + Linked: "linked", |
| 64 | + Evaluated: "evaluated", |
| 65 | + /** |
| 66 | + * Present in `module.linkingStatus` |
| 67 | + * Compatible with Node.js v10 |
| 68 | + * https://nodejs.org/docs/latest-v10.x/api/vm.html#vm_module_status |
| 69 | + */ |
| 70 | + Uninstantiated: "uninstantiated" |
| 71 | +}); |
| 72 | + |
| 73 | +module.exports = { |
| 74 | + ESModuleStatus, |
| 75 | + getNodeVersion, |
| 76 | + getSubPath, |
| 77 | + isRelativePath, |
| 78 | + urlToPath, |
| 79 | + urlToRelativePath |
| 80 | +}; |
0 commit comments