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