1 | import chalk from "chalk";
|
2 |
|
3 | import { ExecutionMode, getExecutionMode } from "./execution-mode";
|
4 |
|
5 | let cachedIsTypescriptSupported: boolean | undefined;
|
6 |
|
7 | export function isTypescriptSupported() {
|
8 | if (cachedIsTypescriptSupported === undefined) {
|
9 | const executionMode = getExecutionMode();
|
10 | if (executionMode === ExecutionMode.EXECUTION_MODE_GLOBAL_INSTALLATION) {
|
11 | cachedIsTypescriptSupported = false;
|
12 | } else if (
|
13 | executionMode === ExecutionMode.EXECUTION_MODE_LOCAL_INSTALLATION
|
14 | ) {
|
15 | try {
|
16 |
|
17 | require.resolve("typescript");
|
18 | require.resolve("ts-node");
|
19 | cachedIsTypescriptSupported = true;
|
20 | } catch {
|
21 | cachedIsTypescriptSupported = false;
|
22 | }
|
23 | } else {
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | cachedIsTypescriptSupported = true;
|
32 | }
|
33 | }
|
34 |
|
35 | return cachedIsTypescriptSupported;
|
36 | }
|
37 |
|
38 | export function loadTsNodeIfPresent() {
|
39 | if (isTypescriptSupported()) {
|
40 |
|
41 | if (process.env.TS_NODE_FILES === undefined) {
|
42 | process.env.TS_NODE_FILES = "true";
|
43 | }
|
44 |
|
45 | try {
|
46 |
|
47 | require("ts-node/register");
|
48 | } catch (error) {
|
49 |
|
50 | if (error.message.includes("Cannot find module 'typescript'")) {
|
51 | console.warn(
|
52 | chalk.yellow(
|
53 | "Failed to load TypeScript support. Please update ts-node."
|
54 | )
|
55 | );
|
56 |
|
57 | return;
|
58 | }
|
59 |
|
60 |
|
61 | throw error;
|
62 | }
|
63 | }
|
64 | }
|