-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
118 lines (95 loc) · 3.95 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
const vscode = require('vscode');
const haste = require('hastebin-gen')
const ncp = require("copy-paste");
const psty = require('@conorthedev/ptsy-node');
const path = require('path')
const detect = require('language-detect')
const extension = vscode.extensions.getExtension('ConorTheDev.vsc-haste')
/**
* Upload code to psty / a haste based host
* @param {String} host
* @param {String} code
* @param {Array} data
*/
function upload(host, code, data = {}, filename) {
if (host == "https://psty.io") {
var ext = detect.filename(filename).toLowerCase()
psty(code, data['theme'], `${ext}`).then(out => {
ncp.copy(out, function () {
console.log(`${extension.packageJSON.displayName}: URL: ${out} - Copied to clipboard!`)
vscode.window.showInformationMessage(`URL: ${out} - Copied to clipboard!`);
})
}).catch(error => {
vscode.window.showErrorMessage(`Failed to upload selected code to ${host} - Error: ${error}`);
});
} else {
var ext = path.extname(filename).replace('.', '')
if (ext == null) {
ext = "txt"
}
haste(code, { url: host, extension: `${ext}` }).then(out => {
ncp.copy(out, function () {
console.log(`${extension.packageJSON.displayName}: URL: ${out} - Copied to clipboard!`)
vscode.window.showInformationMessage(`URL: ${out} - Copied to clipboard!`);
})
}).catch(error => {
vscode.window.showErrorMessage(`Failed to upload selected code to ${host} - Error: ${error}`);
});
}
}
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
console.log(`${extension.packageJSON.displayName}: Loaded ${extension.packageJSON.publisher}.${extension.packageJSON.displayName} v${extension.packageJSON.version}`)
let uploadFile = vscode.commands.registerCommand('extension.vsc-haste.upload-file', function () {
const configuration = vscode.workspace.getConfiguration();
const code = vscode.window.activeTextEditor.document.getText()
const filename = path.basename(vscode.window.activeTextEditor.document.fileName)
var host = configuration.get('vsc-haste.host');
var theme = configuration.get('vsc-haste.psty.theme');
if (theme == null) {
vscode.workspace.getConfiguration().update('vsc-haste.psty.theme', "default", vscode.ConfigurationTarget.Global);
theme = "default"
}
if (host == null) {
vscode.workspace.getConfiguration().update('vsc-haste.host', "https://hasteb.in", vscode.ConfigurationTarget.Global);
currentValue = "https://hasteb.in"
}
if (code == "") {
vscode.window.showErrorMessage(`This file is empty! Put some code in this file and try again`);
return;
}
vscode.window.showInformationMessage(`Uploading ${filename} to ${host}`);
upload(host, code, { 'theme': theme }, filename)
});
let uploadSelection = vscode.commands.registerCommand('extension.vsc-haste.upload-file-select', function () {
const configuration = vscode.workspace.getConfiguration();
const code = vscode.window.activeTextEditor.document.getText(vscode.window.activeTextEditor.selection)
const filename = path.basename(vscode.window.activeTextEditor.document.fileName)
var host = configuration.get('vsc-haste.host');
var theme = configuration.get('vsc-haste.psty.theme');
if (theme == null) {
vscode.workspace.getConfiguration().update('vsc-haste.psty.theme', "default", vscode.ConfigurationTarget.Global);
theme = "default"
}
if (host == null) {
vscode.workspace.getConfiguration().update('vsc-haste.host', "https://hasteb.in", vscode.ConfigurationTarget.Global);
currentValue = "https://hasteb.in"
}
if (code == "") {
vscode.window.showErrorMessage(`You have selected no code! Select some code and try again`);
return;
}
vscode.window.showInformationMessage(`Uploading selected code in ${filename} to ${host}`);
upload(host, code, { 'theme': theme }, filename)
});
context.subscriptions.push(uploadFile);
context.subscriptions.push(uploadSelection);
}
exports.activate = activate;
function deactivate() { }
module.exports = {
activate,
deactivate
}