1 | "use strict";
|
2 |
|
3 | const path = require("path");
|
4 | const ts = require("typescript");
|
5 |
|
6 | const cwd = process.cwd();
|
7 | const compilerOptions = loadCompilerOptions(cwd) || {};
|
8 | const extensions = ["ts", "tsx"];
|
9 | if (compilerOptions.allowJs) {
|
10 | extensions.push("js");
|
11 | extensions.push("jsx");
|
12 | }
|
13 |
|
14 | let testDir = "test";
|
15 | const packageData = require(path.join(cwd, "package.json"));
|
16 | if (
|
17 | packageData &&
|
18 | typeof packageData.directories === "object" &&
|
19 | typeof packageData.directories.test === "string"
|
20 | ) {
|
21 | testDir = packageData.directories.test;
|
22 | }
|
23 | const pattern = path.join(testDir, `**/*.@(${extensions.join("|")})`);
|
24 |
|
25 | require("./index")({ cwd, pattern, extensions });
|
26 |
|
27 | function loadCompilerOptions(cwd) {
|
28 | const tsconfigPath = ts.findConfigFile(cwd, ts.sys.fileExists);
|
29 | if (!tsconfigPath) {
|
30 | return null;
|
31 | }
|
32 | const result = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
|
33 | if (result.error) {
|
34 | throw new Error(result.error.messageText);
|
35 | }
|
36 | if (result.config && result.config.compilerOptions) {
|
37 | const basepath = path.dirname(tsconfigPath);
|
38 | const { options } = ts.parseJsonConfigFileContent(result.config, ts.sys, basepath);
|
39 | return options;
|
40 | }
|
41 | return null;
|
42 | }
|