UNPKG

@sleekify/sleekify

Version:

A TypeScript decorator driven approach for developing web applications.

71 lines (70 loc) 2.92 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PathUtil = void 0; const glob_1 = require("glob"); const node_path_1 = __importDefault(require("node:path")); class PathUtil { static filenameToDirectory(filename) { const separatorIndex = filename.lastIndexOf(node_path_1.default.sep); if (separatorIndex < 0) { throw new Error(`The filename path of '${filename}' is invalid`); } return filename.substring(0, separatorIndex); } static getCallerFilename() { const stack = new Error().stack; if (stack == null) { throw new Error('Cannot determine the caller filename when the error stack is missing'); } const line = stack.split('\n')[3] ?? ''; const startIndex = line.indexOf('(') + 1; const endIndex = line.search(/:\d+:\d+\)$/); if (startIndex <= 0 || endIndex < 0 || startIndex > endIndex) { throw new Error('Cannot determine the caller filename when the error stack is malformed'); } return line.substring(startIndex, endIndex); } static getCommonBasePath(path1, path2) { const minLength = Math.min(path1.length, path2.length); let charIndex = 0; for (; charIndex < minLength; charIndex++) { if (path1.charAt(charIndex) !== path2.charAt(charIndex)) { break; } } const matchingPrefix = path1.substring(0, charIndex); const pathSepIndex = matchingPrefix.lastIndexOf(node_path_1.default.sep); const commonBasePath = pathSepIndex >= 0 ? matchingPrefix.substring(0, pathSepIndex + 1) : matchingPrefix; return commonBasePath; } static async getModulePaths(globPath, options) { let modulePaths; if (options?.isTypeScript === true) { if (options?.hasTypesScriptOutDir) { modulePaths = await (0, glob_1.glob)(`${globPath}*(*.js|*.ts)`); } else { const tsModulePaths = await (0, glob_1.glob)(`${globPath}*.ts`); const jsModulePaths = await (0, glob_1.glob)(`${globPath}*.js`); const pathMap = {}; for (const tsPath of tsModulePaths) { pathMap[tsPath.slice(0, -3)] = true; } modulePaths = [...tsModulePaths]; for (const jsPath of jsModulePaths) { if (!pathMap[jsPath.slice(0, -3)]) { modulePaths.push(jsPath); } } } } else { modulePaths = await (0, glob_1.glob)(`${globPath}*.js`); } return modulePaths.sort(); } } exports.PathUtil = PathUtil;