-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
global-dir.ts
146 lines (120 loc) · 3.96 KB
/
global-dir.ts
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import type envPathsT from "env-paths";
import debug from "debug";
import fs from "fs-extra";
import os from "os";
import path from "path";
const log = debug("hardhat:core:global-dir");
async function generatePaths(packageName = "hardhat") {
const { default: envPaths } = await import("env-paths");
return envPaths(packageName);
}
function generatePathsSync(packageName = "hardhat") {
const envPaths: typeof envPathsT = require("env-paths");
return envPaths(packageName);
}
function getConfigDirSync(): string {
const { config } = generatePathsSync();
fs.ensureDirSync(config);
return config;
}
async function getDataDir(packageName?: string): Promise<string> {
const { data } = await generatePaths(packageName);
await fs.ensureDir(data);
return data;
}
export async function getCacheDir(): Promise<string> {
const { cache } = await generatePaths();
await fs.ensureDir(cache);
return cache;
}
export async function readAnalyticsId() {
const globalDataDir = await getDataDir();
const idFile = path.join(globalDataDir, "analytics.json");
return readId(idFile);
}
/**
* This is the first way that the analytics id was saved.
*/
export function readFirstLegacyAnalyticsId() {
const oldIdFile = path.join(os.homedir(), ".buidler", "config.json");
return readId(oldIdFile);
}
/**
* This is the same way the analytics id is saved now, but using buidler as the
* name of the project for env-paths
*/
export async function readSecondLegacyAnalyticsId() {
const globalDataDir = await getDataDir("buidler");
const idFile = path.join(globalDataDir, "analytics.json");
return readId(idFile);
}
async function readId(idFile: string): Promise<string | undefined> {
log(`Looking up Client Id at ${idFile}`);
let clientId: string;
try {
const data = await fs.readJSON(idFile, { encoding: "utf8" });
clientId = data.analytics.clientId;
} catch (error) {
return undefined;
}
log(`Client Id found: ${clientId}`);
return clientId;
}
export async function writeAnalyticsId(clientId: string) {
const globalDataDir = await getDataDir();
const idFile = path.join(globalDataDir, "analytics.json");
await fs.writeJSON(
idFile,
{
analytics: {
clientId,
},
},
{ encoding: "utf-8", spaces: 2 }
);
log(`Stored clientId ${clientId}`);
}
export async function getCompilersDir() {
const cache = await getCacheDir();
// Note: we introduce `-v2` to invalidate all the previous compilers at once
const compilersCache = path.join(cache, "compilers-v2");
await fs.ensureDir(compilersCache);
return compilersCache;
}
/**
* Checks if the user has given (or refused) consent for telemetry.
*
* Returns undefined if it can't be determined.
*/
export function hasConsentedTelemetry(): boolean | undefined {
const configDir = getConfigDirSync();
const telemetryConsentPath = path.join(configDir, "telemetry-consent.json");
const fileExists = fs.pathExistsSync(telemetryConsentPath);
if (!fileExists) {
return undefined;
}
const { consent } = fs.readJSONSync(telemetryConsentPath);
return consent;
}
export function writeTelemetryConsent(consent: boolean) {
const configDir = getConfigDirSync();
const telemetryConsentPath = path.join(configDir, "telemetry-consent.json");
fs.writeJSONSync(telemetryConsentPath, { consent }, { spaces: 2 });
}
/**
* Checks if we have already prompted the user to install the Hardhat for VSCode extension.
*/
export function hasPromptedForHHVSCode(): boolean {
const configDir = getConfigDirSync();
const extensionPromptedPath = path.join(configDir, "extension-prompt.json");
const fileExists = fs.pathExistsSync(extensionPromptedPath);
return fileExists;
}
export function writePromptedForHHVSCode() {
const configDir = getConfigDirSync();
const extensionPromptedPath = path.join(configDir, "extension-prompt.json");
fs.writeFileSync(extensionPromptedPath, "{}");
}
export function getVarsFilePath(): string {
return path.join(getConfigDirSync(), "vars.json");
}