"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var fs_1 = require("fs"); var micromatch = require("micromatch"); var path_1 = require("path"); var _ts = require("typescript"); var constants_1 = require("../constants"); var sha1_1 = require("../util/sha1"); exports.hasOwn = Object.prototype.hasOwnProperty; function getResolvedModulesCache(cachedir) { return path_1.join(cachedir, sha1_1.sha1('ts-jest-resolved-modules', '\x00')); } exports.getResolvedModulesCache = getResolvedModulesCache; function cacheResolvedModules(fileName, fileContent, memoryCache, program, cacheDir, logger) { var importReferences = program.getSourceFile(fileName).imports; if (importReferences.length) { logger.debug({ fileName: fileName }, "cacheResolvedModules(): get resolved modules of test file " + fileName); memoryCache.resolvedModules[fileName] = Object.create(null); memoryCache.resolvedModules[fileName].modulePaths = importReferences .filter(function (importReference) { var _a; return (_a = importReference.parent.parent.resolvedModules) === null || _a === void 0 ? void 0 : _a.get(importReference.text); }) .map(function (importReference) { return path_1.normalize(importReference.parent.parent.resolvedModules.get(importReference.text) .resolvedFileName); }) .reduce(function (a, b) { return a.concat(b); }, []); memoryCache.resolvedModules[fileName].testFileContent = fileContent; fs_1.writeFileSync(getResolvedModulesCache(cacheDir), JSON.stringify(memoryCache.resolvedModules)); } } exports.cacheResolvedModules = cacheResolvedModules; function isTestFile(testMatchPatterns, fileName) { return testMatchPatterns.some(function (pattern) { return typeof pattern === 'string' ? micromatch.isMatch(fileName, pattern) : pattern.test(fileName); }); } exports.isTestFile = isTestFile; function isUsingProjectReferences(program, projectReferences) { if (projectReferences && !!program.getProjectReferences) { return Boolean(program && program.getProjectReferences()); } return false; } function getResolvedProjectReferences(program) { var getProjectReferences = program.getResolvedProjectReferences || program.getProjectReferences; if (getProjectReferences) { return getProjectReferences(); } return; } function getProjectReferenceForFile(filePath, program, projectReferences) { if (isUsingProjectReferences(program, projectReferences)) { return (program && getResolvedProjectReferences(program).find(function (ref) { return (ref && ref.commandLine.fileNames.some(function (file) { return path_1.normalize(file) === filePath; })) || false; })); } return; } function getAndCacheProjectReference(filePath, program, files, projectReferences) { var file = files.get(filePath); if (file !== undefined && file.projectReference) { return file.projectReference.project; } var projectReference = getProjectReferenceForFile(filePath, program, projectReferences); if (file !== undefined) { file.projectReference = { project: projectReference }; } return projectReference; } exports.getAndCacheProjectReference = getAndCacheProjectReference; function getOutputJavaScriptFileName(inputFileName, projectReference) { var options = projectReference.commandLine.options; var projectDirectory = options.rootDir || path_1.dirname(projectReference.sourceFile.fileName); var relativePath = path_1.relative(projectDirectory, inputFileName); var outputPath = path_1.resolve(options.outDir || projectDirectory, relativePath); var newExtension = constants_1.JSON_REGEX.test(inputFileName) ? '.json' : constants_1.TS_TSX_REGEX.test(inputFileName) && options.jsx === _ts.JsxEmit.Preserve ? '.jsx' : '.js'; return outputPath.replace(constants_1.EXTENSION_REGEX, newExtension); } function getAndCacheOutputJSFileName(inputFileName, projectReference, files) { var file = files.get(inputFileName); if (file && file.projectReference && file.projectReference.outputFileName) { return file.projectReference.outputFileName; } var outputFileName = getOutputJavaScriptFileName(inputFileName, projectReference); if (file !== undefined) { file.projectReference = file.projectReference || { project: projectReference, }; file.projectReference.outputFileName = outputFileName; } return outputFileName; } exports.getAndCacheOutputJSFileName = getAndCacheOutputJSFileName;