1 | import debug from "debug";
|
2 | import findUp from "find-up";
|
3 | import path from "path";
|
4 |
|
5 | const log = debug("buidler:core:execution-mode");
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | export enum ExecutionMode {
|
14 | EXECUTION_MODE_TS_NODE_TESTS,
|
15 | EXECUTION_MODE_LINKED,
|
16 | EXECUTION_MODE_GLOBAL_INSTALLATION,
|
17 | EXECUTION_MODE_LOCAL_INSTALLATION,
|
18 | }
|
19 |
|
20 | const workingDirectoryOnLoad = process.cwd();
|
21 |
|
22 | export function getExecutionMode(): ExecutionMode {
|
23 | const isInstalled = __filename.includes("node_modules");
|
24 |
|
25 | if (!isInstalled) {
|
26 |
|
27 |
|
28 | if (__dirname.startsWith(workingDirectoryOnLoad)) {
|
29 | return ExecutionMode.EXECUTION_MODE_TS_NODE_TESTS;
|
30 | }
|
31 |
|
32 | return ExecutionMode.EXECUTION_MODE_LINKED;
|
33 | }
|
34 |
|
35 | try {
|
36 | if (require("is-installed-globally")) {
|
37 | return ExecutionMode.EXECUTION_MODE_GLOBAL_INSTALLATION;
|
38 | }
|
39 | } catch (error) {
|
40 | log(
|
41 | "Failed to load is-installed-globally. Using alternative local installation detection\n",
|
42 | error
|
43 | );
|
44 |
|
45 | if (!alternativeIsLocalInstallation()) {
|
46 | return ExecutionMode.EXECUTION_MODE_GLOBAL_INSTALLATION;
|
47 | }
|
48 | }
|
49 |
|
50 | return ExecutionMode.EXECUTION_MODE_LOCAL_INSTALLATION;
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | export function isLocalDev(): boolean {
|
57 | const executionMode = getExecutionMode();
|
58 |
|
59 | return (
|
60 | executionMode === ExecutionMode.EXECUTION_MODE_LINKED ||
|
61 | executionMode === ExecutionMode.EXECUTION_MODE_TS_NODE_TESTS
|
62 | );
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | function alternativeIsLocalInstallation(): boolean {
|
74 | let cwd = workingDirectoryOnLoad;
|
75 |
|
76 | while (true) {
|
77 | const nodeModules = findUp.sync("node_modules", { cwd });
|
78 |
|
79 | if (nodeModules === null) {
|
80 | return false;
|
81 | }
|
82 |
|
83 | if (__dirname.startsWith(nodeModules)) {
|
84 | return true;
|
85 | }
|
86 |
|
87 | cwd = path.join(nodeModules, "..", "..");
|
88 | }
|
89 | }
|