-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
145 lines (114 loc) · 3.43 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
const vscode = require("vscode");
const { exec } = require("child_process");
const kill = require("tree-kill");
let window = vscode.window;
const strings = {
extensionName: "fast-gulp",
outputChannelName: "Fast Gulp",
noExtensionFolder: "No workspace folder found.",
gulp: {
isNotStarted: "'gulp' is not started.",
isAlreadyStarted: "'gulp' is already started.",
},
statusBar: {
start: "Start 'gulp'",
startDisplay: "$(play) gulp",
stop: "Stop 'gulp'",
stopDisplay: "$(stop) gulp",
},
log: {
start: "Starting 'gulp'...",
stop: "Stopping 'gulp'...",
},
};
const commands = {
startGulp: `${strings.extensionName}.startGulp`,
stopGulp: `${strings.extensionName}.stopGulp`,
};
let cProcess = null; // Variable to store the running gulp process
const statusBarItem = window.createStatusBarItem(
vscode.StatusBarAlignment.Right
);
const outputChannel = window.createOutputChannel(strings.outputChannelName);
function cleanText(text) {
/* to make the text ready for output channel */
// remove color ANSI codes
text = text.replace(/\x1B\[[0-9;]*[JKmsu]/g, "");
return text;
}
function outputToChannel(text) {
return outputChannel.appendLine(cleanText(text));
}
function startGulp() {
outputToChannel(strings.log.start);
statusBarItem.text = strings.statusBar.stopDisplay;
statusBarItem.tooltip = strings.statusBar.stop;
statusBarItem.command = commands.stopGulp;
statusBarItem.show();
outputChannel.show();
if (cProcess) {
window.showErrorMessage(strings.gulp.isAlreadyStarted);
}
cProcess = exec("gulp");
cProcess.stdout.on("data", (data) => {
outputToChannel(`[STDOUT]: ${data.toString()}`);
});
cProcess.stderr.on("data", (data) => {
outputToChannel(`[STDERR]: ${data.toString()}`);
});
cProcess.on("close", (code) => {
if (code === 0 || code === 1) {
outputToChannel("Successfully closed.");
} else {
let close_text = `[--ERROR--]: Failed with exit code ${code}.`;
window.showErrorMessage(close_text);
outputToChannel(close_text);
}
});
cProcess.on("error", (error) => {
let error_text = `[--ERROR--]: ${error.message}`;
window.showErrorMessage(error_text);
outputToChannel(error_text);
cProcess = null;
});
outputToChannel("done");
}
function stopGulp() {
outputToChannel(strings.log.stop);
statusBarItem.text = strings.statusBar.startDisplay;
statusBarItem.tooltip = strings.statusBar.start;
statusBarItem.command = commands.startGulp;
statusBarItem.show();
if (cProcess) {
kill(cProcess.pid);
cProcess = null; // Reset gulpProcess
} else {
window.showErrorMessage(strings.gulp.isNotStarted);
}
outputToChannel("done");
}
function activate({ subscriptions }) {
outputToChannel("[INFO]: Activated the extension.");
statusBarItem.text = strings.statusBar.startDisplay;
statusBarItem.tooltip = strings.statusBar.start;
statusBarItem.command = commands.startGulp;
statusBarItem.show();
const workspacePath = vscode.workspace.workspaceFolders
.map((folder) => folder.uri.path)
.toString()
.slice(1);
if (!workspacePath) {
window.showErrorMessage(strings.noExtensionFolder);
return;
}
process.chdir(workspacePath);
subscriptions.push(
vscode.commands.registerCommand(commands.startGulp, startGulp),
vscode.commands.registerCommand(commands.stopGulp, stopGulp)
);
}
function deactivate() {}
module.exports = {
activate,
deactivate,
};