ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
112 lines (110 loc) • 4.34 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
class FileUtils {
constructor() {
}
/**
* Ensure the directory exists synchronously.
* @param host - File system host.
* @param dirPath - Directory path.
*/
static ensureDirectoryExistsSync(host, dirPath) {
if (host.directoryExistsSync(dirPath))
return;
// ensure the parent exists and is not the root
const parentDirPath = path.dirname(dirPath);
if (parentDirPath !== dirPath && path.dirname(parentDirPath) !== parentDirPath)
FileUtils.ensureDirectoryExistsSync(host, parentDirPath);
// make this directory
host.mkdirSync(dirPath);
}
/**
* Ensure the directory exists asynchronously.
* @param host - File system host.
* @param dirPath - Directory path.
*/
static ensureDirectoryExists(host, dirPath) {
return __awaiter(this, void 0, void 0, function* () {
if (yield host.directoryExists(dirPath))
return;
// ensure the parent exists and is not the root
const parentDirPath = path.dirname(dirPath);
if (parentDirPath !== dirPath && path.dirname(parentDirPath) !== parentDirPath)
yield FileUtils.ensureDirectoryExists(host, parentDirPath);
// make this directory
yield host.mkdir(dirPath);
});
}
/**
* 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 path.
* @param fileOrDirPath - Path to get the directory name from.
*/
static getDirPath(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));
}
/**
* Changes all back slashes to forward slashes.
* @param fileOrDirPath - Path.
*/
static standardizeSlashes(fileOrDirPath) {
return fileOrDirPath.replace(/\\/g, "/");
}
/**
* Checks if a file path matches a specified search string.
* @param filePath - File path.
* @param searchString - Search string.
*/
static filePathMatches(filePath, searchString) {
const splitBySlash = (p) => this.standardizeSlashes(p || "").replace(/^\//, "").split("/");
const filePathItems = splitBySlash(filePath);
const searchItems = splitBySlash(searchString);
if (searchItems.length > filePathItems.length)
return false;
for (let i = 0; i < searchItems.length; i++) {
if (searchItems[searchItems.length - i - 1] !== filePathItems[filePathItems.length - i - 1])
return false;
}
return searchItems.length > 0;
}
}
exports.FileUtils = FileUtils;
//# sourceMappingURL=FileUtils.js.map