forked from mohamed-chs/Project-Layout-VSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
235 lines (191 loc) · 7.15 KB
/
extension.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Importing necessary modules
const vscode = require("vscode");
const fs = require("fs");
const path = require("path");
/**
* @param {vscode.ExtensionContext} context
*/
// Activating the extension
function activate(context) {
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "Project-Layout-VSCode" is now active!');
// Registering the command and pushing it to the context subscriptions
let disposable = vscode.commands.registerCommand(
"extension.generateProjectLayout",
generateProjectLayout
);
context.subscriptions.push(disposable);
}
// Function to generate the project layout
function generateProjectLayout() {
// Getting the root path
const rootPath = vscode.workspace.workspaceFolders
? vscode.workspace.workspaceFolders[0].uri.fsPath
: undefined;
if (!rootPath) {
vscode.window.showErrorMessage("No workspace folder is open");
return;
}
// Getting the settings
const config = vscode.workspace.getConfiguration('vscodeProjectLayout');
const outputFileName = config.get('outputFileName');
const outputFileLocation = config.get('outputFileLocation');
const additionalIgnoreDirs = config.get('additionalIgnoreDirs');
const ignoreExtensions = config.get('ignoreExtensions');
const outputFormat = config.get('outputFormat');
// Getting the directories to ignore
let ignoreDirs = getDirectoriesToIgnore();
ignoreDirs.push(...additionalIgnoreDirs);
// Starting the output string
let output = "";
switch (outputFormat) {
case 'markdown':
output += "--- Folder Layout ---\n";
output += getFolderLayoutMarkdown(rootPath, ignoreDirs, ignoreExtensions, 0);
break;
case 'json':
output += JSON.stringify(getFolderLayoutJson(rootPath, ignoreDirs, ignoreExtensions), null, 2);
break;
default:
output += "--- Folder Layout ---\n";
output += getFolderLayoutPlain(rootPath, ignoreDirs, ignoreExtensions, 0);
}
// Determine the file extension based on the output format
let fileExtension;
switch (outputFormat) {
case 'json':
fileExtension = '.json';
break;
case 'markdown':
fileExtension = '.md';
break;
default:
fileExtension = '.txt';
}
// Add the extension to the output file name
const completeOutputFileName = `${outputFileName}${fileExtension}`;
// Generating the output path
const outputPath = path.join(rootPath, outputFileLocation, completeOutputFileName);
// Writing the output to the file
fs.writeFileSync(outputPath, output);
// Showing a message that the project layout was generated successfully
vscode.window.showInformationMessage("Project layout generated successfully");
}
// Functions to get the folder layout
// Function to get the folder layout in JSON format
function getFolderLayoutJson(dir, ignoreDirs, ignoreExtensions) {
let layout = {};
// If the directory exists
if (fs.existsSync(dir)) {
// Reading the files in the directory
const files = fs.readdirSync(dir);
files.forEach((file) => {
const fullPath = path.join(dir, file);
const relativePath = path.relative(process.cwd(), fullPath);
// If the file is a directory
if (fs.lstatSync(fullPath).isDirectory()) {
if (shouldIgnore(relativePath, ignoreDirs, ignoreExtensions, true)) {
layout[file] = "(...)";
} else {
layout[file] = getFolderLayoutJson(fullPath, ignoreDirs, ignoreExtensions);
}
} else {
// If the file is not a directory and should not be ignored
if (!shouldIgnore(relativePath, ignoreDirs, ignoreExtensions)) {
layout[file] = null;
}
}
});
}
return layout;
}
// Function to get the folder layout in Markdown format
function getFolderLayoutMarkdown(dir, ignoreDirs, ignoreExtensions, level) {
let output = "";
// If the directory exists
if (fs.existsSync(dir)) {
// Reading the files in the directory
const files = fs.readdirSync(dir);
files.forEach((file) => {
const fullPath = path.join(dir, file);
const relativePath = path.relative(process.cwd(), fullPath);
const indent = " ".repeat(level * 2);
if (fs.lstatSync(fullPath).isDirectory()) {
if (shouldIgnore(relativePath, ignoreDirs, ignoreExtensions, true)) {
output += `${indent}- ${file}/ (...)\n`;
} else {
output += `${indent}- ${file}/\n`;
output += getFolderLayoutMarkdown(fullPath, ignoreDirs, ignoreExtensions, level + 1);
}
} else if (!shouldIgnore(relativePath, ignoreDirs, ignoreExtensions)) {
output += `${indent}- ${file}\n`;
}
});
}
return output;
}
// Function to get the folder layout in plain format
function getFolderLayoutPlain(dir, ignoreDirs, ignoreExtensions, level) {
let output = "";
// If the directory exists
if (fs.existsSync(dir)) {
// Reading the files in the directory
const files = fs.readdirSync(dir);
files.forEach((file, index) => {
const fullPath = path.join(dir, file);
const relativePath = path.relative(process.cwd(), fullPath);
const isLastFile = index === files.length - 1;
const prefix = level === 0 ? "" : isLastFile ? "└── " : "├── ";
const indent = " ".repeat(level * 4) + (level > 0 ? prefix : "");
if (fs.lstatSync(fullPath).isDirectory()) {
if (shouldIgnore(relativePath, ignoreDirs, ignoreExtensions, true)) {
output += indent + `[${file}] (...)\n`;
} else {
output += indent + `[${file}]\n`;
output += getFolderLayoutPlain(fullPath, ignoreDirs, ignoreExtensions, level + 1);
}
} else if (!shouldIgnore(relativePath, ignoreDirs, ignoreExtensions)) {
output += indent + file + "\n";
}
});
}
return output;
}
// Function to get the directories to ignore
function getDirectoriesToIgnore() {
// Getting the root path
const rootPath = vscode.workspace.workspaceFolders
? vscode.workspace.workspaceFolders[0].uri.fsPath
: undefined;
// If there's no workspace, return an empty array
if (!rootPath) {
return [];
}
// Getting the settings
const config = vscode.workspace.getConfiguration('vscodeProjectLayout');
const relativePathToIgnore = config.get('ignoreDirPath');
// Resolve the absolute path to the ignore file
const absolutePathToIgnore = path.resolve(rootPath, relativePathToIgnore);
if (!fs.existsSync(absolutePathToIgnore)) {
return [];
}
// Reading the contents of the file and splitting it by lines
let contents = fs.readFileSync(absolutePathToIgnore, 'utf8');
let dirs = contents.split('\n').map(line => line.trim()).filter(line => line !== '');
return dirs;
}
// Function to determine if a file should be ignored
function shouldIgnore(relativePath, ignoreDirs, ignoreExtensions, isDirectory = false) {
if (isDirectory) {
return ignoreDirs.some(dir => relativePath.includes(dir));
}
const fileExtension = path.extname(relativePath);
return ignoreExtensions.includes(fileExtension);
}
// Deactivating the extension
function deactivate() { }
// Exporting the activate and deactivate functions
module.exports = {
activate,
deactivate,
};