-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
98 lines (74 loc) · 3.11 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
const vscode = require('vscode');
const fs = require('fs');
const path = require('path');
function activate(context) {
let importSidePanelCommand = vscode.commands.registerCommand('extension.importSidePanelStructure', async function () {
await importStructure('sidepanel.json');
});
let importPopupCommand = vscode.commands.registerCommand('extension.importPopupStructure', async function () {
await importStructure('popup.json');
});
context.subscriptions.push(importSidePanelCommand);
context.subscriptions.push(importPopupCommand);
}
async function importStructure(configFileName) {
const workspaceFolders = vscode.workspace.workspaceFolders;
if (!workspaceFolders) {
vscode.window.showErrorMessage('Please open a folder in VSCode before running this command.');
return;
}
const rootPath = workspaceFolders[0].uri.fsPath;
const configPath = path.join(__dirname, 'templates', configFileName); // Updated to look inside the 'templates' folder
console.log('Looking for configuration file at:', configPath); // Log the path being checked
if (!fs.existsSync(configPath)) {
vscode.window.showErrorMessage('Configuration file not found.');
return;
}
const jsonContent = fs.readFileSync(configPath, 'utf8');
try {
const structure = JSON.parse(jsonContent);
createStructure(rootPath, structure);
vscode.window.showInformationMessage('Project structure imported successfully!');
} catch (error) {
vscode.window.showErrorMessage(`Failed to import project structure: ${error.message}`);
}
}
function createStructure(basePath, structure) {
for (const folder in structure) {
const folderPath = path.join(basePath, folder);
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath);
}
const folderContents = structure[folder];
const files = folderContents.files || [];
const subfolders = folderContents.folders || [];
// Create files with content from templates
files.forEach((file) => {
let destinationFile = file;
// Rename manifest file to manifest.json if needed
if (file.startsWith('manifest-')) {
destinationFile = 'manifest.json';
}
const filePath = path.join(folderPath, destinationFile);
console.log('Writing file to:', filePath);
if (!fs.existsSync(filePath)) {
const templatePath = path.join(__dirname, 'templates', file);
let content = '';
if (fs.existsSync(templatePath)) {
content = fs.readFileSync(templatePath, 'utf8');
}
fs.writeFileSync(filePath, content);
}
});
// Recursively handle subfolders
subfolders.forEach((subfolder) => {
const subfolderStructure = structure[folder][subfolder] || {};
createStructure(folderPath, { [subfolder]: subfolderStructure });
});
}
}
function deactivate() {}
module.exports = {
activate,
deactivate
};