95 lines
14 KiB
JavaScript
95 lines
14 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
const lodash_1 = require("lodash");
|
||
|
const strip_bom_1 = __importDefault(require("strip-bom"));
|
||
|
const promisified_functions_1 = require("../utils/promisified-functions");
|
||
|
const runtime_1 = require("../errors/runtime");
|
||
|
const types_1 = require("../errors/types");
|
||
|
const compilers_1 = require("./compilers");
|
||
|
const SOURCE_CHUNK_LENGTH = 1000;
|
||
|
class Compiler {
|
||
|
constructor(sources, compilerOptions, { isCompilerServiceMode, baseUrl, experimentalEsm } = {}) {
|
||
|
this.sources = sources;
|
||
|
this.experimentalEsm = experimentalEsm;
|
||
|
(0, compilers_1.initTestFileCompilers)(compilerOptions, { isCompilerServiceMode, baseUrl, experimentalEsm });
|
||
|
}
|
||
|
static getSupportedTestFileExtensions() {
|
||
|
return (0, lodash_1.uniq)((0, lodash_1.flattenDeep)((0, compilers_1.getTestFileCompilers)().map(compiler => compiler.getSupportedExtension())));
|
||
|
}
|
||
|
static async createTestFileInfo(filename) {
|
||
|
let code = null;
|
||
|
try {
|
||
|
code = await (0, promisified_functions_1.readFile)(filename);
|
||
|
}
|
||
|
catch (err) {
|
||
|
throw new runtime_1.GeneralError(types_1.RUNTIME_ERRORS.cannotFindSpecifiedTestSource, filename);
|
||
|
}
|
||
|
code = (0, strip_bom_1.default)(code).toString();
|
||
|
const compiler = (0, lodash_1.find)((0, compilers_1.getTestFileCompilers)(), someCompiler => someCompiler.canCompile(code, filename));
|
||
|
if (!compiler)
|
||
|
return null;
|
||
|
return {
|
||
|
filename,
|
||
|
code,
|
||
|
compiler,
|
||
|
compiledCode: null,
|
||
|
};
|
||
|
}
|
||
|
async _createTestFilesInfo(filenames) {
|
||
|
const testFilesInfo = await Promise.all(filenames.map(filename => Compiler.createTestFileInfo(filename)));
|
||
|
return testFilesInfo.filter(info => !!info);
|
||
|
}
|
||
|
async _precompileFiles(compiler, testFilesInfo) {
|
||
|
if (!compiler.canPrecompile || this.experimentalEsm && compiler.canCompileInEsm)
|
||
|
return;
|
||
|
const precompiledCode = await compiler.precompile(testFilesInfo);
|
||
|
for (let i = 0; i < testFilesInfo.length; i++)
|
||
|
testFilesInfo[i].compiledCode = precompiledCode[i];
|
||
|
}
|
||
|
_getCompilerTasks(testFilesInfo) {
|
||
|
const tasks = new WeakMap();
|
||
|
const compilers = [];
|
||
|
for (const info of testFilesInfo) {
|
||
|
const { compiler } = info;
|
||
|
if (!tasks.has(compiler)) {
|
||
|
compilers.push(compiler);
|
||
|
tasks.set(compiler, []);
|
||
|
}
|
||
|
tasks.get(info.compiler).push(info);
|
||
|
}
|
||
|
return compilers.map(compiler => ({ compiler, compilerTestFilesInfo: tasks.get(compiler) }));
|
||
|
}
|
||
|
async _getTests({ compiler, filename, code, compiledCode }) {
|
||
|
if (compiledCode || this.experimentalEsm && compiler.canCompileInEsm)
|
||
|
return await compiler.execute(compiledCode, filename);
|
||
|
return await compiler.compile(code, filename);
|
||
|
}
|
||
|
async _compileTestFiles(filenames) {
|
||
|
const testFilesInfo = await this._createTestFilesInfo(filenames);
|
||
|
const compilerTasks = this._getCompilerTasks(testFilesInfo);
|
||
|
await Promise.all(compilerTasks.map(({ compiler, compilerTestFilesInfo }) => this._precompileFiles(compiler, compilerTestFilesInfo)));
|
||
|
const tests = [];
|
||
|
for (const info of testFilesInfo)
|
||
|
tests.push(await this._getTests(info));
|
||
|
return tests;
|
||
|
}
|
||
|
async getTests() {
|
||
|
// NOTE: split sources into chunks because the fs module can't read all files
|
||
|
// simultaneously if the number of them is too large (several thousands).
|
||
|
const sourceChunks = (0, lodash_1.chunk)(this.sources, SOURCE_CHUNK_LENGTH);
|
||
|
let tests = [];
|
||
|
while (sourceChunks.length)
|
||
|
tests = tests.concat(await this._compileTestFiles(sourceChunks.shift()));
|
||
|
Compiler.cleanUp();
|
||
|
return (0, lodash_1.flattenDeep)(tests).filter(test => !!test);
|
||
|
}
|
||
|
static cleanUp() {
|
||
|
(0, compilers_1.getTestFileCompilers)().forEach(compiler => compiler.cleanUp());
|
||
|
}
|
||
|
}
|
||
|
exports.default = Compiler;
|
||
|
module.exports = exports.default;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvY29tcGlsZXIvaW5kZXguanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7QUFBQSxtQ0FLZ0I7QUFFaEIsMERBQWlDO0FBQ2pDLDBFQUEwRDtBQUMxRCwrQ0FBaUQ7QUFDakQsMkNBQWlEO0FBQ2pELDJDQUEwRTtBQUcxRSxNQUFNLG1CQUFtQixHQUFHLElBQUksQ0FBQztBQUVqQyxNQUFxQixRQUFRO0lBQ3pCLFlBQWEsT0FBTyxFQUFFLGVBQWUsRUFBRSxFQUFFLHFCQUFxQixFQUFFLE9BQU8sRUFBRSxlQUFlLEVBQUUsR0FBRyxFQUFFO1FBQzNGLElBQUksQ0FBQyxPQUFPLEdBQUcsT0FBTyxDQUFDO1FBQ3ZCLElBQUksQ0FBQyxlQUFlLEdBQUcsZUFBZSxDQUFDO1FBRXZDLElBQUEsaUNBQXFCLEVBQUMsZUFBZSxFQUFFLEVBQUUscUJBQXFCLEVBQUUsT0FBTyxFQUFFLGVBQWUsRUFBRSxDQUFDLENBQUM7SUFDaEcsQ0FBQztJQUVELE1BQU0sQ0FBQyw4QkFBOEI7UUFDakMsT0FBTyxJQUFBLGFBQUksRUFBQyxJQUFBLG9CQUFXLEVBQUMsSUFBQSxnQ0FBb0IsR0FBRSxDQUFDLEdBQUcsQ0FBQyxRQUFRLENBQUMsRUFBRSxDQUFDLFFBQVEsQ0FBQyxxQkFBcUIsRUFBRSxDQUFDLENBQUMsQ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