UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

63 lines (61 loc) 2.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const path = require("path"); class FileUtils { constructor() { } /** * Gets the current directory. */ static getCurrentDirectory() { return this.getStandardizedAbsolutePath(path.resolve()); } /** * Joins the paths. * @param paths - Paths to join. */ static pathJoin(...paths) { return path.join(...paths); } /** * Gets the standardized absolute path. * @param fileOrDirPath - Path to standardize. */ static getStandardizedAbsolutePath(fileOrDirPath) { return this.standardizeSlashes(path.normalize(path.resolve(fileOrDirPath))); } /** * Gets the directory name. * @param fileOrDirPath - Path to get the directory name from. */ static getDirName(fileOrDirPath) { return path.dirname(fileOrDirPath); } /** * Gets the absolute path when absolute, otherwise gets the relative path from the base dir. * @param filePath - File path. * @param baseDir - Base dir to use when file path is relative. */ static getAbsoluteOrRelativePathFromPath(filePath, baseDir) { if (path.isAbsolute(filePath)) return FileUtils.getStandardizedAbsolutePath(filePath); return FileUtils.getStandardizedAbsolutePath(path.join(baseDir, filePath)); } static standardizeSlashes(fileName) { return fileName.replace(/\\/g, "/"); } static filePathMatches(fileName, searchString) { const splitBySlash = (p) => this.standardizeSlashes(p || "").replace(/^\//, "").split("/"); const fileNameItems = splitBySlash(fileName); const searchItems = splitBySlash(searchString); if (searchItems.length > fileNameItems.length) return false; for (let i = 0; i < searchItems.length; i++) { if (searchItems[searchItems.length - i - 1] !== fileNameItems[fileNameItems.length - i - 1]) return false; } return searchItems.length > 0; } } exports.FileUtils = FileUtils; //# sourceMappingURL=FileUtils.js.map