ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
150 lines (149 loc) • 6.38 kB
JavaScript
"use strict";
var __values = (this && this.__values)/* istanbul ignore next */ || function (o) {
var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
if (m) return m.call(o);
return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
};
Object.defineProperty(exports, "__esModule", { value: true });
var errors = require("./../errors");
var utils_1 = require("./../utils");
var minimatch_1 = require("minimatch");
var VirtualFileSystemHost = /** @class */ (function () {
function VirtualFileSystemHost() {
this.files = new utils_1.KeyValueCache();
this.directories = utils_1.createHashSet();
this.directories.add("/");
}
VirtualFileSystemHost.prototype.delete = function (path) {
this.deleteSync(path);
return Promise.resolve();
};
VirtualFileSystemHost.prototype.deleteSync = function (path) {
path = utils_1.FileUtils.getStandardizedAbsolutePath(this, path);
if (this.directories.has(path)) {
try {
for (var _a = __values(utils_1.ArrayUtils.from(this.files.getKeys())), _b = _a.next(); !_b.done; _b = _a.next()) {
var filePath = _b.value;
if (utils_1.StringUtils.startsWith(filePath, path))
this.files.removeByKey(filePath);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_1) throw e_1.error; }
}
try {
for (var _d = __values(utils_1.ArrayUtils.from(this.directories.values())), _e = _d.next(); !_e.done; _e = _d.next()) {
var directoryPath = _e.value;
if (utils_1.StringUtils.startsWith(directoryPath, path))
this.directories.delete(directoryPath);
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_e && !_e.done && (_f = _d.return)) _f.call(_d);
}
finally { if (e_2) throw e_2.error; }
}
}
else
this.files.removeByKey(path);
var e_1, _c, e_2, _f;
};
VirtualFileSystemHost.prototype.readFile = function (filePath, encoding) {
if (encoding === void 0) { encoding = "utf-8"; }
try {
return Promise.resolve(this.readFileSync(filePath, encoding));
}
catch (err) {
return Promise.reject(err);
}
};
VirtualFileSystemHost.prototype.readFileSync = function (filePath, encoding) {
if (encoding === void 0) { encoding = "utf-8"; }
filePath = utils_1.FileUtils.getStandardizedAbsolutePath(this, filePath);
var fileText = this.files.get(filePath);
if (fileText == null)
throw new errors.FileNotFoundError(filePath);
return fileText;
};
VirtualFileSystemHost.prototype.writeFile = function (filePath, fileText) {
this.writeFileSync(filePath, fileText);
return Promise.resolve();
};
VirtualFileSystemHost.prototype.writeFileSync = function (filePath, fileText) {
filePath = utils_1.FileUtils.getStandardizedAbsolutePath(this, filePath);
utils_1.FileUtils.ensureDirectoryExistsSync(this, utils_1.FileUtils.getDirPath(filePath));
this.files.set(filePath, fileText);
};
VirtualFileSystemHost.prototype.mkdir = function (dirPath) {
this.mkdirSync(dirPath);
return Promise.resolve();
};
VirtualFileSystemHost.prototype.mkdirSync = function (dirPath) {
dirPath = utils_1.FileUtils.getStandardizedAbsolutePath(this, dirPath);
if (dirPath !== utils_1.FileUtils.getDirPath(dirPath))
utils_1.FileUtils.ensureDirectoryExistsSync(this, utils_1.FileUtils.getDirPath(dirPath));
this.directories.add(dirPath);
};
VirtualFileSystemHost.prototype.fileExists = function (filePath) {
return Promise.resolve(this.fileExistsSync(filePath));
};
VirtualFileSystemHost.prototype.fileExistsSync = function (filePath) {
filePath = utils_1.FileUtils.getStandardizedAbsolutePath(this, filePath);
return this.files.has(filePath);
};
VirtualFileSystemHost.prototype.directoryExists = function (dirPath) {
return Promise.resolve(this.directoryExistsSync(dirPath));
};
VirtualFileSystemHost.prototype.directoryExistsSync = function (dirPath) {
dirPath = utils_1.FileUtils.getStandardizedAbsolutePath(this, dirPath);
return this.directories.has(dirPath);
};
VirtualFileSystemHost.prototype.getCurrentDirectory = function () {
return "/";
};
VirtualFileSystemHost.prototype.glob = function (patterns) {
var filePaths = [];
try {
for (var patterns_1 = __values(patterns), patterns_1_1 = patterns_1.next(); !patterns_1_1.done; patterns_1_1 = patterns_1.next()) {
var pattern = patterns_1_1.value;
var mm = new minimatch_1.Minimatch(pattern, { matchBase: true });
try {
for (var _a = __values(this.files.getKeys()), _b = _a.next(); !_b.done; _b = _a.next()) {
var filePath = _b.value;
if (mm.match(filePath))
filePaths.push(filePath);
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_3) throw e_3.error; }
}
}
}
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {
try {
if (patterns_1_1 && !patterns_1_1.done && (_d = patterns_1.return)) _d.call(patterns_1);
}
finally { if (e_4) throw e_4.error; }
}
return filePaths;
var e_4, _d, e_3, _c;
};
return VirtualFileSystemHost;
}());
exports.VirtualFileSystemHost = VirtualFileSystemHost;