definition-tester
Version:
DefinitelyTyped repository testing infrastructure
136 lines • 5.05 kB
JavaScript
;
var path = require('path');
var Lazy = require('lazy.js');
var Promise = require('bluebird');
var util = require('../util/util');
var File_1 = require('./File');
var FileIndex = (function () {
function FileIndex(options) {
this.options = options;
}
FileIndex.prototype.findFilesByName = function (nameRegexp) {
var _this = this;
return new Promise(function (resolve) {
var finish = function () {
resolve(_this.files.filter(function (f) { return nameRegexp.test(f.fileNameWithExtension); }).map(function (f) { return f.fullPath; }));
};
if (_this.files) {
finish();
}
else {
_this.readIndex().then(finish);
}
});
};
FileIndex.prototype.checkAcceptFile = function (fileName) {
return (fileName.indexOf('_infrastructure/') < 0) &&
(fileName.indexOf('node_modules/') < 0);
};
FileIndex.prototype.listIndex = function () {
return Object.keys(this.fileMap);
};
FileIndex.prototype.hasFile = function (target) {
return target in this.fileMap;
};
FileIndex.prototype.getFile = function (target) {
if (target in this.fileMap) {
return this.fileMap[target];
}
return null;
};
FileIndex.prototype.setFile = function (file) {
if (file.fullPath in this.fileMap) {
throw new Error("cannot overwrite file: " + file.fullPath);
}
this.fileMap[file.fullPath] = file;
};
FileIndex.prototype.readIndex = function () {
var _this = this;
this.fileMap = Object.create(null);
return util.glob('**/*', {
cwd: this.options.dtPath
}).then(function (fileNames) {
_this.files = Lazy(fileNames).filter(_this.checkAcceptFile).map(function (fileName) {
var file = File_1.default.fromPathAndFilename(_this.options.dtPath, fileName);
_this.fileMap[file.fullPath] = file;
return file;
}).toArray();
}).return();
};
FileIndex.prototype.collectDiff = function (changes) {
var _this = this;
return new Promise(function (resolve) {
_this.changed = Object.create(null);
_this.removed = Object.create(null);
Lazy(changes).filter(function (full) {
return _this.checkAcceptFile(full);
}).uniq().each(function (local) {
var full = path.resolve(_this.options.dtPath, local);
var file = _this.getFile(full);
if (!file) {
file = File_1.default.fromFullPath(full);
_this.setFile(file);
_this.removed[full] = file;
}
else {
_this.changed[full] = file;
}
});
resolve(null);
});
};
FileIndex.prototype.parseFiles = function () {
var _this = this;
return this.loadReferences(this.files).then(function () {
return _this.getMissingReferences();
});
};
FileIndex.prototype.getMissingReferences = function () {
var _this = this;
return Promise.attempt(function () {
_this.missing = Object.create(null);
Lazy(_this.removed).keys().each(function (removed) {
if (removed in _this.refMap) {
_this.missing[removed] = _this.refMap[removed];
}
});
});
};
FileIndex.prototype.loadReferences = function (files) {
throw new Error('nyi');
};
FileIndex.prototype.addToRefMap = function (fullPath, file) {
if (fullPath in this.refMap) {
this.refMap[fullPath].push(file);
}
else {
this.refMap[fullPath] = [file];
}
};
FileIndex.prototype.collectTargets = function () {
var _this = this;
return new Promise(function (resolve) {
var result = Object.create(null);
var queue = Lazy(_this.changed).values().toArray();
while (queue.length > 0) {
var next = queue.shift();
var fp = next.fullPath;
if (result[fp]) {
continue;
}
result[fp] = next;
if (fp in _this.refMap) {
var arr = _this.refMap[fp];
for (var i = 0, ii = arr.length; i < ii; i++) {
queue.push(arr[i]);
}
}
}
resolve(Lazy(result).values().toArray());
});
};
return FileIndex;
}());
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = FileIndex;
//# sourceMappingURL=FileIndex.js.map