alm
Version:
The best IDE for TypeScript
183 lines (182 loc) • 6.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Make typescript available globally
*/
var byots = require("byots");
var ensureImport = byots;
var sw = require("../../utils/simpleWorker");
var contract = require("./testedContract");
var utils_1 = require("../../../common/utils");
var fsu = require("../../utils/fsu");
var json_1 = require("../../../common/json");
var utils = require("../../../common/utils");
var mochaRunner = require("./runners/mochaRunner");
var testedMessagePrefix = "[TESTED]";
/**
* TIP if other workers start using this file we will move its parsing / handling
* to the main web worker.
*/
var configFileName = 'alm.json';
var Worker;
(function (Worker) {
Worker.init = function (data) {
TestedWorkerImplementation.init(data.workingDir);
TestedWorkerImplementation.restart();
return utils_1.resolve({});
};
Worker.fileSaved = function (data) {
if (data.filePath.toLowerCase().endsWith(configFileName)) {
TestedWorkerImplementation.restart();
}
if (TestedWorkerImplementation.globalState.started) {
if (TestedWorkerImplementation.globalState.testedJson.filePaths.some(function (x) { return data.filePath === x; })) {
TestedWorkerImplementation.globalState.filePathsToRun.push(data.filePath);
TestedWorkerImplementation.runNext();
}
}
return utils_1.resolve({});
};
})(Worker || (Worker = {}));
// Ensure that the namespace follows the contract
var _checkTypes = Worker;
// run worker
exports.master = sw.runWorker({
workerImplementation: Worker,
masterContract: contract.master
}).master;
var errorsCache_1 = require("../../utils/errorsCache");
var testResultsCache_1 = require("./common/testResultsCache");
var TestedWorkerImplementation;
(function (TestedWorkerImplementation) {
/** Init errors */
var errorCache = new errorsCache_1.ErrorsCache();
errorCache.errorsDelta.on(exports.master.receiveErrorCacheDelta);
/** Init test result cache */
var testResultCache = new testResultsCache_1.TestResultsCache();
testResultCache.testResultsDelta.on(exports.master.receiveTestResultsDelta);
/** Init global state */
TestedWorkerImplementation.globalState = {
started: false,
workingDir: '',
testedJson: {
filePaths: []
},
filePathsToRun: []
};
/**
* Reinit the global state + errors
*/
function init(workingDir) {
if (workingDir === void 0) { workingDir = ''; }
errorCache.clearErrors();
testResultCache.clearResults();
TestedWorkerImplementation.globalState = {
started: false,
workingDir: workingDir,
testedJson: {
filePaths: []
},
filePathsToRun: []
};
}
TestedWorkerImplementation.init = init;
function reinit() {
init(TestedWorkerImplementation.globalState.workingDir);
}
/**
* Restart if:
* - config file changes
* - working directory changes
*/
function restart() {
reinit();
var testedJsonFilePath;
try {
testedJsonFilePath = fsu.travelUpTheDirectoryTreeTillYouFind(TestedWorkerImplementation.globalState.workingDir, configFileName);
}
catch (err) {
// Leave disabled
return;
}
// Validate configFile
var parsed = json_1.parse(fsu.readFile(testedJsonFilePath));
if (parsed.error) {
errorCache.setErrorsByFilePaths([testedJsonFilePath], [json_1.parseErrorToCodeError(testedJsonFilePath, parsed.error, 'tested')]);
return;
}
var rawData = parsed.data;
/** This config file may or may not be valid. But its definitely not for `testing` */
if (!rawData.tests) {
return;
}
/** Sanitize raw data */
rawData.tests.include = rawData.tests.include || ["./**/*.ts", "./**/*.tsx"],
rawData.tests.exclude = rawData.tests.exclude || ["node_modules"];
/** Expand the filePaths */
var filePaths = expandIncludeExclude(utils.getDirectory(testedJsonFilePath), rawData.tests);
/** Good to go */
TestedWorkerImplementation.globalState.started = true;
TestedWorkerImplementation.globalState.testedJson = {
filePaths: filePaths
};
console.log(testedMessagePrefix, "File count:", filePaths.length);
/** Kick off */
TestedWorkerImplementation.globalState.filePathsToRun = filePaths.map(function (x) { return x; });
runNext();
}
TestedWorkerImplementation.restart = restart;
var runningSomeTest = false;
function runNext() {
if (runningSomeTest)
return;
if (TestedWorkerImplementation.globalState.filePathsToRun.length) {
var next = TestedWorkerImplementation.globalState.filePathsToRun.shift();
TestedWorkerImplementation.globalState.filePathsToRun = utils.uniq(TestedWorkerImplementation.globalState.filePathsToRun);
runningSomeTest = true;
setWorking();
mochaRunner.runTest(next).then(function (res) {
/**
* Add result
*/
testResultCache.addResult(res);
/** No longer running */
runningSomeTest = false;
/** Try and run the next one if any */
runNext();
});
}
else {
clearWorking();
}
}
TestedWorkerImplementation.runNext = runNext;
})(TestedWorkerImplementation || (TestedWorkerImplementation = {}));
/**
* Debounced to match the test delta sending debounce.
*/
var _isWorking = false;
var clearWorking = utils.debounce(function () {
_isWorking = false;
exports.master.receiveWorking({ working: false });
console.log(testedMessagePrefix, "Completed");
}, 1000);
var setWorking = function () {
if (!_isWorking) {
exports.master.receiveWorking({ working: true });
_isWorking = true;
console.log(testedMessagePrefix, "Testing");
}
};
/** Utility: include / exclude expansion */
function expandIncludeExclude(rootDir, cfg) {
var tsResult = ts.parseJsonConfigFileContent({
compilerOptions: {
allowJs: true
},
include: cfg.include,
exclude: cfg.exclude
}, ts.sys, rootDir);
// console.log(tsResult); // DEBUG
return tsResult.fileNames || [];
}