This repository has been archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextension.js
243 lines (224 loc) · 8.4 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
236
237
238
239
240
241
242
243
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const sh = require('shelljs');
const Shell = require('node-powershell');
const fs = require('fs');
const path = require('path');
var tmp = require('tmp');
const Output = vscode.window.createOutputChannel('Vscode Backup');
let ps;
let folderPath = '';
let documents = '';
if(process.platform === "win32") {
folderPath = path.join(process.env.APPDATA, 'Code\\User');
documents = path.join(process.env.USERPROFILE, 'Documents');
ps = new Shell({
executionPolicy: 'Bypass',
noProfile: true
});
}
else if(process.platform === "darwin"){
folderPath = path.join(process.env.HOME, "Library/Application Support/Code/User");
documents = path.join(process.env.HOME, 'Documents');
}
else {
folderPath = path.join(process.env.HOME, '.config/Code/User');
documents = path.join(process.env.HOME, 'Documents');
}
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
// Use the console to output diagnostic information (Output.appendLine) and errors (console.error)
// This line of code will only be executed once when your extension is activated
Output.appendLine('Congratulations, "vscode-backup" is now active!');
// vscode.commands.executeCommand('extension.backup');
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let command1 = vscode.commands.registerCommand('extension.backup', function () {
// The code you place here will be executed every time your command is executed
if(process.platform === "win32") {
ps.addCommand('code --list-extensions | % { "code --install-extension $_ " }');
ps.invoke()
.then(output => {
fs.writeFile(path.join(folderPath, 'plugins'), output, error => {
if(error) {
Output.appendLine(error.toString());
vscode.window.showErrorMessage('Failed to create backup file');
}
else {
archive();
}
});
})
.catch(err => {
Output.appendLine(err);
vscode.window.showErrorMessage('Failed to get extensions list');
});
}
else {
sh.exec('code --list-extensions | xargs -L 1 echo code --install-extension', (code, output) => {
fs.writeFile(path.join(folderPath, 'plugins'), output, error => {
if(error) {
Output.appendLine(error.toString());
vscode.window.showErrorMessage('Failed to create backup file');
}
else {
archive();
}
});
});
}
});
let command2 = vscode.commands.registerCommand('extension.restore', function () {
// The code you place here will be executed every time your command is executed
archive('extract');
setTimeout(()=>{
if(process.platform === "win32") {
var spawn = require("child_process").spawn,child;
child = spawn("powershell.exe",[path.join(folderPath, 'plugins.ps1')]);
child.stdout.on("data",function(data){
Output.appendLine(data);
});
child.stderr.on("data",function(data){
Output.appendLine("Errors: " + data);
vscode.window.showErrorMessage('Some Hiccups while Installing Extensions. Check Output for Details');
});
child.on("exit",function(){
Output.appendLine("Script finished");
vscode.window.showInformationMessage('Extensions Imported. Check Output for Details');
});
child.stdin.end(); //end input
}
else {
var spawn = require("child_process").spawn,child;
child = spawn("bash",[path.join(folderPath, 'plugins.sh')]);
child.stdout.on("data",function(data){
Output.appendLine(data);
});
child.stderr.on("data",function(data){
Output.appendLine("Errors: " + data);
vscode.window.showErrorMessage('Some Hiccups while Installing Extensions. Check Output for Details');
});
child.on("exit",function(){
Output.appendLine("Script finished");
vscode.window.showInformationMessage('Extensions Imported. Check Output for Details');
});
child.stdin.end(); //end input
}
},2000);
});
let command3 = vscode.commands.registerCommand('extension.open-directory', function () {
if(process.platform === "win32") {
require('child_process').exec('start "" "'+documents+'"');
}
else if(process.platform === "darwin") {
require('child_process').exec('open "" "'+documents+'"');
}
else {
require('child_process').exec('xdg-open "" "'+documents+'"', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
});
}
});
context.subscriptions.push(command1, command2, command3);
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {}
function archive(mode = 'create') { //implemet node-zip after mac test
if(mode === 'create') {
// @ts-ignore
var zip = new require('node-zip')();
zip.file('plugins', fs.readFileSync(path.join(folderPath, 'plugins')));
if(fs.existsSync(path.join(folderPath, 'settings.json'))) {
zip.file('settings.json', fs.readFileSync(path.join(folderPath, 'settings.json')));
}
// if(fs.existsSync(path.join(documents, 'vs_backup.zip'))) {
// var file = fs.readFileSync(path.join(documents, 'vs_backup.zip'));
// // @ts-ignore
// var currentZip = new require('node-zip')(file, {base64: false, checkCRC32: true});
// var currentCount, tmpobj, oldCount;
// if(process.platform === 'win32') {
// currentCount = Count(path.join(folderPath, 'plugins.ps1'));
// tmpobj = tmp.fileSync({ prefix: 'vscode-', postfix: '.ps1' });
// fs.writeFileSync(tmpobj.name, currentZip.files['plugins.ps1']._data);
// }
// else {
// currentCount = Count(path.join(folderPath, 'plugins.sh'));
// tmpobj = tmp.fileSync({ prefix: 'vscode-', postfix: '.sh' });
// fs.writeFileSync(tmpobj.name, currentZip.files['plugins.sh']._data);
// }
// oldCount = Count(tmpobj.name);
// if(oldCount > currentCount) {
// vscode.window.showInformationMessage('The Current Backup file seems to have more stuff. Wanna oevrwite?', 'Yes', 'No')
// .then(selection => {
// if(selection === 'Yes') {
// var data = zip.generate({ base64:false, compression: 'DEFLATE' });
// fs.writeFileSync(path.join(documents, 'vs_backup.zip'), data, 'binary');
// vscode.window.showInformationMessage('Backup File Created');
// }
// else {
// vscode.window.showInformationMessage('Backup Canceled');
// }
// })
// }
// else if(oldCount < currentCount || oldCount == currentCount){
// var data = zip.generate({ base64:false, compression: 'DEFLATE' });
// fs.writeFileSync(path.join(documents, 'vs_backup.zip'), data, 'binary');
// vscode.window.showInformationMessage('Backup File Created');
// }
// tmpobj.removeCallback();
// }
// else {
var data = zip.generate({ base64:false, compression: 'DEFLATE' });
fs.writeFileSync(path.join(documents, 'vs_backup.zip'), data, 'binary');
vscode.window.showInformationMessage('Backup File Created');
// }
}
else if(mode === 'extract') {
var file = fs.readFileSync(path.join(documents, 'vs_backup.zip'));
// @ts-ignore
var zip = new require('node-zip')(file, {base64: false, checkCRC32: true});
if(zip.files['settings.json']) {
fs.writeFileSync(path.join(folderPath, 'settings.json'), zip.files['settings.json']._data);
vscode.window.showInformationMessage('User Settings Imported Successfully');
}
if(process.platform === 'win32') {
fs.writeFileSync(path.join(folderPath, 'plugins.ps1'), zip.files['plugins']._data);
}
else if(process.platform === 'darwin') {
fs.writeFileSync(path.join(folderPath, 'plugins.sh'), zip.files['plugins']._data);
}
else {
fs.writeFileSync(path.join(folderPath, 'plugins.sh'), zip.files['plugins']._data);
}
vscode.window.showWarningMessage('Installing Plugins. Do Not Close the Window. Check Output log for details.');
}
}
function Count(file) {
var i;
var count = 0;
fs.createReadStream(file)
.on('data', function(chunk) {
for (i=0; i < chunk.length; ++i)
if (chunk[i] == 10) count++;
})
.on('end', function() {
console.log(count);
return count;
});
}
module.exports = {
activate,
deactivate
}