-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
109 lines (101 loc) · 4.05 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
// Libraries
const vscode = require('vscode');
const path = require('path');
const fs = require('fs-extra');
const ERRORS = {
misconfiguration: 'Please configure Tesserak paths before',
unreplaceable: 'A file in the same position already exists.\nYour settings avoid to override it'
};
const MESSAGES = {
successCopyed: 'Copied file to output location',
successCopyedFolder: 'Copied folder and content to output location'
};
class Tesserak {
constructor() {
const settings = vscode.workspace.getConfiguration('tesserak');
if (!settings.has('pathMapping') || typeof settings.get('pathMapping')[0].input === 'undefined' || typeof settings.get('pathMapping')[0].output === 'undefined') {
this.showErrorMessage('misconfiguration');
this.hasConfig = false;
} else {
this.paths = settings.get('pathMapping');
// this.inputPath = settings.get('pathMapping')[0].input;
// this.outpuPath = settings.get('pathMapping')[0].output;
this.hasConfig = true;
}
this.replaceIfExists = (!settings.has('replaceIfExists') || typeof settings.replaceIfExists !== 'boolean') ? true : settings.get('replaceIfExists');
}
showErrorMessage(error) {
vscode.window.showErrorMessage(ERRORS[error]);
}
showMessage(message) {
vscode.window.showInformationMessage(MESSAGES[message]);
}
showMessageAndOpenFile(message, file) {
vscode.window.showInformationMessage(MESSAGES[message], 'open file').then(() => {
vscode.workspace.openTextDocument(file).then((doc) => {
vscode.window.showTextDocument(doc);
});
});
}
displayStatusBarMessage(message) {
this.statusBar = vscode.window.createStatusBarItem(1);
this.statusBar.text = MESSAGES[message];
this.statusBar.show();
this.timer = setTimeout(() => {
this.statusBar.hide();
this.statusBar = null;
}, 3000);
}
file() {
if (this.inputFiles.length) {
this.inputFiles.forEach(file => {
const outputFile = this.getOutputFile(file.fsPath);
if (!this.skipFile(outputFile) && outputFile !== '') {
fs.mkdirp(path.dirname(outputFile)).then(() => {
fs.copy(file.fsPath, outputFile).then(() => {
this.displayStatusBarMessage('successCopyed');
if (!!path.extname(file.fsPath)) {
this.showMessageAndOpenFile('successCopyed', outputFile);
} else {
this.showMessage('successCopyedFolder');
}
});
});
} else {
this.showErrorMessage('unreplaceable');
}
});
}
}
skipFile(file) {
return this.replaceIfExists === false && fs.pathExistsSync(file);
}
getOutputFile(file) {
const relativeFile = vscode.workspace.asRelativePath(file, false);
this.workspacePath = file.replace(relativeFile, '');
let pathToMap = this.paths.filter(path => relativeFile.startsWith(path.input))[0];
return (pathToMap !== undefined && typeof pathToMap === 'object') ? `${this.workspacePath}${pathToMap.output}${relativeFile.replace(pathToMap.input, '')}` : '';
}
dispose() {
clearTimeout(this.timer);
this.timer = null;
this.statusBar.dispose();
this.statusBar = null;
this._pathMapping = null;
}
}
function activate() {
const tesserak = new Tesserak();
vscode.commands.registerCommand('extension.tesserakThis', (file, files) => {
if (tesserak.hasConfig) {
const fileList = files.length ? files : [file];
tesserak.inputFiles = fileList;
tesserak.file();
} else {
tesserak.showErrorMessage();
}
});
}
exports.activate = activate;
function deactivate() { }
exports.deactivate = deactivate;