-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
72 lines (59 loc) · 2.1 KB
/
extension.js
File metadata and controls
72 lines (59 loc) · 2.1 KB
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
const vscode = require("vscode");
const fs = require("fs");
const path = require("path");
function activate(context) {
let disposable = vscode.commands.registerCommand(
"extractEnvVars",
async function () {
const workspaceFolders = vscode.workspace.workspaceFolders;
if (!workspaceFolders) {
vscode.window.showErrorMessage("No folder open.");
return;
}
const rootPath = workspaceFolders[0].uri.fsPath;
const envVars = new Set();
async function readFiles(dir) {
const files = fs.readdirSync(dir);
for (const file of files) {
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
const skipDirs = [
"node_modules",
"dist",
"build",
".vscode",
".git",
];
if (skipDirs.includes(file)) {
continue; // skip these folders
}
await readFiles(fullPath);
} else if (file.endsWith(".js") || file.endsWith(".ts")) {
const content = fs.readFileSync(fullPath, "utf-8");
// const matches = [...content.matchAll(/process\.env\.([A-Z0-9_]+)/g)];
// matches.forEach(match => envVars.add(match[1]));
const allMatches = [
...content.matchAll(/process\.env\.([a-zA-Z0-9_]+)/g), // Node.js & Next.js
...content.matchAll(/import\.meta\.env\.([a-zA-Z0-9_]+)/g), // Vite & SvelteKit
];
allMatches.forEach((match) => envVars.add(match[1]));
}
}
}
await readFiles(rootPath);
const envText = Array.from(envVars)
.map((v) => `${v}=`)
.join("\n");
const envFile = path.join(rootPath, ".env-HMK_CodeWeb");
fs.writeFileSync(envFile, envText);
vscode.window.showInformationMessage(
`.env file created with ${envVars.size} variables.`
);
}
);
context.subscriptions.push(disposable);
}
exports.activate = activate;
function deactivate() {}
exports.deactivate = deactivate;