-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver-controller.js
181 lines (149 loc) · 5.89 KB
/
server-controller.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
const { window, workspace, ProgressLocation } = require('vscode');
const { getProject } = require('./project-resolver');
const { openBrowser } = require('./open');
const path = require('path');
const { spawn } = require('child_process');
const { LiveShareController } = require('./live-share-controller');
const { pickFile } = require('./file-picker');
const StatusbarUi = require('./statusbar-ui');
/* eslint-disable no-magic-numbers */
exports.ServerController = class ServerController {
constructor() {
this.servers = new Map;
this.buildServers = new Map;
const showOnStatusbar = workspace.getConfiguration('hqServer').get('showOnStatusbar');
if (showOnStatusbar) {
StatusbarUi.init();
this.documentChangeSubscription = window.onDidChangeActiveTextEditor(() => {
const { workspaceFolders } = workspace;
if (!workspaceFolders) return StatusbarUi.disable();
const activeProject = getProject(workspaceFolders);
if (!activeProject) return StatusbarUi.disable();
if (this.servers.has(activeProject.uri.fsPath)) {
const { url } = this.servers.get(activeProject.uri.fsPath);
StatusbarUi.stop(url);
} else StatusbarUi.start();
if (this.buildServers.has(activeProject.uri.fsPath)) {
const { url } = this.buildServers.get(activeProject.uri.fsPath);
StatusbarUi.buildProgress(url);
} else StatusbarUi.build();
return null;
});
}
}
async start(arg, resource) {
const { workspaceFolders } = workspace;
if (!workspaceFolders) {
return window.showErrorMessage('Open a folder or workspace... (File -> Open)');
}
if (workspaceFolders.length === 0) {
return window.showErrorMessage('Add folder to the workspace... (File -> Add Folder to Workspace..)');
}
const activeProject = getProject(workspaceFolders);
if (!activeProject) return StatusbarUi.disable();
// if server is already running for this project, just open preview in a browser
if (arg !== 'build' && this.servers.has(activeProject.uri.fsPath)) {
const { url } = this.servers.get(activeProject.uri.fsPath);
await openBrowser(url);
return null;
}
// if it is a build resource command pick a resource
let resourceName;
if (arg === 'build' && resource) {
resourceName = await pickFile(activeProject);
}
// otherwise start new server
const defaultPort = workspace.getConfiguration('hqServer').get('defaultPort');
const projectPath = activeProject.uri.fsPath;
const nodeVersionProcess = spawn(
'node',
[ '--version' ],
{
cwd: projectPath,
stdio: [ 'pipe', 'pipe', 'pipe', 'ipc' ],
},
);
nodeVersionProcess.stdout.on('data', v => {
const version = String(v);
const [ major, minor ] = version
.slice(1)
.split('.')
.map(x => Number(x));
const jsModules = major < 14;
const notSupported = major < 12 ||
(major === 12 && minor < 10);
if (notSupported) {
return window.showErrorMessage(`System default node ${version} is not supported, please install node >= v12.10.0, make it default and restart VSCode: nvm i 12 && nvm alias default 12`);
}
const args = [ '--no-warnings' ];
if (jsModules) args.push('--experimental-modules');
const hqArgs = [ ...args, path.join(__dirname, './run.mjs'), projectPath, defaultPort, arg ];
if (resource) hqArgs.push(resourceName);
const hq = spawn(
'node',
hqArgs,
{
cwd: projectPath,
stdio: [ 'pipe', 'pipe', 'pipe', 'ipc' ],
},
);
hq.stdout.on('data', data => console.log(String(data)));
hq.stderr.on('data', data => console.error(String(data)));
if (arg === 'build') {
window.withProgress({
cancellable: false,
location: ProgressLocation.Notification,
title: `Building ${activeProject.uri.fsPath}`,
}, () => new Promise(resolve => {
hq.on('exit', () => {
StatusbarUi.build();
this.buildServers.delete(activeProject.uri.fsPath);
window.showInformationMessage(`Build completed ${activeProject.uri.fsPath}`);
resolve();
});
}));
}
hq.on('message', url => {
if (arg === 'build') {
StatusbarUi.buildProgress();
this.buildServers.set(activeProject.uri.fsPath, { hq, url });
} else {
const liveShareController = new LiveShareController;
this.servers.set(activeProject.uri.fsPath, { hq, liveShareController, url });
StatusbarUi.stop(url);
liveShareController.share(activeProject.name, url);
openBrowser(url);
}
});
return null;
});
nodeVersionProcess.stderr.on('data', data => console.error(String(data)));
return null;
}
stop() {
const { workspaceFolders } = workspace;
if (!workspaceFolders) {
return window.showErrorMessage('Open a folder or workspace... (File -> Open)');
}
if (workspaceFolders.length === 0) {
return window.showErrorMessage('Add folder to the workspace... (File -> Add Folder to Workspace..)');
}
const activeProject = getProject(workspaceFolders);
if (!activeProject) return null;
if (!this.servers.has(activeProject.uri.fsPath)) {
return window.showErrorMessage(`Server is not running for project ${activeProject.name}`);
}
const { hq, liveShareController } = this.servers.get(activeProject.uri.fsPath);
hq.kill();
this.servers.delete(activeProject.uri.fsPath);
StatusbarUi.start();
liveShareController.stop();
return null;
}
dispose() {
for (const { hq } of this.servers.values()) hq.kill();
for (const { hq } of this.buildServers.values()) hq.kill();
StatusbarUi.dispose();
if (this.documentChangeSubscription) this.documentChangeSubscription.dispose();
}
};