forked from Ekinoxx0/gtautil-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
110 lines (89 loc) · 2.83 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
const vscode = require('vscode');
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
const openedFileNames = [];
function checkExistsWithTimeout(filePath, timeout) {
return new Promise(function (resolve, reject) {
var timer = setTimeout(function () {
watcher.close();
reject(new Error('File did not exists and was not created during the timeout.'));
}, timeout);
fs.access(filePath, fs.constants.R_OK, function (err) {
if (!err) {
clearTimeout(timer);
watcher.close();
resolve();
}
});
var dir = path.dirname(filePath);
var basename = path.basename(filePath);
var watcher = fs.watch(dir, function (eventType, filename) {
if (eventType === 'rename' && filename === basename) {
clearTimeout(timer);
watcher.close();
resolve();
}
});
});
}
function activate(context) {
vscode.workspace.onDidOpenTextDocument(e => {
var fileName = e.fileName
const ext = fileName.split('.').reverse()[0];
switch(ext) {
case 'ymap':
case 'ytyp':
case 'ymt': {
openedFileNames.push(fileName + '.xml');
const gtautil = spawn('gtautil', ['exportmeta', '-i', fileName.substring(fileName.lastIndexOf('\\')+1, fileName.length)], {
cwd: fileName.substring(0, fileName.lastIndexOf("\\")+1)
});
gtautil.stdout.on('data', data => {
//console.log('STDOUT : ' + data);
});
gtautil.stderr.on('data', data => {
//console.log('STDERR : ' + data);
});
gtautil.on('exit', code => {
checkExistsWithTimeout(fileName + '.xml', 5000).then(_ => {
const openPath = vscode.Uri.parse('file:///' + fileName + '.xml');
vscode.commands.executeCommand('workbench.action.closeActiveEditor')
vscode.workspace.openTextDocument(openPath).then(doc => {
vscode.window.showTextDocument(doc);
});
}).catch(_ => {
vscode.window.showErrorMessage('GTAUtil failed at exporting an xml for this file within 5s')
})
});
break;
}
default: break;
}
});
vscode.workspace.onDidCloseTextDocument(e => {
const fileName = e.fileName.replace('.git', '')
const idx = openedFileNames.indexOf(fileName);
if(idx !== -1) {
openedFileNames.splice(idx, 1);
}
});
vscode.workspace.onDidSaveTextDocument(e => {
const fileName = e.fileName.replace('.git', '')
if(openedFileNames.indexOf(fileName) !== -1) {
const gtautil = spawn('GTAUtil', ['importmeta', '-i', fileName]);
gtautil.stdout.on('data', data => {
console.log(data);
});
gtautil.stderr.on('data', data => {
console.log(data);
});
}
});
}
exports.activate = activate;
function deactivate() {}
module.exports = {
activate,
deactivate
}