UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for static analysis and code manipulation.

95 lines (94 loc) 4.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var errors = require("../errors"); var utils_1 = require("../utils"); /** * Contains common methods between Project and Directory. * * I'll definitely need to refactor this in the future... just putting these methods in a common place for now. */ var DirectoryCoordinator = /** @class */ (function () { function DirectoryCoordinator(compilerFactory, fileSystemWrapper) { this.compilerFactory = compilerFactory; this.fileSystemWrapper = fileSystemWrapper; } DirectoryCoordinator.prototype.addExistingDirectoryIfExists = function (dirPath, options) { var e_1, _a; var directory = this.compilerFactory.getDirectoryFromPath(dirPath, options); if (directory == null) return undefined; if (options.recursive) { try { for (var _b = tslib_1.__values(utils_1.FileUtils.getDescendantDirectories(this.fileSystemWrapper, dirPath)), _c = _b.next(); !_c.done; _c = _b.next()) { var descendantDirPath = _c.value; this.compilerFactory.createDirectoryOrAddIfExists(descendantDirPath, options); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } } return directory; }; DirectoryCoordinator.prototype.addExistingDirectory = function (dirPath, options) { var directory = this.addExistingDirectoryIfExists(dirPath, options); if (directory == null) throw new errors.DirectoryNotFoundError(dirPath); return directory; }; DirectoryCoordinator.prototype.createDirectoryOrAddIfExists = function (dirPath, options) { return this.compilerFactory.createDirectoryOrAddIfExists(dirPath, options); }; DirectoryCoordinator.prototype.addExistingSourceFileIfExists = function (filePath, options) { return this.compilerFactory.addOrGetSourceFileFromFilePath(filePath, options); }; DirectoryCoordinator.prototype.addExistingSourceFile = function (filePath, options) { var sourceFile = this.addExistingSourceFileIfExists(filePath, options); if (sourceFile == null) throw new errors.FileNotFoundError(this.fileSystemWrapper.getStandardizedAbsolutePath(filePath)); return sourceFile; }; DirectoryCoordinator.prototype.addExistingSourceFiles = function (fileGlobs, options) { var e_2, _a, e_3, _b; if (typeof fileGlobs === "string") fileGlobs = [fileGlobs]; var sourceFiles = []; var globbedDirectories = utils_1.FileUtils.getParentMostPaths(fileGlobs.filter(function (g) { return !utils_1.FileUtils.isNegatedGlob(g); }).map(function (g) { return utils_1.FileUtils.getGlobDir(g); })); try { for (var _c = tslib_1.__values(this.fileSystemWrapper.glob(fileGlobs)), _d = _c.next(); !_d.done; _d = _c.next()) { var filePath = _d.value; var sourceFile = this.addExistingSourceFileIfExists(filePath, options); if (sourceFile != null) sourceFiles.push(sourceFile); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_d && !_d.done && (_a = _c.return)) _a.call(_c); } finally { if (e_2) throw e_2.error; } } try { for (var globbedDirectories_1 = tslib_1.__values(globbedDirectories), globbedDirectories_1_1 = globbedDirectories_1.next(); !globbedDirectories_1_1.done; globbedDirectories_1_1 = globbedDirectories_1.next()) { var dirPath = globbedDirectories_1_1.value; this.addExistingDirectoryIfExists(dirPath, { recursive: true, markInProject: options.markInProject }); } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (globbedDirectories_1_1 && !globbedDirectories_1_1.done && (_b = globbedDirectories_1.return)) _b.call(globbedDirectories_1); } finally { if (e_3) throw e_3.error; } } return sourceFiles; }; return DirectoryCoordinator; }()); exports.DirectoryCoordinator = DirectoryCoordinator;